flags : more common help functions, but redefining the subcmds in each package is probably wrong, esp. since last had to be deduped

This commit is contained in:
bth 2026-01-20 13:10:47 -08:00
commit 77e3e23446
5 changed files with 31 additions and 14 deletions

View file

@ -74,6 +74,14 @@ func ValidateIncrementFetchOrExit(args []string, i int) (int, string) {
return i + 1, args[i+1]
}
func HelpRequired(cmd string, long string) {
log.Printf("um %s: %s is required", cmd, long)
}
func HelpInvalidArg(cmd string, arg string) {
log.Printf("um %s: invalid argument: %s", cmd, arg)
}
// print out help string by reflecting over fields of provided opts struct
// and os.Exit(0)
func Help(subcmd string, opts any) {

View file

@ -10,6 +10,9 @@ import (
"strings"
)
// but this stinks:
const lcmd = "last"
const (
// NOTE: the -r flag:
LS_CMD = `ls -r [0-9]*.md`
@ -44,7 +47,7 @@ func last() (string, error) {
func Last() {
s, err := last()
if err != nil {
log.Fatalf("um last: %v", err)
log.Fatalf("um %s: %v", lcmd, err)
}
// send to stdout, not stderr as is default for log.Print:
fmt.Println(s)

View file

@ -10,6 +10,8 @@ import (
"github.com/brtholomy/um/go/flags"
)
const cmd = "next"
type options struct {
Descriptor flags.Arg
Tags flags.Arg
@ -33,10 +35,10 @@ func parseArgs(args []string) options {
case i == 1 && !flags.HasDashPrefix(arg):
opts.Tags.Val = arg
case arg == opts.Help.Long || arg == opts.Help.Short:
flags.Help("next", opts)
flags.Help(cmd, opts)
default:
log.Printf("um next: invalid argument: %s", arg)
flags.Help("next", opts)
flags.HelpInvalidArg(cmd, arg)
flags.Help(cmd, opts)
}
}
return opts
@ -107,13 +109,13 @@ func Next(args []string) {
opts := parseArgs(args)
l, err := last()
if err != nil {
log.Fatalf("um next: %v", err)
log.Fatalf("um %s: %v", cmd, err)
}
filename, err := next(l, opts.Descriptor.Val)
if err != nil {
log.Fatalf("um next: %v", err)
log.Fatalf("um %s: %v", cmd, err)
}
if err := emacsNext(filename, opts.Tags.Val); err != nil {
log.Fatalf("um next: %v", err)
log.Fatalf("um %s: %v", cmd, err)
}
}

View file

@ -11,6 +11,8 @@ import (
"github.com/brtholomy/um/go/pipe"
)
const cmd = "sort"
type options struct {
Source flags.Flag[string]
Key flags.Flag[string]
@ -39,15 +41,15 @@ func parseArgs(args []string) options {
case arg == opts.Write.Long || arg == opts.Write.Short:
opts.Write.Val = true
case arg == opts.Help.Long || arg == opts.Help.Short:
flags.Help("sort", opts)
flags.Help(cmd, opts)
default:
log.Printf("um sort: invalid argument: %s", arg)
flags.Help("sort", opts)
flags.Help(cmd, opts)
}
}
if !opts.Key.IsSet() {
log.Printf("um sort: %s is required", opts.Key.Long)
flags.Help("sort", opts)
flags.HelpRequired(cmd, opts.Key.Long)
flags.Help(cmd, opts)
}
return opts
}

View file

@ -6,6 +6,8 @@ import (
"github.com/brtholomy/um/go/flags"
)
const cmd = "tag"
type options struct {
Query flags.Arg
Date flags.Flag[string]
@ -38,10 +40,10 @@ func parseArgs(args []string) options {
case arg == opts.Date.Long || arg == opts.Date.Short:
i, opts.Date.Val = flags.ValidateIncrementFetchOrExit(args, i)
case arg == opts.Help.Long || arg == opts.Help.Short:
flags.Help("tag", opts)
flags.Help(cmd, opts)
default:
log.Printf("um tag: invalid argument: %s", arg)
flags.Help("tag", opts)
flags.HelpInvalidArg(cmd, arg)
flags.Help(cmd, opts)
}
}
return opts