command: expose command with direct handling
All checks were successful
Test / Create distribution (push) Successful in 26s
Test / Run NixOS test (push) Successful in 3m28s

This exposes flag set on commands with direct handling.

Signed-off-by: Ophestra <cat@gensokyo.uk>
This commit is contained in:
Ophestra 2025-02-23 00:24:03 +09:00
parent dfa3217037
commit 54308f79d2
Signed by: cat
SSH Key Fingerprint: SHA256:gQ67O0enBZ7UdZypgtspB2FDM1g3GVw8nX0XSdcFw8Q
2 changed files with 15 additions and 3 deletions

View File

@ -31,6 +31,11 @@ func newNode(output io.Writer, logf LogFunc, name, usage string) *node {
}
func (n *node) Command(name, usage string, f HandlerFunc) Node {
n.NewCommand(name, usage, f)
return n
}
func (n *node) NewCommand(name, usage string, f HandlerFunc) Flag[Node] {
if f == nil {
panic("invalid handler")
}
@ -43,7 +48,7 @@ func (n *node) Command(name, usage string, f HandlerFunc) Node {
if !n.adopt(s) {
panic("attempted to initialise subcommand with non-unique name")
}
return n
return s
}
func (n *node) New(name, usage string) Node {

View File

@ -19,6 +19,11 @@ type (
Define(b *strings.Builder, set *flag.FlagSet, p any, name, usage string)
}
Flag[T any] interface {
// Flag defines a generic flag type in Node's flag set.
Flag(p any, name string, value FlagDefiner, usage string) T
}
Command interface {
Parse(arguments []string) error
baseNode[Command]
@ -28,10 +33,12 @@ type (
baseNode[T any] interface {
// Command appends a subcommand with direct command handling.
Command(name, usage string, f HandlerFunc) T
// Flag defines a generic flag type in Node's flag set.
Flag(p any, name string, value FlagDefiner, usage string) T
// New returns a new subcommand tree.
New(name, usage string) (sub Node)
// NewCommand returns a new subcommand with direct command handling.
NewCommand(name, usage string, f HandlerFunc) (sub Flag[Node])
Flag[T]
}
)