Documentation
¶
Overview ¶
Package getopt implements the Unix getopt function for parsing command-line options.
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ( ErrDone = errors.New("getopt: done") ErrUnknownOpt = errors.New("getopt: unrecognized option") ErrIllegalOptArg = errors.New("getopt: option disallows arguments") ErrMissingOptArg = errors.New("getopt: option requires an argument") )
Errors that can be returned during option parsing.
Functions ¶
This section is empty.
Types ¶
type Config ¶
type Config struct { Opts []Opt // allowed short options LongOpts []LongOpt // allowed long options Func Func // parsing function Mode Mode // parsing behavior }
A Config defines the rules and behavior used when parsing options. Note the zero values for Func (FuncGetOpt) and Mode (ModeGNU), which will determine the parsing behavior unless set otherwise.
type Func ¶
type Func int
Func indicates which POSIX or GNU extension function to emulate during option parsing.
type LongOpt ¶
A LongOpt is a parsing rule for a named, long command-line option (e.g., --option).
func LongOptStr ¶
OptStr parses a long option string, returning a slice of LongOpt.
The option string uses the same format as --longoptions in the GNU getopt(1) command. Option names are comma-separated, and argument rules are designated by colon suffixes, like with OptStr.
type Opt ¶
An Opt is a parsing rule for a short, single-character command-line option (e.g., -a).
func OptStr ¶
OptStr parses an option string, returning a slice of Opt.
The option string uses the same format as getopt, with each character representing an option. Options with a single ":" suffix require an argument. Options with a double "::" suffix optionally accept an argument. Options with no suffix do not allow arguments.
type State ¶
type State struct {
// contains filtered or unexported fields
}
func NewState ¶
NewState returns a new State to parse options from args, starting with the element at index 1.
func (*State) Args ¶
Args returns the current slice of arguments in State. This may differ from the slice used to initialize State, since parsing can permute the argument order.
func (*State) GetOpt ¶
GetOpt returns the result of parsing the next option in State.
If parsing has successfully completed, err will be ErrDone. Otherwise, the returned Result indicates either a valid option, or the properties of an invalid option if err is non-nil.
func (*State) OptInd ¶
OptInd returns the index of the next argument that will be parsed in State.
After all options have been parsed, OptInd will index the first parameter (non-option) argument returned by State.Args. If no parameters are present, the index will be invalid, since the next argument's index would have exceeded the bounds of the argument slice. State.Params provides safe access to parameters.
func (*State) Params ¶
Params returns the slice of parameter (non-option) arguments. If parsing has not successfully completed with ErrDone, this may include arguments that otherwise be parsed as options.
func (*State) Parse ¶
Parse returns a slice of Result by calling State.GetOpt until an error is returned.