Documentation ¶
Index ¶
- Variables
- type Beforer
- type CLI
- type Command
- func (cmd *Command) AddCommand(subCmd *Command) *Command
- func (cmd *Command) Apply(parent *Command)
- func (cmd *Command) HelpString() string
- func (cmd *Command) Parse() ParseResult
- func (cmd *Command) ParseArgs(args []string) ParseResult
- func (cmd *Command) SetDescription(description string) *Command
- func (cmd *Command) SetHelp(help string) *Command
- func (cmd *Command) WriteHelp(w io.Writer)
- type CommandOption
- type ContextRunner
- type ExitCoder
- type LookupEnvFunc
- type ParseResult
- type Runner
- type Setter
- type SetterFunc
- type Setuper
- type UsageErrorWrapper
Constants ¶
This section is empty.
Variables ¶
var ErrHelp = fmt.Errorf("cli: help requested")
Functions ¶
This section is empty.
Types ¶
type CLI ¶ added in v0.7.0
type CLI struct { // HelpWriter is used to print help output when calling ParseResult.Run // (and other similar methods). HelpWriter io.Writer // ErrWriter is used to print errors when calling ParseResult.Run (and // other similar methods). ErrWriter io.Writer // LookupEnv is called during parsing for any fields which define an env // var key, but are not set by argument. LookupEnv LookupEnvFunc // Setter can be used to define custom setters for arbitrary field types, // or to override the default field setters. // // Here is an example which uses a custom layout for parsing any time.Time // fields: // // type customTime time.Time // func (t *customTime) Set(s string) error { // parsed, err := time.Parse("2006-01-02 15:04", s) // if err != nil { // return err // } // *ts.value = parsed // return nil // } // cli := cli.NewCLI() // cli.Setter = func(i interface{}) cli.Setter { // switch v := i.(type) { // case *time.Time: // return (*customTime)(v) // default: // // return nil to fall back on default behavior // return nil // } // } Setter SetterFunc }
CLI defines functionality which is global to all commands which it constructs. The top-level New and Build methods use a CLI with good defaults for most cases, but custom CLI structs can be used to modify behavior.
type Command ¶
type Command struct {
// contains filtered or unexported fields
}
func Build ¶
func Build(name string, config interface{}, opts ...CommandOption) (*Command, error)
Build is like New, but it returns any errors instead of calling panic, at the expense of being harder to chain.
func New ¶
func New(name string, config interface{}, opts ...CommandOption) *Command
New creates a new Command with the provided name and config. The config must be a pointer to a configuration struct. Default values can be specified by simply setting them on the config struct.
Command options (e.g. help text and subcommands) can be passed as additonal CommandOption arguments, or set using chained method calls. Note that *Command implements CommandOption, so subcommands can be registered by simply passing them as arugments.
New returns an Command pointer for further method chaining. If an error is encountered while building the options, such as a struct field having an unsupported type, New will panic. If you would like to have errors returned for handling, use Build instead.
func (*Command) AddCommand ¶
AddCommand registers another Command instance as a subcommand of this Command instance.
func (*Command) HelpString ¶
func (*Command) Parse ¶
func (cmd *Command) Parse() ParseResult
Parse is a convenience method for calling ParseArgs(os.Args[1:])
func (*Command) ParseArgs ¶
func (cmd *Command) ParseArgs(args []string) ParseResult
ParseArgs parses the passed-in args slice, along with environment variables, into the config fields, and returns a ParseResult which can be used for further method chaining.
If there are args remaining after parsing this Command's fields, subcommands will be recursively parsed until a concrete result is returned
If a Before method is implemented on the config, this method will call it before calling Run or recursing into any subcommand parsing.
func (*Command) SetDescription ¶ added in v0.8.0
type CommandOption ¶ added in v0.8.0
type CommandOption interface {
Apply(cmd *Command)
}
func WithDescription ¶ added in v0.8.0
func WithDescription(description string) CommandOption
func WithHelp ¶ added in v0.8.0
func WithHelp(help string) CommandOption
type ContextRunner ¶ added in v0.7.0
type LookupEnvFunc ¶ added in v0.6.0
type ParseResult ¶ added in v0.7.0
ParseResult contains information about the results of command argument parsing.
func (ParseResult) Run ¶ added in v0.7.0
func (r ParseResult) Run() error
Run calls the Run method of the Command config for the parsed command or, if an error occurred during parsing, prints the help text and returns that error instead. If help was requested, the error will flag.ErrHelp. If the underlying command Run method accepts a context, context.Background() will be passed.
func (ParseResult) RunFatal ¶ added in v0.7.0
func (r ParseResult) RunFatal()
RunFatal is like Run, except it automatically handles printing out any errors returned by the Run method of the underlying Command config, and exits with an appropriate status code.
If no error occurs, the exit code will be 0. If an error is returned and it implements the ExitCoder interface, the result of ExitCode() will be used as the exit code. If an error is returned that does not implement ExitCoder, the exit code will be 1.
func (ParseResult) RunFatalWithContext ¶ added in v0.7.0
func (r ParseResult) RunFatalWithContext(ctx context.Context)
RunFatalWithContext is like RunFatal, but it accepts an explicit context which will be passed to the command's Run method if it accepts one.
func (ParseResult) RunFatalWithSigCancel ¶ added in v0.7.0
func (r ParseResult) RunFatalWithSigCancel()
RunFatalWithSigCancel is like RunFatal, but it automatically registers a signal handler for SIGINT and SIGTERM that will cancel the context that is passed to the command's Run method, if it accepts one.
func (ParseResult) RunWithContext ¶ added in v0.7.0
func (r ParseResult) RunWithContext(ctx context.Context) error
RunWithContext is like Run, but it accepts an explicit context which will be passed to the command's Run method, if it accepts one.
func (ParseResult) RunWithSigCancel ¶ added in v0.7.0
func (r ParseResult) RunWithSigCancel() error
RunWithSigCancel is like Run, but it automatically registers a signal handler for SIGINT and SIGTERM that will cancel the context that is passed to the command's Run method, if it accepts one.
type SetterFunc ¶ added in v0.6.2
type SetterFunc func(interface{}) Setter
type UsageErrorWrapper ¶ added in v0.9.0
type UsageErrorWrapper struct {
Err error
}
UsageErrorWrapper wraps another error to indicate that the error was due to incorrect usage. When this error is handled, help text should be printed in addition to the error message.
func UsageError ¶ added in v0.9.0
func UsageError(err error) UsageErrorWrapper
UsageError wraps the given error as a UsageErrorWrapper.
func UsageErrorf ¶ added in v0.9.0
func UsageErrorf(format string, v ...interface{}) UsageErrorWrapper
UsageErrorf is a convenience method for wrapping the result of fmt.Errorf as a UsageErrorWrapper.
func (UsageErrorWrapper) Error ¶ added in v0.9.0
func (w UsageErrorWrapper) Error() string
func (UsageErrorWrapper) Unwrap ¶ added in v0.9.0
func (w UsageErrorWrapper) Unwrap() error