check args usage before running a command

This commit is contained in:
gutmet 2020-10-17 20:15:50 +02:00
parent 008c973565
commit 0c0c53f9df

View File

@ -38,6 +38,14 @@ func optLogFatal(decorum string, err error) {
} }
} }
func checkUsage(args []string, argcMin int, argcMax int, help string) {
argc := len(args)
if (argcMin > -1 && argc < argcMin) || (argcMax > -1 && argc > argcMax) {
fmt.Fprintf(os.Stderr, "USAGE: %s %s\n", os.Args[0], help)
os.Exit(-1)
}
}
func get(url string) []byte { func get(url string) []byte {
return _send(url, nil, false) return _send(url, nil, false)
} }
@ -245,26 +253,21 @@ func PrintTweets(tweets []Status, userFilter hashset) {
} }
func status(args []string) error { func status(args []string) error {
checkUsage(args, 1, -1, "status STATUS [FILE1 FILE2 ...]")
tweets := updateStatus(args, "", "") tweets := updateStatus(args, "", "")
PrintTweets(tweets, nil) PrintTweets(tweets, nil)
return nil return nil
} }
func reply(args []string) error { func reply(args []string) error {
if len(args) < 2 { checkUsage(args, 2, -1, "reply TWEET_ID MESSAGE [FILE1 FILE2 ...]")
fmt.Fprintln(os.Stderr, "Usage: drivel reply TWEET_ID MESSAGE [FILE1 FILE2 ...]")
os.Exit(-1)
}
tweets := updateStatus(args[1:], ObjectID(args[0]), "") tweets := updateStatus(args[1:], ObjectID(args[0]), "")
PrintTweets(tweets, nil) PrintTweets(tweets, nil)
return nil return nil
} }
func quote(args []string) error { func quote(args []string) error {
if len(args) < 2 { checkUsage(args, 2, -1, "quote TWEET_ID MESSAGE [FILE1 FILE2 ...]")
fmt.Println(os.Stderr, "Usage: drivel quote TWEET_ID MESSAGE [FILE1 FILE2 ...]")
os.Exit(-1)
}
tweets := updateStatus(args[1:], "", ObjectID(args[0])) tweets := updateStatus(args[1:], "", ObjectID(args[0]))
PrintTweets(tweets, nil) PrintTweets(tweets, nil)
return nil return nil
@ -280,10 +283,7 @@ func _lookup(ids []string) []Status {
} }
func lookup(args []string) error { func lookup(args []string) error {
if len(args) < 1 { checkUsage(args, 1, -1, "lookup TWEET_ID1 [TWEET_ID2 TWEET_ID3 ...]")
fmt.Fprintln(os.Stderr, "USAGE: drivel lookup TWEET_ID1 [TWEET_ID2 TWEET_ID3 ...]")
os.Exit(-1)
}
tweets := _lookup(args) tweets := _lookup(args)
PrintTweets(tweets, nil) PrintTweets(tweets, nil)
return nil return nil
@ -299,18 +299,21 @@ func timeline(endpoint string) []Status {
} }
func mentions(args []string) error { func mentions(args []string) error {
checkUsage(args, 0, 0, "mentions")
tweets := timeline(MENTIONS_ENDPOINT) tweets := timeline(MENTIONS_ENDPOINT)
PrintTweets(tweets, mentionsFilter) PrintTweets(tweets, mentionsFilter)
return nil return nil
} }
func home(args []string) error { func home(args []string) error {
checkUsage(args, 0, 0, "home")
tweets := timeline(HOME_ENDPOINT) tweets := timeline(HOME_ENDPOINT)
PrintTweets(tweets, homeFilter) PrintTweets(tweets, homeFilter)
return nil return nil
} }
func userTimeline(flags userTimelineFlags, args []string) error { func userTimeline(flags userTimelineFlags, args []string) error {
checkUsage(args, 1, 1, "timeline USER")
tweets := timeline(TIMELINE_ENDPOINT + UserTimelineParameters(flags, args[0])) tweets := timeline(TIMELINE_ENDPOINT + UserTimelineParameters(flags, args[0]))
PrintTweets(tweets, nil) PrintTweets(tweets, nil)
return nil return nil
@ -329,11 +332,8 @@ func userTimelineCommand() (goutil.CommandFlagsInit, goutil.CommandFunc) {
} }
func retweet(args []string) error { func retweet(args []string) error {
checkUsage(args, 1, 1, "retweet TWEET_ID")
log := func(err error) { optLogFatal("retweet", err) } log := func(err error) { optLogFatal("retweet", err) }
if len(args) != 1 {
fmt.Fprintln(os.Stderr, "USAGE: drivel retweet TWEET_ID")
os.Exit(-1)
}
id := args[0] id := args[0]
tweets := _lookup([]string{id}) tweets := _lookup([]string{id})
if len(tweets) != 1 { if len(tweets) != 1 {
@ -348,11 +348,8 @@ func retweet(args []string) error {
} }
func like(args []string) error { func like(args []string) error {
checkUsage(args, 1, 1, "like TWEET_ID")
log := func(err error) { optLogFatal("like", err) } log := func(err error) { optLogFatal("like", err) }
if len(args) != 1 {
fmt.Fprintln(os.Stderr, "USAGE: drivel like TWEET_ID")
os.Exit(-1)
}
body := send(LIKE_ENDPOINT, LikeRequest(args[0])) body := send(LIKE_ENDPOINT, LikeRequest(args[0]))
var tweet Status var tweet Status
err := json.Unmarshal(body, &tweet) err := json.Unmarshal(body, &tweet)
@ -414,7 +411,8 @@ func wipeTimeline(likes bool, keepDays int) {
} }
} }
func wipe(flags wipeFlags) error { func wipe(flags wipeFlags, args []string) error {
checkUsage(args, 0, 0, "wipe")
wipeTimeline(true, flags.keepDays) wipeTimeline(true, flags.keepDays)
wipeTimeline(false, flags.keepDays) wipeTimeline(false, flags.keepDays)
return nil return nil
@ -429,7 +427,7 @@ func wipeCommand() (goutil.CommandFlagsInit, goutil.CommandFunc) {
flagsInit := func(s *flag.FlagSet) { flagsInit := func(s *flag.FlagSet) {
s.IntVar(&f.keepDays, "keep-days", WIPE_KEEP_DAYS, "don't wipe the last N days") s.IntVar(&f.keepDays, "keep-days", WIPE_KEEP_DAYS, "don't wipe the last N days")
} }
return flagsInit, func([]string) error { return wipe(f) } return flagsInit, func(args []string) error { return wipe(f, args) }
} }
type hashset map[string]interface{} type hashset map[string]interface{}