43 lines
730 B
Go
43 lines
730 B
Go
|
|
package wand
|
||
|
|
|
||
|
|
import "os"
|
||
|
|
|
||
|
|
type Cmd struct {
|
||
|
|
name string
|
||
|
|
impl any
|
||
|
|
subcommands []Cmd
|
||
|
|
isDefault bool
|
||
|
|
minPositional int
|
||
|
|
maxPositional int
|
||
|
|
shortForms []string
|
||
|
|
description string
|
||
|
|
isHelp bool
|
||
|
|
helpRequested bool
|
||
|
|
}
|
||
|
|
|
||
|
|
func Command(name string, impl any, subcmds ...Cmd) Cmd {
|
||
|
|
return command(name, impl, subcmds...)
|
||
|
|
}
|
||
|
|
|
||
|
|
func Default(cmd Cmd) Cmd {
|
||
|
|
cmd.isDefault = true
|
||
|
|
return cmd
|
||
|
|
}
|
||
|
|
|
||
|
|
// io doesn't count
|
||
|
|
func Args(cmd Cmd, min, max int) Cmd {
|
||
|
|
cmd.minPositional = min
|
||
|
|
cmd.maxPositional = max
|
||
|
|
return cmd
|
||
|
|
}
|
||
|
|
|
||
|
|
func ShortForm(cmd Cmd, f ...string) Cmd {
|
||
|
|
cmd.shortForms = f
|
||
|
|
return cmd
|
||
|
|
}
|
||
|
|
|
||
|
|
func Exec(impl any) {
|
||
|
|
cmd := wrap(impl)
|
||
|
|
exec(os.Stdout, os.Stderr, os.Exit, cmd, os.Environ(), os.Args)
|
||
|
|
}
|