Documentation
¶
Overview ¶
Package optionparser is a library for defining and parsing command line options. It aims to provide a natural language interface for defining short and long parameters and mandatory and optional arguments. It provides the user with nice output formatting on the built-in method '--help'.
Index ¶
- Variables
- type OptionParser
- func (op *OptionParser) Command(cmd string, helptext string)
- func (op *OptionParser) GenerateCompletion(shell, programName string, w io.Writer) error
- func (op *OptionParser) Help()
- func (op *OptionParser) On(a ...interface{})
- func (op *OptionParser) Parse() error
- func (op *OptionParser) ParseFrom(args []string) error
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ErrHelp = errors.New("help requested")
ErrHelp is returned by Parse/ParseFrom when the user requested help. Callers can check for this sentinel error and decide whether to os.Exit(0).
Functions ¶
This section is empty.
Types ¶
type OptionParser ¶
type OptionParser struct {
Extra []string
Banner string
Coda string
Start int
Stop int
// Out is the writer Help() and related formatting functions write to.
// Defaults to os.Stdout.
Out io.Writer
// contains filtered or unexported fields
}
OptionParser contains the methods to parse options and the settings to influence the output of --help. Set the Banner and Coda for usage info, set Start and Stop for output of the long description text.
func NewOptionParser ¶
func NewOptionParser() *OptionParser
NewOptionParser initializes the OptionParser struct with sane settings for Banner, Start and Stop and adds a "-h", "--help" option for convenience.
func (*OptionParser) Command ¶
func (op *OptionParser) Command(cmd string, helptext string)
Command defines optional arguments to the command line. These are written in a separate section called 'Commands' on --help.
func (*OptionParser) GenerateCompletion ¶ added in v1.2.0
func (op *OptionParser) GenerateCompletion(shell, programName string, w io.Writer) error
GenerateCompletion writes a shell completion script for the given shell to w. Supported shells are "bash", "zsh" and "fish". programName is the command users invoke (for example "sp").
The generated script offers tab completion for all registered long and short options. Options registered with a "--no-" prefix are emitted in both forms. Registered commands appear as positional completions.
Because the parser has no knowledge of option value semantics, options that expect a parameter offer file completion as a best-effort default.
func (*OptionParser) Help ¶
func (op *OptionParser) Help()
Help prints help text generated from the "On" commands to op.Out.
Example ¶
op := NewOptionParser()
var str string
var options = make(map[string]string)
var truefalse bool
var stringslice []string
myfunc := func() { return }
op.On("-a", "--func", "call myfunc", myfunc)
op.On("--bstring FOO", "set string to FOO", &str)
op.On("-c", "set boolean option (try -no-c)", options)
op.On("-d", "--dlong VAL", "set option", options)
op.On("-e", "--elong [VAL]", "set option with optional parameter", options)
op.On("-f", "boolean option", &truefalse)
op.On("-g VALUES", "give multiple values", &stringslice)
op.Help()
Output: Usage: [parameter] command -h, --help Show this help -a, --func call myfunc --bstring=FOO set string to FOO -c set boolean option (try -no-c) -d, --dlong=VAL set option -e, --elong[=VAL] set option with optional parameter -f boolean option -g=VALUES give multiple values
func (*OptionParser) On ¶
func (op *OptionParser) On(a ...interface{})
On defines arguments and parameters. Each argument is one of:
- a short option, such as "-x",
- a long option, such as "--extra",
- a long option with an argument such as "--extra FOO" (or "--extra=FOO") for a mandatory argument,
- a long option with an argument in brackets, e.g. "--extra [FOO]" for a parameter with optional argument,
- a string (not starting with "-") used for the parameter description, e.g. "This parameter does this and that",
- a string variable in the form of &str that is used for saving the result of the argument,
- a variable of type map[string]string which is used to store the result (the parameter name is the key, the value is either the string true or the argument given on the command line)
- a variable of type *[]string which gets a comma separated list of values,
- a bool variable (in the form &bool) to hold a boolean value, or
- a function in the form of func() or in the form of func(string) which gets called if the command line parameter is found.
On panics if the user supplies a type in its argument other than the ones given above.
op := optionparser.NewOptionParser()
op.On("-a", "--func", "call myfunc", myfunc)
op.On("--bstring FOO", "set string to FOO", &somestring)
op.On("-c", "set boolean option (try --no-c)", options)
op.On("-d", "--dlong VAL", "set option", options)
op.On("-e", "--elong [VAL]", "set option with optional parameter", options)
op.On("-f", "boolean option", &truefalse)
op.On("-g VALUES", "give multiple values", &stringslice)
and running the program with --help gives the following output:
go run main.go --help
Usage: [parameter] command
-h, --help Show this help
-a, --func call myfunc
--bstring=FOO set string to FOO
-c set boolean option (try --no-c)
-d, --dlong=VAL set option
-e, --elong[=VAL] set option with optional parameter
-f boolean option
-g=VALUES give multiple values
func (*OptionParser) Parse ¶
func (op *OptionParser) Parse() error
Parse takes the command line arguments as found in os.Args and interprets them. If it finds an unknown option or a missing mandatory argument, it returns an error.
func (*OptionParser) ParseFrom ¶ added in v1.0.3
func (op *OptionParser) ParseFrom(args []string) error
ParseFrom takes a slice of string arguments and interprets them. If it finds an unknown option or a missing mandatory argument, it returns an error. Note: this function expects args like os.Args (program name at index 0) and starts parsing at index 1.