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, "-")
}
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)
}

View file

@ -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)
}

View file

@ -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)
}

View file

@ -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)
}

3
um.go
View file

@ -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)
}