flags : reflection-powered general ParseArgs : uglier than tidy switch statements per cmd, but much easier to add new flags

This commit is contained in:
bth 2026-01-21 11:44:14 -08:00
commit 6f9f5dbb23
5 changed files with 93 additions and 87 deletions

View file

@ -31,20 +31,6 @@ func initOpts() options {
}
}
func parseArgs(args []string) options {
opts := initOpts()
for _, arg := range args {
switch {
case arg == opts.Help.Long || arg == opts.Help.Short:
flags.Help(CMD, SUMMARY, opts)
default:
flags.HelpInvalidArg(CMD, arg)
flags.Help(CMD, SUMMARY, opts)
}
}
return opts
}
// get the lexical last file from GLOB
func GlobLast(glob string) (string, error) {
// NOTE: filepath.Glob is more reliable than a manual ls call:
@ -59,7 +45,9 @@ func GlobLast(glob string) (string, error) {
}
func Last(args []string) {
_ = parseArgs(args)
opts := initOpts()
flags.ParseArgs(CMD, SUMMARY, args, &opts)
s, err := GlobLast(GLOB)
if err != nil {
log.Fatalf("um %s: %v", cmd.Last, err)