Documentation
¶
Index ¶
- Variables
- type Beforer
- type Opts
- func (opts *Opts) AddCommand(cmdOpts *Opts) *Opts
- func (opts *Opts) AddCommands(cmds []*Opts) *Opts
- func (opts *Opts) HelpString() string
- func (opts *Opts) Parse() ParsedOpts
- func (opts *Opts) ParseArgs(args []string) ParsedOpts
- func (opts *Opts) SetHelp(help string) *Opts
- func (opts *Opts) SetShortHelp(help string) *Opts
- func (opts *Opts) SetShortName(shortName string) *Opts
- func (opts *Opts) WriteHelp(w io.Writer)
- type ParsedOpts
- type Runner
Constants ¶
This section is empty.
Variables ¶
var ErrHelp = fmt.Errorf("opts: help requested")
Functions ¶
This section is empty.
Types ¶
type Opts ¶
type Opts struct { Name string ShortName string Help string ShortHelp string // 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 Opts 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 Opts 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 (*Opts) AddCommand ¶
AddCommand registers another Opts instance as a subcommand of this Opts instance.
func (*Opts) AddCommands ¶
AddCommands registers multiple Opts instances as subcommands of this Opts instance.
func (*Opts) HelpString ¶
func (*Opts) Parse ¶
func (opts *Opts) Parse() ParsedOpts
Parse is a convenience method for calling ParseArgs(os.Args)
func (*Opts) ParseArgs ¶
func (opts *Opts) ParseArgs(args []string) ParsedOpts
ParseArgs parses using the passed-in args slice and OS-provided environment variables and returns a ParsedOpts instance which can be used for further method chaining.
If there are args remaining after parsing this Opts' fields, subcommands will be parsed recursively. The returned ParsedOpts.Runner represents the command which was specified (if it has a Run method). If a Before method is implemented on the Opts' config, this method will call it before recursing into any subcommand parsing.
func (*Opts) SetShortHelp ¶
SetHelp configures the command short help string.
func (*Opts) SetShortName ¶
SetShortName configures a short alias for the command.
type ParsedOpts ¶
func (ParsedOpts) Run ¶
func (po ParsedOpts) Run() error
Run calls the Run method of the Opts 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 (ParsedOpts) RunFatal ¶
func (po ParsedOpts) RunFatal()
RunFatal is like Run, except it automatically handles printing out any errors returned by the Run method of the underlying Opts config, and exits with an appropriate status (1 if error, 0 otherwise).