Documentation
¶
Index ¶
- type ExclusiveGroup
- type Group
- type Parser
- func (parser *Parser) FormatError(err error) string
- func (parser *Parser) FormatHelp() string
- func (parser *Parser) FormatUsage() string
- func (parser *Parser) GetCommand() string
- func (parser *Parser) GetDescription() string
- func (parser *Parser) Parse() error
- func (parser *Parser) ParseArgs(arguments []string) error
- func (parser *Parser) ParseOrExit()
- func (parser *Parser) Validate() error
- type Subparser
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type ExclusiveGroup ¶
type ExclusiveGroup struct {
Options []option.Option
// Required demands that one of them be given.
Required bool
}
ExclusiveGroup is a set of the parser's options of which at most one may be given.
type Group ¶
Group titles a set of the parser's options in the help message. It only arranges the help; it does not affect what is accepted.
type Parser ¶
type Parser struct {
Options []option.Option
Parsers []Subparser
// Groups arrange options under their own headings in the help message. An option that appears
// in no group is listed under "Options:".
Groups []*Group
// ExclusiveGroups declare options that rule one another out.
ExclusiveGroups []*ExclusiveGroup
// Command is the name this parser answers to when it is used as a Subparser.
Command string
Description string
ProgramName string
// Positionals are given the arguments that name no option, in order, according to how many
// each takes. A positional is named in the help by its metavar.
Positionals []option.Option
// Rest receives the arguments left over once the positionals have taken theirs, including
// everything after a "--" terminator. A leftover argument is an error while this is nil.
Rest *[]string
// DisableHelp withholds the automatic help option, leaving "-h" and "--help" to be used as
// ordinary option names, or to be reported as unknown.
DisableHelp bool
// DisableAbbrev withholds prefix matching of long names. It is allowed by default, as argparse
// and getopt_long allow it: an unambiguous prefix stands for the whole name, and an ambiguous
// one is an error naming the candidates. Disabling it fixes the accepted command line to the
// names as written, so that adding an option later cannot break a caller that spelled an
// existing one short.
DisableAbbrev bool
// Output receives the help message. os.Stdout is used when nil: help is an answer to an
// explicit request, not a diagnostic, so it must stay on stdout to survive a pipe.
Output io.Writer
// Width is the column at which the help message wraps. COLUMNS, then defaultWidth, is used
// when this is not positive.
Width int
}
func (*Parser) FormatError ¶
FormatError renders err the way a command-line program conventionally reports a bad invocation: the usage line, then the program name and what went wrong.
func (*Parser) FormatHelp ¶
FormatHelp returns the usage message describing the parser's commands and options.
func (*Parser) FormatUsage ¶
FormatUsage returns the usage line on its own. A report of a bad invocation conventionally begins with it, so that being told what went wrong also says how the program is meant to be run.
func (*Parser) GetCommand ¶
GetCommand returns the name this parser answers to as a subparser.
func (*Parser) GetDescription ¶
GetDescription returns the description a parent parser shows beside this parser's command.
func (*Parser) Parse ¶
Parse parses the arguments of the running program, excluding its name. An empty argument list is still parsed, so that a missing required option is reported.
func (*Parser) ParseArgs ¶
ParseArgs parses arguments, dispatching to a subparser when the first argument names one.
func (*Parser) ParseOrExit ¶
func (parser *Parser) ParseOrExit()
ParseOrExit parses the running program's arguments and returns only if that succeeded. A request for help has already been answered on stdout, and leaves through status 0; a bad invocation is reported on stderr and leaves through status 2, as a command-line program conventionally does.