Documentation ¶
Index ¶
- Constants
- type Parser
- func (p *Parser) Args() []map[string]string
- func (p *Parser) At(index int) (string, bool)
- func (p *Parser) Get(name string, alts ...string) []string
- func (p *Parser) GetInt(name string, alts ...string) int64
- func (p *Parser) GetString(name string, alts ...string) string
- func (p *Parser) HasOption(option string, alts ...string) bool
- func (p *Parser) LookupInt(name string, alts ...string) (int64, bool)
- func (p *Parser) LookupString(name string, alts ...string) (string, bool)
- func (p *Parser) Options() []string
- func (p *Parser) Parse(args ...[]string) error
- func (p *Parser) Positional() []string
Constants ¶
const FlagPrefix = '-'
FlagPrefix defines the special character used to indicate that this arg is a flag, we use this instead of a hard coded value to make it easier if we want to support platform-specific syntax in the future (i.e. the /arg syntax in Windows).
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Parser ¶
type Parser struct {
// contains filtered or unexported fields
}
Parser is the main struct that does the parsing and hold the results.
func NewParser ¶
NewParser constructs a new Parser struct and returns a pointer to it.
Optionally you can pass the arguments slice to be parsed to this function, example:
parser := args.NewParser(os.Args[1:])
Please note that you have to pass the arguments slice to either this or the `Parse()` function.
func (*Parser) Args ¶
Args returns the parsed arguments (if has value) in the form of a slice of key-value pairs.
Make sure to call `Parse()` before using this function.
func (*Parser) At ¶
At returns the positional argument in the specified index, and a bool value indicates weither the positional argument exists.
If the specified index was not found (out-of-index) the first return value will be an empty string.
Make sure to call `Parse()` before using this function.
func (*Parser) Get ¶
Get return all values provided with the given name. example:
$ myapp --name foo --name bar --name baz names := parser.Get("--name") for _, name := range names { print(name) }
Get() allow alternative names lookup, example:
names := parser.Get("--name", "-n")
Make sure to call `Parse()` before using this function.
func (*Parser) GetInt ¶
GetInt returns the value of the given argument name.
If multiple values found with the same name, the first one from the right will be returned, example:
$ myapp --age 18 --age 21 --age 30 age := parser.GetInt("--age") print(age) // 30
GetInt() allow alternative age lookup, example:
$ myapp --name foo -a 30 age := parser.LookupString("--age", "-a") print(age) // 30
GetInt() returns -1 If argument was not found.
GetInt() panics if the argument was found but
was an invalid int value.
Make sure to call `Parse()` before using this function.
func (*Parser) GetString ¶
GetString returns the value of the given argument name.
If multiple values found with the same name, the first one from the right will be returned, example:
$ myapp --name foo --name bar --name baz name := parser.GetString("--name") print(name) // baz
GetString() allow alternative name lookup, example:
names := parser.GetString("--name", "-n")
Make sure to call `Parse()` before using this function.
func (*Parser) HasOption ¶
HasOption asks if a specific option was provided, example:
if parser.HasOption("-h") { // display help message! }
HasOption also supports alias names lookup, example:
if parser.HasOption("--help", "-h") { // display help message! }
Make sure to call `Parse()` before using this function.
func (*Parser) LookupInt ¶
LookupInt returns the value of the given argument name, and a bool value indicates weither the argument exists.
If multiple values found with the same name, the first one from the right will be returned, example:
$ myapp --age 18 --age 21 --age 30 age, exists := parser.GetInt("--age") print(age) // 30 print(exists) // true
LookupInt() allow alternative name lookup, example:
$ myapp --name foo -a 30 age, exists := parser.LookupInt("--age", "-a") print(age) // 30 print(exists) // true
LookupInt() returns -1 If argument was not found.
LookupInt() panics if the argument was found but
was an invalid int value.
Make sure to call `Parse()` before using this function.
func (*Parser) LookupString ¶
LookupString returns the value of the given argument name, and a bool value indicates weither the argument exists.
If multiple values found with the same name, the first one from the right will be returned, example:
$ myapp --name foo --name bar --name baz name, exists := parser.LookupString("--name") print(name) // baz print(exists) // true
LookupString() allow alternative name lookup, example:
$ myapp --age 30 -n bar name, exists := parser.LookupString("--name", "-n") print(name) // bar print(exists) // true
Make sure to call `Parse()` before using this function.
func (*Parser) Options ¶
Options returns the parsed options in the form of string-slice.
Make sure to call `Parse()` before using this function.
func (*Parser) Parse ¶
Parse parses the command-line arguments and store, the result into the owner struct.
Optionally you can pass the arguments slice to be parsed to this function, example:
parser := args.NewParser() parser.Parse(os.Args[1:])
Please note that arguments passed to this function will be ignored if you declared the parser struct with initial arguments, like:
parser := args.NewParser(os.Args[1:])
func (*Parser) Positional ¶
Positional returns the parsed positional arguments in the form of string-slice.
Make sure to call `Parse()` before using this function.