cmd pkg for Subcommand type, use everywhere

This commit is contained in:
bth 2026-01-20 13:30:53 -08:00
commit c434005d39
7 changed files with 52 additions and 45 deletions

11
cmd/cmd.go Normal file
View file

@ -0,0 +1,11 @@
package cmd
type Subcommand string
const (
Tag Subcommand = "tag"
Next Subcommand = "next"
Last Subcommand = "last"
Sort Subcommand = "sort"
Help Subcommand = "help"
)

View file

@ -7,6 +7,8 @@ import (
"reflect" "reflect"
"strings" "strings"
"text/tabwriter" "text/tabwriter"
"github.com/brtholomy/um/go/cmd"
) )
type Arg struct { type Arg struct {
@ -74,17 +76,17 @@ func ValidateIncrementFetchOrExit(args []string, i int) (int, string) {
return i + 1, args[i+1] return i + 1, args[i+1]
} }
func HelpRequired(cmd string, long string) { func HelpRequired(sub cmd.Subcommand, long string) {
log.Printf("um %s: %s is required", cmd, long) log.Printf("um %s: %s is required", sub, long)
} }
func HelpInvalidArg(cmd string, arg string) { func HelpInvalidArg(sub cmd.Subcommand, arg string) {
log.Printf("um %s: invalid argument: %s", cmd, arg) log.Printf("um %s: invalid argument: %s", sub, arg)
} }
// 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) // and os.Exit(0)
func Help(subcmd string, opts any) { func Help(sub cmd.Subcommand, opts any) {
v := reflect.ValueOf(opts) v := reflect.ValueOf(opts)
t := v.Type() t := v.Type()
@ -110,7 +112,7 @@ func Help(subcmd string, opts any) {
) )
} }
} }
log.Printf("um %s%s\n", subcmd, positional) log.Printf("um %s%s\n", sub, positional)
w.Flush() w.Flush()
os.Exit(0) os.Exit(0)
} }

View file

@ -8,10 +8,9 @@ import (
"os/exec" "os/exec"
"regexp" "regexp"
"strings" "strings"
)
// but this stinks: cmdpkg "github.com/brtholomy/um/go/cmd"
const lcmd = "last" )
const ( const (
// NOTE: the -r flag: // NOTE: the -r flag:
@ -47,7 +46,7 @@ func last() (string, error) {
func Last() { func Last() {
s, err := last() s, err := last()
if err != nil { if err != nil {
log.Fatalf("um %s: %v", lcmd, err) log.Fatalf("um %s: %v", cmdpkg.Last, err)
} }
// send to stdout, not stderr as is default for log.Print: // send to stdout, not stderr as is default for log.Print:
fmt.Println(s) fmt.Println(s)

View file

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

View file

@ -7,11 +7,12 @@ import (
"slices" "slices"
"strings" "strings"
cmdpkg "github.com/brtholomy/um/go/cmd"
"github.com/brtholomy/um/go/flags" "github.com/brtholomy/um/go/flags"
"github.com/brtholomy/um/go/pipe" "github.com/brtholomy/um/go/pipe"
) )
const cmd = "sort" const cmd = cmdpkg.Sort
type options struct { type options struct {
Source flags.Flag[string] Source flags.Flag[string]
@ -43,7 +44,7 @@ func parseArgs(args []string) options {
case arg == opts.Help.Long || arg == opts.Help.Short: case arg == opts.Help.Long || arg == opts.Help.Short:
flags.Help(cmd, opts) flags.Help(cmd, opts)
default: default:
log.Printf("um sort: invalid argument: %s", arg) flags.HelpInvalidArg(cmd, arg)
flags.Help(cmd, opts) flags.Help(cmd, opts)
} }
} }

View file

@ -3,10 +3,12 @@ package tag
import ( import (
"log" "log"
cmdpkg "github.com/brtholomy/um/go/cmd"
"github.com/brtholomy/um/go/flags" "github.com/brtholomy/um/go/flags"
) )
const cmd = "tag" // just so that copied calls into flags.Help don't have to be adjusted between files:
const cmd = cmdpkg.Tag
type options struct { type options struct {
Query flags.Arg Query flags.Arg

41
um.go
View file

@ -5,22 +5,13 @@ import (
"log" "log"
"os" "os"
nextpkg "github.com/brtholomy/um/go/next" "github.com/brtholomy/um/go/cmd"
sortpkg "github.com/brtholomy/um/go/sort" "github.com/brtholomy/um/go/next"
tagpkg "github.com/brtholomy/um/go/tag" "github.com/brtholomy/um/go/sort"
"github.com/brtholomy/um/go/tag"
) )
type Subcommand string var helpShort string = fmt.Sprintf("um [%s | %s | %s | %s | %s]", cmd.Tag, cmd.Next, cmd.Last, cmd.Sort, cmd.Help)
const (
tag Subcommand = "tag"
next Subcommand = "next"
last Subcommand = "last"
sort Subcommand = "sort"
help Subcommand = "help"
)
var helpShort string = fmt.Sprintf("um [%s | %s | %s | %s | %s]", tag, next, last, sort, help)
var helpLong string = fmt.Sprintf(`%s var helpLong string = fmt.Sprintf(`%s
An (U)ltralight database for (M)arkdown composition. An (U)ltralight database for (M)arkdown composition.
@ -40,19 +31,19 @@ func main() {
log.Fatalln(helpShort) log.Fatalln(helpShort)
} }
cmd := Subcommand(os.Args[1]) arg := cmd.Subcommand(os.Args[1])
args := os.Args[2:] args := os.Args[2:]
switch cmd { switch arg {
case next: case cmd.Next:
nextpkg.Next(args) next.Next(args)
case last: case cmd.Last:
nextpkg.Last() next.Last()
case tag: case cmd.Tag:
tagpkg.Tag(args) tag.Tag(args)
case sort: case cmd.Sort:
sortpkg.Sort(args) sort.Sort(args)
case help: case cmd.Help:
log.Fatalln(helpLong) log.Fatalln(helpLong)
default: default:
log.Println("um: command not found") log.Println("um: command not found")