argument_parser

package
v1.14.0 Latest Latest
Warning

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

Go to latest
Published: Aug 16, 2026 License: MIT Imports: 11 Imported by: 0

Documentation

Index

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

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
	// 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

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) 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.

type Subparser

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

Subparser consumes the arguments that follow its command name.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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