From c19b11e11528c60f5f3c7d1c73a07b6793f2551a Mon Sep 17 00:00:00 2001 From: bth Date: Sun, 18 Jan 2026 20:17:40 -0800 Subject: [PATCH] flags.ValidValueOrExit and os.Exit on Help --- flags/flags.go | 12 +++++++++--- next/next.go | 7 +++---- sort/sort.go | 19 +++++++++---------- tag/tag.go | 15 +++++++++------ um.go | 3 ++- 5 files changed, 32 insertions(+), 24 deletions(-) diff --git a/flags/flags.go b/flags/flags.go index 4d79115..72eafad 100644 --- a/flags/flags.go +++ b/flags/flags.go @@ -52,15 +52,20 @@ func HasDashPrefix(s string) bool { return strings.HasPrefix(s, "-") } -func MissingValueArg(args []string, i int) bool { +func MissingValue(args []string, i int) bool { return i+1 == len(args) || HasDashPrefix(args[i+1]) } -func HelpMissingVal(flag string) { - fmt.Printf("%s needs a value assignment\n", flag) +// if no value ahead in the args, print error and exit +func ValidValueOrExit(args []string, i int) { + if MissingValue(args, i) { + fmt.Printf("%s needs a value assignment\n", args[i]) + os.Exit(1) + } } // print out help string by reflecting over fields of provided opts struct +// and os.Exit(0) func Help(subcmd string, opts any) { v := reflect.ValueOf(opts) t := v.Type() @@ -89,4 +94,5 @@ func Help(subcmd string, opts any) { } fmt.Printf("um %s%s\n", subcmd, positional) w.Flush() + os.Exit(0) } diff --git a/next/next.go b/next/next.go index bd7da6d..fbfa19a 100644 --- a/next/next.go +++ b/next/next.go @@ -2,7 +2,6 @@ package next import ( "fmt" - "os" "github.com/brtholomy/um/go/flags" ) @@ -31,15 +30,15 @@ func parseArgs(args []string) options { opts.Tags.Val = arg case i >= 2: fmt.Println("um next : too many args") - os.Exit(1) + flags.Help("next", opts) case arg == opts.Help.Long || arg == opts.Help.Short: flags.Help("next", opts) - return opts } } return opts } func Next(args []string) { - _ = parseArgs(args) + opts := parseArgs(args) + fmt.Println(opts) } diff --git a/sort/sort.go b/sort/sort.go index 2827656..6a19a18 100644 --- a/sort/sort.go +++ b/sort/sort.go @@ -1,6 +1,8 @@ package sort import ( + "fmt" + "github.com/brtholomy/um/go/flags" ) @@ -23,25 +25,22 @@ func parseArgs(args []string) options { for i, arg := range args { switch { case arg == opts.Source.Long || arg == opts.Source.Short: - if flags.MissingValueArg(args, i) { - flags.HelpMissingVal(arg) - break - } + flags.ValidValueOrExit(args, i) opts.Source.Val = args[i+1] case arg == opts.Key.Long || arg == opts.Key.Short: - if flags.MissingValueArg(args, i) { - flags.HelpMissingVal(arg) - break - } + flags.ValidValueOrExit(args, i) opts.Key.Val = args[i+1] case arg == opts.Help.Long || arg == opts.Help.Short: flags.Help("sort", opts) - return opts + default: + fmt.Println("invalid argument") + flags.Help("sort", opts) } } return opts } func Sort(args []string) { - _ = parseArgs(args) + opts := parseArgs(args) + fmt.Println(opts) } diff --git a/tag/tag.go b/tag/tag.go index f335a09..6f2bfb1 100644 --- a/tag/tag.go +++ b/tag/tag.go @@ -1,6 +1,8 @@ package tag import ( + "fmt" + "github.com/brtholomy/um/go/flags" ) @@ -29,19 +31,20 @@ func parseArgs(args []string) options { case arg == opts.Invert.Long || arg == opts.Invert.Short: opts.Invert.Val = true case arg == opts.Date.Long || arg == opts.Date.Short: - if flags.MissingValueArg(args, i) { - flags.HelpMissingVal(arg) - break - } + // TODO: should we use reflection here for a general Valid(arg, opts.Field)? + flags.ValidValueOrExit(args, i) opts.Date.Val = args[i+1] case arg == opts.Help.Long || arg == opts.Help.Short: flags.Help("tag", opts) - return opts + default: + fmt.Println("invalid argument") + flags.Help("tag", opts) } } return opts } func Tag(args []string) { - _ = parseArgs(args) + opts := parseArgs(args) + fmt.Println(opts) } diff --git a/um.go b/um.go index fcbbb36..e2a485c 100644 --- a/um.go +++ b/um.go @@ -18,7 +18,7 @@ const ( sort Subcommand = "sort" ) -var help string = fmt.Sprintf("um [%s | %s | %s | %s]", tag, next, last, sort) +var help string = fmt.Sprintf("um [%s | %s | %s | %s] [--help | -h]", tag, next, last, sort) func main() { // TODO: do something useful: @@ -41,6 +41,7 @@ func main() { case sort: sortpkg.Sort(args) default: + fmt.Println("command not found") fmt.Println(help) os.Exit(1) }