argument_parser

package
v1.88.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 27, 2026 License: MIT Imports: 13 Imported by: 6

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

View Source
const (
	Zsh  = "zsh"
	Bash = "bash"
)

The shells that can be written.

Variables

View Source
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

type Group struct {
	Title   string
	Options []option.Option
}

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

func (parser *Parser) FormatError(err error) string

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

func (parser *Parser) FormatHelp() string

FormatHelp returns the usage message describing the parser's commands and options.

func (*Parser) FormatUsage

func (parser *Parser) FormatUsage() string

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

func (parser *Parser) GetCommand() string

GetCommand returns the name this parser answers to as a subparser.

func (*Parser) GetDescription

func (parser *Parser) GetDescription() string

GetDescription returns the description a parent parser shows beside this parser's command.

func (*Parser) Given added in v1.59.0

func (parser *Parser) Given(opt option.Option) bool

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

func (parser *Parser) Parse() error

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

func (parser *Parser) ParseArgs(arguments []string) error

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.

func (*Parser) Validate

func (parser *Parser) Validate() error

Validate reports what is wrong with the parser's own declaration, recursively through its subparsers, so that a duplicated option name surfaces at startup rather than on a first parse that happens to reach it.

func (*Parser) WriteCompletion added in v1.43.0

func (parser *Parser) WriteCompletion(writer io.Writer, shell string) error

WriteCompletion writes a completion script for the shell to w.

type Subparser

type Subparser interface {
	ParseArgs([]string) error
	GetCommand() string
}

Subparser consumes the arguments that follow its command name.

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

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL