diff --git a/drivel.go b/drivel.go index 5f169c5..1f0cee4 100644 --- a/drivel.go +++ b/drivel.go @@ -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 { return _send(url, nil, false) } @@ -245,26 +253,21 @@ func PrintTweets(tweets []Status, userFilter hashset) { } func status(args []string) error { + checkUsage(args, 1, -1, "status STATUS [FILE1 FILE2 ...]") tweets := updateStatus(args, "", "") PrintTweets(tweets, nil) return nil } func reply(args []string) error { - if len(args) < 2 { - fmt.Fprintln(os.Stderr, "Usage: drivel reply TWEET_ID MESSAGE [FILE1 FILE2 ...]") - os.Exit(-1) - } + checkUsage(args, 2, -1, "reply TWEET_ID MESSAGE [FILE1 FILE2 ...]") tweets := updateStatus(args[1:], ObjectID(args[0]), "") PrintTweets(tweets, nil) return nil } func quote(args []string) error { - if len(args) < 2 { - fmt.Println(os.Stderr, "Usage: drivel quote TWEET_ID MESSAGE [FILE1 FILE2 ...]") - os.Exit(-1) - } + checkUsage(args, 2, -1, "quote TWEET_ID MESSAGE [FILE1 FILE2 ...]") tweets := updateStatus(args[1:], "", ObjectID(args[0])) PrintTweets(tweets, nil) return nil @@ -280,10 +283,7 @@ func _lookup(ids []string) []Status { } func lookup(args []string) error { - if len(args) < 1 { - fmt.Fprintln(os.Stderr, "USAGE: drivel lookup TWEET_ID1 [TWEET_ID2 TWEET_ID3 ...]") - os.Exit(-1) - } + checkUsage(args, 1, -1, "lookup TWEET_ID1 [TWEET_ID2 TWEET_ID3 ...]") tweets := _lookup(args) PrintTweets(tweets, nil) return nil @@ -299,18 +299,21 @@ func timeline(endpoint string) []Status { } func mentions(args []string) error { + checkUsage(args, 0, 0, "mentions") tweets := timeline(MENTIONS_ENDPOINT) PrintTweets(tweets, mentionsFilter) return nil } func home(args []string) error { + checkUsage(args, 0, 0, "home") tweets := timeline(HOME_ENDPOINT) PrintTweets(tweets, homeFilter) return nil } func userTimeline(flags userTimelineFlags, args []string) error { + checkUsage(args, 1, 1, "timeline USER") tweets := timeline(TIMELINE_ENDPOINT + UserTimelineParameters(flags, args[0])) PrintTweets(tweets, nil) return nil @@ -329,11 +332,8 @@ func userTimelineCommand() (goutil.CommandFlagsInit, goutil.CommandFunc) { } func retweet(args []string) error { + checkUsage(args, 1, 1, "retweet TWEET_ID") 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] tweets := _lookup([]string{id}) if len(tweets) != 1 { @@ -348,11 +348,8 @@ func retweet(args []string) error { } func like(args []string) error { + checkUsage(args, 1, 1, "like TWEET_ID") 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])) var tweet Status 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(false, flags.keepDays) return nil @@ -429,7 +427,7 @@ func wipeCommand() (goutil.CommandFlagsInit, goutil.CommandFunc) { flagsInit := func(s *flag.FlagSet) { 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{}