flags.ValidValueOrExit and os.Exit on Help

This commit is contained in:
bth 2026-01-18 20:17:40 -08:00
commit c19b11e115
5 changed files with 32 additions and 24 deletions

View file

@ -52,15 +52,20 @@ func HasDashPrefix(s string) bool {
return strings.HasPrefix(s, "-") 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]) return i+1 == len(args) || HasDashPrefix(args[i+1])
} }
func HelpMissingVal(flag string) { // if no value ahead in the args, print error and exit
fmt.Printf("%s needs a value assignment\n", flag) 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 // print out help string by reflecting over fields of provided opts struct
// and os.Exit(0)
func Help(subcmd string, opts any) { func Help(subcmd string, opts any) {
v := reflect.ValueOf(opts) v := reflect.ValueOf(opts)
t := v.Type() t := v.Type()
@ -89,4 +94,5 @@ func Help(subcmd string, opts any) {
} }
fmt.Printf("um %s%s\n", subcmd, positional) fmt.Printf("um %s%s\n", subcmd, positional)
w.Flush() w.Flush()
os.Exit(0)
} }

View file

@ -2,7 +2,6 @@ package next
import ( import (
"fmt" "fmt"
"os"
"github.com/brtholomy/um/go/flags" "github.com/brtholomy/um/go/flags"
) )
@ -31,15 +30,15 @@ func parseArgs(args []string) options {
opts.Tags.Val = arg opts.Tags.Val = arg
case i >= 2: case i >= 2:
fmt.Println("um next : too many args") fmt.Println("um next : too many args")
os.Exit(1) flags.Help("next", opts)
case arg == opts.Help.Long || arg == opts.Help.Short: case arg == opts.Help.Long || arg == opts.Help.Short:
flags.Help("next", opts) flags.Help("next", opts)
return opts
} }
} }
return opts return opts
} }
func Next(args []string) { func Next(args []string) {
_ = parseArgs(args) opts := parseArgs(args)
fmt.Println(opts)
} }

View file

@ -1,6 +1,8 @@
package sort package sort
import ( import (
"fmt"
"github.com/brtholomy/um/go/flags" "github.com/brtholomy/um/go/flags"
) )
@ -23,25 +25,22 @@ func parseArgs(args []string) options {
for i, arg := range args { for i, arg := range args {
switch { switch {
case arg == opts.Source.Long || arg == opts.Source.Short: case arg == opts.Source.Long || arg == opts.Source.Short:
if flags.MissingValueArg(args, i) { flags.ValidValueOrExit(args, i)
flags.HelpMissingVal(arg)
break
}
opts.Source.Val = args[i+1] opts.Source.Val = args[i+1]
case arg == opts.Key.Long || arg == opts.Key.Short: case arg == opts.Key.Long || arg == opts.Key.Short:
if flags.MissingValueArg(args, i) { flags.ValidValueOrExit(args, i)
flags.HelpMissingVal(arg)
break
}
opts.Key.Val = args[i+1] opts.Key.Val = args[i+1]
case arg == opts.Help.Long || arg == opts.Help.Short: case arg == opts.Help.Long || arg == opts.Help.Short:
flags.Help("sort", opts) flags.Help("sort", opts)
return opts default:
fmt.Println("invalid argument")
flags.Help("sort", opts)
} }
} }
return opts return opts
} }
func Sort(args []string) { func Sort(args []string) {
_ = parseArgs(args) opts := parseArgs(args)
fmt.Println(opts)
} }

View file

@ -1,6 +1,8 @@
package tag package tag
import ( import (
"fmt"
"github.com/brtholomy/um/go/flags" "github.com/brtholomy/um/go/flags"
) )
@ -29,19 +31,20 @@ func parseArgs(args []string) options {
case arg == opts.Invert.Long || arg == opts.Invert.Short: case arg == opts.Invert.Long || arg == opts.Invert.Short:
opts.Invert.Val = true opts.Invert.Val = true
case arg == opts.Date.Long || arg == opts.Date.Short: case arg == opts.Date.Long || arg == opts.Date.Short:
if flags.MissingValueArg(args, i) { // TODO: should we use reflection here for a general Valid(arg, opts.Field)?
flags.HelpMissingVal(arg) flags.ValidValueOrExit(args, i)
break
}
opts.Date.Val = args[i+1] opts.Date.Val = args[i+1]
case arg == opts.Help.Long || arg == opts.Help.Short: case arg == opts.Help.Long || arg == opts.Help.Short:
flags.Help("tag", opts) flags.Help("tag", opts)
return opts default:
fmt.Println("invalid argument")
flags.Help("tag", opts)
} }
} }
return opts return opts
} }
func Tag(args []string) { func Tag(args []string) {
_ = parseArgs(args) opts := parseArgs(args)
fmt.Println(opts)
} }

3
um.go
View file

@ -18,7 +18,7 @@ const (
sort Subcommand = "sort" 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() { func main() {
// TODO: do something useful: // TODO: do something useful:
@ -41,6 +41,7 @@ func main() {
case sort: case sort:
sortpkg.Sort(args) sortpkg.Sort(args)
default: default:
fmt.Println("command not found")
fmt.Println(help) fmt.Println(help)
os.Exit(1) os.Exit(1)
} }