Documentation
¶
Index ¶
- Variables
- func AddFlag(flags *pflag.FlagSet, typ reflect.Type, val reflect.Value, name string, ...) error
- type BytesHexFlag
- 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() string
- type CommandKnownRoutes
- type CommandRoute
- type Help
- type InitDefault
Constants ¶
This section is empty.
Variables ¶
var UnrecognizedErr = errors.New("command was not recognized")
Functions ¶
Types ¶
type BytesHexFlag ¶ added in v0.0.2
type BytesHexFlag []byte
BytesHex exposes bytes as a flag, hex-encoded, optional whitespace padding, case insensitive, and optional 0x prefix.
func (*BytesHexFlag) Set ¶ added in v0.0.2
func (f *BytesHexFlag) Set(value string) error
func (BytesHexFlag) String ¶ added in v0.0.2
func (f BytesHexFlag) String() string
func (*BytesHexFlag) Type ¶ added in v0.0.2
func (f *BytesHexFlag) Type() string
type CommandDescription ¶
type CommandDescription struct {
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
// Help Information as provided by the Help interface
Help
}
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:"."` (recurse depth-first). Embedded fields are handled as regular fields unless explicitly squashed. It skips the field explicitly if it's tagged with `ask:"-"` 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() string
Usage prints the help information and the usage of all flags.
type CommandKnownRoutes ¶ added in v0.0.2
type CommandKnownRoutes interface {
// Routes lists the sub-commands that can be asked from Get.
Routes() []string
}
CommandKnownRoutes may be implemented by a CommandRoute to declare which routes are accessible, useful for e.g. help messages to give more information for each of the subcommands.
type CommandRoute ¶
type CommandRoute interface {
// Cmd gets a sub-command, which can be a Command or CommandRoute
// The command that is returned will be loaded with `Load` before it runs or its subcommand is retrieved.
// Return nil if the command route should be ignored, e.g. if this route is also a regular command with arguments.
Cmd(route string) (cmd interface{}, err error)
}
type Help ¶
type Help interface {
// Help explains how a command is used.
Help() string
}
Optionally specify how to get help information (usage of flags is added to this when called through CommandDescription.Usage() )
type InitDefault ¶ added in v0.0.2
type InitDefault interface {
// Default the flags of a command.
Default()
}
InitDefault can be implemented by a command to not rely on the parent command initializing the command correctly, and instead move the responsibility to the command itself. The default is initialized during Load, and a command may embed multiple sub-structures that implement Default.