Documentation
¶
Index ¶
- Variables
- type Beforer
- type CLI
- type Command
- func (cmd *Command) AddCommand(subCmd *Command) *Command
- func (cmd *Command) Description(description string) *Command
- func (cmd *Command) Help(help string) *Command
- func (cmd *Command) HelpString() string
- func (cmd *Command) Parse() ParseResult
- func (cmd *Command) ParseArgs(args []string) ParseResult
- func (cmd *Command) WriteHelp(w io.Writer)
- type ContextRunner
- type ExitCoder
- type LookupEnvFunc
- type ParseResult
- type Runner
- type Setter
- type SetterFunc
- type Setuper
Constants ¶
This section is empty.
Variables ¶
var Defaults = CLI{ HelpWriter: os.Stderr, ErrWriter: os.Stderr, LookupEnv: osLookupEnv, Setter: nil, }
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 CustomTimeSetter struct {
// value *time.Time
// }
// func (ts *CustomTimeSetter) 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.CLI{
// ErrWriter: cli.Defaults.ErrWriter,
// LookupEnv: cli.Defaults.LookupEnv,
// Setter: func(i interface{}) cli.Setter {
// switch v := i.(type) {
// case *time.Time:
// return &CustomTimeSetter{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 ¶
Build is like New, but it returns any errors instead of calling panic, at the expense of being harder to chain.
func New ¶
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.
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) Description ¶ added in v0.6.0
func (*Command) HelpString ¶
func (*Command) Parse ¶
func (cmd *Command) Parse() ParseResult
Parse is a convenience method for calling ParseArgs(os.Args)
func (*Command) ParseArgs ¶
func (cmd *Command) ParseArgs(args []string) ParseResult
ParseArgs parses using the passed-in args slice and OS-provided environment variables 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 recursing into any subcommand parsing.
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