Documentation
¶
Index ¶
- Variables
- type Beforer
- 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() ParsedCommand
- func (cmd *Command) ParseArgs(args []string) ParsedCommand
- func (cmd *Command) WriteHelp(w io.Writer)
- type Context
- type LookupEnvFunc
- type ParsedCommand
- type Runner
- type Setter
- type SetterFunc
- type Setuper
Constants ¶
This section is empty.
Variables ¶
var DefaultContext = Context{ ErrWriter: os.Stderr, LookupEnv: func(key string) (string, bool, error) { val, ok := os.LookupEnv(key) return val, ok, nil }, Setter: nil, }
var ErrHelp = fmt.Errorf("cli: help requested")
Functions ¶
This section is empty.
Types ¶
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() ParsedCommand
Parse is a convenience method for calling ParseArgs(os.Args)
func (*Command) ParseArgs ¶
func (cmd *Command) ParseArgs(args []string) ParsedCommand
ParseArgs parses using the passed-in args slice and OS-provided environment variables and returns a ParsedCommand instance which can be used for further method chaining.
If there are args remaining after parsing this Command' fields, subcommands will be parsed recursively. The returned ParsedCommand.Runner represents the command which was specified (if it has a Run method). If a Before method is implemented on the Command' config, this method will call it before recursing into any subcommand parsing.
type Context ¶ added in v0.6.0
type Context struct {
ErrWriter io.Writer
LookupEnv LookupEnvFunc
Setter SetterFunc
}
func (Context) Build ¶ added in v0.6.0
Build is like New, but it returns any errors instead of calling panic, at the expense of being harder to chain.
func (Context) New ¶ added in v0.6.0
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.
type ParsedCommand ¶
func (ParsedCommand) Run ¶
func (po ParsedCommand) 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.
func (ParsedCommand) RunFatal ¶
func (po ParsedCommand) 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 (1 if error, 0 otherwise).
type SetterFunc ¶ added in v0.6.2
type SetterFunc func(interface{}) Setter