Documentation
¶
Overview ¶
Shell completions, written from a parser's own declaration.
A parser already knows everything a completion needs: the options and what each one is for, whether it takes an argument, the values it accepts, which options rule out which others, and what the subcommands are. A hand-written completion has to be told all of that again, and drifts the first time an option is added. Generating it means the completion cannot disagree with what the program accepts, because both are read from the same declaration.
It lives here rather than beside the parser because a parser cannot import a package that imports it, and because writing a completion is the same job as writing the help: rendering the declaration for someone to read. Every program built on this parser answers --completion without being told to, which is the whole point -- an ecosystem of programs that all complete is worth more than one that each had to opt in.
The generated script is static: it encodes the declaration and calls nothing back. That keeps completion working when the binary is slow to start, or absent, and makes the output something a package can ship.
Index ¶
- Constants
- Variables
- 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) Given(opt option.Option) bool
- func (parser *Parser) Parse() error
- func (parser *Parser) ParseArgs(arguments []string) error
- func (parser *Parser) ParseOrExit()
- func (parser *Parser) Validate() error
- func (parser *Parser) WriteCompletion(writer io.Writer, shell string) error
- type Subparser
- type UnsupportedShellError
Constants ¶
const ( Zsh = "zsh" Bash = "bash" )
The shells that can be written.
Variables ¶
var Shells = []string{Zsh, Bash}
Shells are the shells this package writes, in the order the help lists them.
zsh comes first because it is the one that can express the whole declaration: descriptions against each option, the values an option accepts, and options that rule one another out. bash's completion is a coarser thing and says less, which is a limit of the shell rather than of what is known here.
Functions ¶
This section is empty.
Types ¶
type ExclusiveGroup ¶
type ExclusiveGroup struct {
Options []option.Option
// Groups are the members that are groups of options. They follow the options in the usage
// line, each option of a group bracketed on its own. Naming a group here says what is
// accepted; titling it in the help is what Parser.Groups does, and a group may be in both.
Groups []*Group
// Required demands that one member be given.
Required bool
}
ExclusiveGroup is a set of members of which at most one may be given. A member is an option on its own, or a group of options that count as one: giving any of a group's options chooses that member, and the group's options may be combined with one another freely. One --no-timeouts option against a group of several timeout options is the case a flat list cannot express.
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
// DisableCompletion withholds the automatic --completion option, which a program that writes
// its own completions or wants the name for something else turns off.
DisableCompletion bool
// 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
// contains filtered or unexported fields
}
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) Given ¶ added in v1.59.0
Given reports whether an argument of the last parse named the option, as opposed to the option holding its default or having been left alone. The parser tells the two apart for its own checks, so that a defaulted option never conflicts with one that rules it out; this is the same answer for a program that needs it, such as to tell an explicit --timeout 0 from no timeout given. It is false before any parse, and for every option of a parser that handed the arguments to a subparser.
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.
type UnsupportedShellError ¶ added in v1.43.0
type UnsupportedShellError struct {
Shell string
}
UnsupportedShellError is a shell this package cannot write.
func (*UnsupportedShellError) Error ¶ added in v1.43.0
func (unsupportedShellError *UnsupportedShellError) Error() string