Documentation
¶
Overview ¶
Package options implements a self-documenting command line options parsing framework.
The list of options a program wishes to use is specified as a lengthy, multi-line string. This string also serves as the help-string for the options.
Here is an example option specification:
usage: example-tool A short description of the command -- flag --flag,-f,FLAG A description for this flag option= --option=,-o=,OPTION= A description for this option the description continues here !required= --required,-r=,REQUIRED= A required option -- env_var= ENV_VAR= An environment variable -- help help,h Show this help message run run Run some function -- Additional help for options or defaults etc. go here.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Options ¶
Representation of parsed command line arguments according to a given option specification
func (*Options) Get ¶
Return the option corresponding to 'nm'. If the option is not set (provided on the command line), the bool retval will be False.
func (*Options) GetBool ¶
Interpret the option corresponding to the key 'nm' as a Bool and parse it. A failed parse defaults to False.
func (*Options) GetInt ¶
Interpret the option corresponding to the key 'nm' as a signed integer (auto-detected base). The second retval will be false if the parse fails or the key is not found.
func (*Options) GetMulti ¶
For options that are providd multiple times, return all of them in a slice. A nil slice implies the option was not set on the command line.
type Spec ¶
type Spec struct {
// contains filtered or unexported fields
}
Representation of a parsed option specification.
func Parse ¶
Parse a spec string and return a Spec object
Example ¶
spec, err := Parse(` usage: example-tool A short description of the command -- flag --flag,-f,FLAG A description for this flag option= --option=,-o=,OPTION= A description for this option the description continues here !required= --required,-r=,REQUIRED= A required option -- env_var= ENV_VAR= An environment variable -- help help,h Show this help message run run Run some function -- More freestyle text `) if err != nil { spec.PrintUsageWithError(err) } opts, err := spec.Interpret([]string{"example-tool", "--required", "hello world"}, []string{}) if err != nil { spec.PrintUsageWithError(err) } v, _ := opts.Get("required") fmt.Printf("required: %s", v)
Output: required: hello world
func (*Spec) Interpret ¶
Parse the command line arguments in 'args' and the environment variables in 'environ'. Return the resulting, parsed options in 'o' and any error in 'err'.
func (*Spec) MustInterpret ¶
Parse the command line arguments in 'args' and the environment variables in 'environ'. This expects the parsing to succeed and exits with usage string and error if the parsing fails.
func (*Spec) PrintUsageAndExit ¶
func (spec *Spec) PrintUsageAndExit()
Print the usage string to STDOUT and exit with a non-zero code.
func (*Spec) PrintUsageWithError ¶
Print the error string corresponding to 'err' and then show the usage string. Both are sent to STDERR. Exit with a non-zero code.