cmd pkg for Subcommand type, use everywhere
This commit is contained in:
parent
77e3e23446
commit
c434005d39
7 changed files with 52 additions and 45 deletions
11
cmd/cmd.go
Normal file
11
cmd/cmd.go
Normal 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"
|
||||
)
|
||||
|
|
@ -7,6 +7,8 @@ import (
|
|||
"reflect"
|
||||
"strings"
|
||||
"text/tabwriter"
|
||||
|
||||
"github.com/brtholomy/um/go/cmd"
|
||||
)
|
||||
|
||||
type Arg struct {
|
||||
|
|
@ -74,17 +76,17 @@ 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 HelpRequired(sub cmd.Subcommand, long string) {
|
||||
log.Printf("um %s: %s is required", sub, long)
|
||||
}
|
||||
|
||||
func HelpInvalidArg(cmd string, arg string) {
|
||||
log.Printf("um %s: invalid argument: %s", cmd, arg)
|
||||
func HelpInvalidArg(sub cmd.Subcommand, arg string) {
|
||||
log.Printf("um %s: invalid argument: %s", sub, arg)
|
||||
}
|
||||
|
||||
// print out help string by reflecting over fields of provided opts struct
|
||||
// and os.Exit(0)
|
||||
func Help(subcmd string, opts any) {
|
||||
func Help(sub cmd.Subcommand, opts any) {
|
||||
v := reflect.ValueOf(opts)
|
||||
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()
|
||||
os.Exit(0)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -8,10 +8,9 @@ import (
|
|||
"os/exec"
|
||||
"regexp"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// but this stinks:
|
||||
const lcmd = "last"
|
||||
cmdpkg "github.com/brtholomy/um/go/cmd"
|
||||
)
|
||||
|
||||
const (
|
||||
// NOTE: the -r flag:
|
||||
|
|
@ -47,7 +46,7 @@ func last() (string, error) {
|
|||
func Last() {
|
||||
s, err := last()
|
||||
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:
|
||||
fmt.Println(s)
|
||||
|
|
|
|||
15
next/next.go
15
next/next.go
|
|
@ -7,10 +7,11 @@ import (
|
|||
"os/exec"
|
||||
"strconv"
|
||||
|
||||
cmdpkg "github.com/brtholomy/um/go/cmd"
|
||||
"github.com/brtholomy/um/go/flags"
|
||||
)
|
||||
|
||||
const cmd = "next"
|
||||
const this = cmdpkg.Next
|
||||
|
||||
type options struct {
|
||||
Descriptor flags.Arg
|
||||
|
|
@ -35,10 +36,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(cmd, opts)
|
||||
flags.Help(this, opts)
|
||||
default:
|
||||
flags.HelpInvalidArg(cmd, arg)
|
||||
flags.Help(cmd, opts)
|
||||
flags.HelpInvalidArg(this, arg)
|
||||
flags.Help(this, opts)
|
||||
}
|
||||
}
|
||||
return opts
|
||||
|
|
@ -109,13 +110,13 @@ func Next(args []string) {
|
|||
opts := parseArgs(args)
|
||||
l, err := last()
|
||||
if err != nil {
|
||||
log.Fatalf("um %s: %v", cmd, err)
|
||||
log.Fatalf("um %s: %v", this, err)
|
||||
}
|
||||
filename, err := next(l, opts.Descriptor.Val)
|
||||
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 {
|
||||
log.Fatalf("um %s: %v", cmd, err)
|
||||
log.Fatalf("um %s: %v", this, err)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -7,11 +7,12 @@ import (
|
|||
"slices"
|
||||
"strings"
|
||||
|
||||
cmdpkg "github.com/brtholomy/um/go/cmd"
|
||||
"github.com/brtholomy/um/go/flags"
|
||||
"github.com/brtholomy/um/go/pipe"
|
||||
)
|
||||
|
||||
const cmd = "sort"
|
||||
const cmd = cmdpkg.Sort
|
||||
|
||||
type options struct {
|
||||
Source flags.Flag[string]
|
||||
|
|
@ -43,7 +44,7 @@ func parseArgs(args []string) options {
|
|||
case arg == opts.Help.Long || arg == opts.Help.Short:
|
||||
flags.Help(cmd, opts)
|
||||
default:
|
||||
log.Printf("um sort: invalid argument: %s", arg)
|
||||
flags.HelpInvalidArg(cmd, arg)
|
||||
flags.Help(cmd, opts)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,10 +3,12 @@ package tag
|
|||
import (
|
||||
"log"
|
||||
|
||||
cmdpkg "github.com/brtholomy/um/go/cmd"
|
||||
"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 {
|
||||
Query flags.Arg
|
||||
|
|
|
|||
41
um.go
41
um.go
|
|
@ -5,22 +5,13 @@ import (
|
|||
"log"
|
||||
"os"
|
||||
|
||||
nextpkg "github.com/brtholomy/um/go/next"
|
||||
sortpkg "github.com/brtholomy/um/go/sort"
|
||||
tagpkg "github.com/brtholomy/um/go/tag"
|
||||
"github.com/brtholomy/um/go/cmd"
|
||||
"github.com/brtholomy/um/go/next"
|
||||
"github.com/brtholomy/um/go/sort"
|
||||
"github.com/brtholomy/um/go/tag"
|
||||
)
|
||||
|
||||
type Subcommand string
|
||||
|
||||
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 helpShort string = fmt.Sprintf("um [%s | %s | %s | %s | %s]", cmd.Tag, cmd.Next, cmd.Last, cmd.Sort, cmd.Help)
|
||||
var helpLong string = fmt.Sprintf(`%s
|
||||
|
||||
An (U)ltralight database for (M)arkdown composition.
|
||||
|
|
@ -40,19 +31,19 @@ func main() {
|
|||
log.Fatalln(helpShort)
|
||||
}
|
||||
|
||||
cmd := Subcommand(os.Args[1])
|
||||
arg := cmd.Subcommand(os.Args[1])
|
||||
args := os.Args[2:]
|
||||
|
||||
switch cmd {
|
||||
case next:
|
||||
nextpkg.Next(args)
|
||||
case last:
|
||||
nextpkg.Last()
|
||||
case tag:
|
||||
tagpkg.Tag(args)
|
||||
case sort:
|
||||
sortpkg.Sort(args)
|
||||
case help:
|
||||
switch arg {
|
||||
case cmd.Next:
|
||||
next.Next(args)
|
||||
case cmd.Last:
|
||||
next.Last()
|
||||
case cmd.Tag:
|
||||
tag.Tag(args)
|
||||
case cmd.Sort:
|
||||
sort.Sort(args)
|
||||
case cmd.Help:
|
||||
log.Fatalln(helpLong)
|
||||
default:
|
||||
log.Println("um: command not found")
|
||||
|
|
|
|||
Loading…
Reference in a new issue