35 lines
989 B
Go
35 lines
989 B
Go
package cli
|
|
|
|
import (
|
|
state "v/state"
|
|
)
|
|
|
|
type Namespace struct {
|
|
Label string
|
|
Commands map[string]Command
|
|
OrderedCommands []string
|
|
}
|
|
|
|
// Command definition for CLI subcommands.
|
|
type Command struct {
|
|
Label string
|
|
Handler func([]string, Flags, state.State) error
|
|
Usage string
|
|
Description string
|
|
}
|
|
|
|
// Registers a command.
|
|
// This specifies a label that is used to route the user input to
|
|
// the right command, a handler that is called when the label is used,
|
|
// and usage/description details that get included in autogenerated help messaging.
|
|
func (n *Namespace) AddCommand(label string, handler func([]string, Flags, state.State) error, usage string, description string) *Namespace {
|
|
if n.Commands == nil {
|
|
n.Commands = map[string]Command{}
|
|
n.OrderedCommands = []string{}
|
|
}
|
|
|
|
n.OrderedCommands = append(n.OrderedCommands, label)
|
|
n.Commands[label] = Command{Label: label, Handler: handler, Usage: usage, Description: description}
|
|
|
|
return n
|
|
}
|