Documentation ¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ( ErrUnparsed = errors.New("command tree is unparsed, can't run") ErrInvalidArguments = errors.New("invalid arguments") )
Functions ¶
This section is empty.
Types ¶
type ArgsValidator ¶
ArgsValidator is the function signature for representing an argument validator for Command's.
func CombineValidator ¶
func CombineValidator(validators ...ArgsValidator) ArgsValidator
CombineValidator is used for combining multiple ArgsValidator's into one. It accepts multiple ArgsValidator functions and returns a single ArgsValidator, that checks all conditions in order they are passed.
func ExactArgs ¶
func ExactArgs(n int) ArgsValidator
ExactArgs returns an error unless there are exactly N args.
func MaxArgs ¶
func MaxArgs(n int) ArgsValidator
MaxArgs returns an error if there are more than N args.
func MinArgs ¶
func MinArgs(n int) ArgsValidator
MinArgs returns an error if there are not at least N args.
func OnlyValidArgs ¶
func OnlyValidArgs(validArgs []string) ArgsValidator
OnlyValidArgs returns an error if there are any args that are not contained in the validArgs slice.
func RangeArgs ¶
func RangeArgs(min, max int) ArgsValidator
RangeArgs returns an error if the number of args is not in the expected range.
type Command ¶
type Command struct { // Usage is a one liner usage message. First word of Usage is used for the Command's name. // Required for sub-commands. // Recommend syntax is: // // cmd [flags] subcmd [flags] <required> [<optional> ...] Usage string // Aliases is a slice of alternate names that can be used for a sub command instead of the first word of usage. // Optional. Aliases []string // ShortHelp is a short description that is displayed in the global help -h output. // Optional, but recommended. ShortHelp string // LongHelp is a longer description that is displayed int the '<this-command> -h' output. // If not provided ShortHelp will be used in its place. Optional. LongHelp string // Subcommands is a slice of commands supported by Command. // Subcommands are optional and only needed if you application needs multiple commands. Subcommands []*Command // TODO UsageFunc func(c *Command) string // FlagSet for this command. Optional, but if none is provided, // an empty FlagSet will be defined to ensure -h works as expected. FlagSet *flag.FlagSet // ArgsValidator provides a validation function for arguments. There are multiple builtin validators as the // XArgs functions in this package. // Any error returned by ArgsValidator gets wrapped by an ErrInvalidArguments then is returned by Run or ParseAndRun. // When ArgsValidator returns an error the commands usage will be printed as well as the body of the error message. ArgsValidator ArgsValidator // Exec is the function that does the actual work, most Command's will implement this, unless they are just a // namespace for Subcommands. // The error returned by Exec will be bubble up and be returned by Run and ParseAndRun. // If flag.ErrHelp or ErrInvalidArguments is returned the commands usage will be printed to the output. Exec func(ctx context.Context, args []string) error // contains filtered or unexported fields }
func (*Command) ParseAndRun ¶
ParseAndRun is a helper function to execute parse and run in a single invocation.
type NoExecError ¶
type NoExecError struct {
Command *Command
}
func (NoExecError) Error ¶
func (e NoExecError) Error() string