Documentation
¶
Index ¶
- Variables
- type Command
- type CommandDescription
- func (descr *CommandDescription) Execute(ctx context.Context, args ...string) (final *CommandDescription, isHelp bool, err error)
- func (descr *CommandDescription) Load(val interface{}) error
- func (descr *CommandDescription) LoadField(f reflect.StructField, val reflect.Value) (requiredArg, optionalArg string, err error)
- func (descr *CommandDescription) LoadReflect(val reflect.Value) error
- func (descr *CommandDescription) Usage(name string) string
- type CommandRoute
- type Help
Constants ¶
This section is empty.
Variables ¶
var InvalidCmdTypeErr = errors.New("command type is not supported")
var NotRecognizedErr = errors.New("command was not recognized")
Functions ¶
This section is empty.
Types ¶
type CommandDescription ¶
type CommandDescription struct {
Help string
FlagsSet *pflag.FlagSet
// Flags that can be passed as positional required args
RequiredArgs []string
// Flags that can be passed as positional optional args
OptionalArgs []string
// Command to run, may be nil if nothing has to run
Command
// Sub-command routing, can create commands (or other sub-commands) to access, may be nil if no sub-commands
CommandRoute
}
An interface{} can be loaded as a command-description to execute it. See Load()
func Load ¶
func Load(val interface{}) (*CommandDescription, error)
Load takes a structure instance that defines a command through its type, and the default values by determining them from the actual type.
func LoadReflect ¶
func LoadReflect(val reflect.Value) (*CommandDescription, error)
LoadReflect is the same as Load, but directly using reflection to handle the value.
func (*CommandDescription) Execute ¶
func (descr *CommandDescription) Execute(ctx context.Context, args ...string) (final *CommandDescription, isHelp bool, err error)
Runs the command, with given context and arguments. The final sub-command that actually runs is returned, and may be nil in case of an error. The "isHelp" will be true if help information was requested for the command (through `help`, `--help` or `-h`) To add inputs/outputs such as STDOUT to a command, they can be added as field in the command struct definition, and the command can pass them on to sub-commands. Similarly logging and other misc. data can be passed around. The execute parameters are kept minimal.
func (*CommandDescription) Load ¶
func (descr *CommandDescription) Load(val interface{}) error
Load adds more flags/args/meta to the command description. It recursively goes into the field if it's tagged with `ask:"."`, or if it's an embedded field. (recurse depth-first) It skips the field explicitly if it's tagged with `ask:"-"` (used to ignore embedded fields) Multiple target values can be loaded if they do not conflict, the first Command and CommandRoute found will be used. The flags will be set over all loaded values.
func (*CommandDescription) LoadField ¶
func (descr *CommandDescription) LoadField(f reflect.StructField, val reflect.Value) (requiredArg, optionalArg string, err error)
Check the struct field, and add flag for it if asked for
func (*CommandDescription) LoadReflect ¶
func (descr *CommandDescription) LoadReflect(val reflect.Value) error
LoadReflect is the same as Load, but directly using reflection to handle the value.
func (*CommandDescription) Usage ¶
func (descr *CommandDescription) Usage(name string) string
Usage prints the help information and the usage of all flags.
type CommandRoute ¶
type CommandRoute interface {
// Get a subcommand, which can be a Command or CommandRoute
// The remaining arguments are passed to the subcommand on execution
// The command that is returned will be loaded with `Load` before it runs
Get(ctx context.Context, args ...string) (cmd interface{}, remaining []string, err error)
}