Documentation
¶
Overview ¶
Package opt implements command line options parsing.
Short and long option formats are supported: -o and --option. Multiple short options can be grouped with one dash sign: -abc. Option can has non-optional argument. Short option parameter can be passed as a part of option string or with a next coming string. To pass value `foo` for option `o` next two syntaxes can be used: -ofoo, -o foo. Long option can be separated from its value with space or equals sign: --option foo, --option=foo. Command-line options and arguments can be passed in any order. `--` indicates and of the passed options, and rest is treated as arguments only, even if it is started from dash.
Usage:
1. Define supported options list using Desc structure to describe every option.
descs := []*opt.Desc{
{"a", "add", opt.ArgNone,
"", "add new item"},
{"d", "delete", opt.ArgNone,
"", "delete item"},
{"h", "help", opt.ArgNone,
"", "display help information and exit"},
{"p", "path", opt.ArgString,
"path", "path to store output files to"},
}
- Parse command-line arguments. opts, args, err := opt.Parse(os.Args[1:], descs) if err != nil { fmt.Fprintf(os.Stderr, "%s\n", err) os.Exit(1) }
Use options and arguments. if opts.Has("help") { fmt.Println("Options:") fmt.Print(opt.Usage(descs)) }
path := opts.StringOr("path", "")
if opts.Has("add") { fmt.Printf("Adding new item into '%s'...\n", path) } if opts.Has("delete") { fmt.Printf("Deleting new item from '%s'...\n", path) }
fmt.Printf("arguments: %s\n", args)
Index ¶
- func Usage(descs []*Desc) string
- type ArgType
- type Desc
- type Option
- type Options
- func (o Options) Float(name string) (float64, bool)
- func (o Options) FloatOr(name string, value float64) float64
- func (o Options) Floats(name string) []float64
- func (o Options) Has(name string) bool
- func (o Options) Int(name string) (int, bool)
- func (o Options) IntOr(name string, value int) int
- func (o Options) Ints(name string) []int
- func (o Options) String(name string) (string, bool)
- func (o Options) StringOr(name string, value string) string
- func (o Options) Strings(name string) []string
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Desc ¶
type Desc struct {
// Short, one letter, name of the option. Short options
// starts with single dash sign.
Short string
// Long option name. Long options starts with two dash signs.
Long string
// Arg is an option type description.
Arg ArgType
// Name of the argument for string, int and float options.
// This name is used for usage information generation.
ArgName string
// Option's description. Used for usage information generation.
Description string
}
Desc describes available option. Short or Long option can be empty, in case option doen't have short or long version respectively.
type Option ¶
type Option struct {
// Option description.
Desc *Desc
// Arguments passed with command line arguments.
Args []interface{}
}
Option parsed from command line arguments.
type Options ¶
type Options []*Option
Options is a list of all parsed options.
func Parse ¶
Parse parses given command line arguments. Available application arguments are defined by `descs` argument. Returns a list of parsed options and a list of free arguments.
func (Options) Float ¶
Float returns float64 option's argument by its short or long name. If option was not defined second parameter is false.
func (Options) FloatOr ¶
FloatOr returns argument of float option or default value if option was not defined by command line arguments list.
func (Options) Floats ¶
Floats returns a list of arguments for float64 option by its short or long name.
func (Options) Int ¶
Int returns integer option's argument by its short or long name. If option was not defined second parameter is false.
func (Options) IntOr ¶
IntOr returns argument of int option or default value if option was not defined by command line arguments list.
func (Options) String ¶
String returns string option's argument by its short or long name. If option was not defined second parameter is false.