API Reference
- Setup
- Parsing Arguments
- Registering Flags and Options
- Retrieving Values
- Positional Arguments
- Commands
Setup
-
func NewParser() *ArgParser
-
Creates a new
ArgParser
instance.You can specify
.Helptext
and.Version
strings for the parser.Specifying a helptext string activates an automatic
--help
flag (also a-h
shortcut unless registered by another option).Specifying a version string activates an automatic
--version
flag (also a-v
shortcut unless registered by another option).
Parsing Arguments
-
func (parser *ArgParser) Parse()
-
Parses the application's command line arguments.
Registering Flags and Options
-
func (parser *ArgParser) NewFlag(name string)
-
Registers a new flag. The
name
parameter accepts an unlimited number of space-separated aliases and single-character shortcuts.A flag is a valueless option — it's either present or absent, but takes no argument. You can check for the presence of a flag using the parser's
.Found()
method. -
func (parser *ArgParser) NewFloatOption(name string, fallback float64)
-
Registers a new float-valued option. The
name
parameter accepts an unlimited number of space-separated aliases and single-character shortcuts. Thefallback
parameter specifies the option's default value. -
func (parser *ArgParser) NewIntOption(name string, fallback int)
-
Registers a new integer-valued option. The
name
parameter accepts an unlimited number of space-separated aliases and single-character shortcuts. Thefallback
parameter specifies the option's default value. -
func (parser *ArgParser) NewStringOption(name string, fallback string)
-
Registers a new string-valued option. The
name
parameter accepts an unlimited number of space-separated aliases and single-character shortcuts. Thefallback
parameter specifies the option's default value.
Retrieving Values
Any of an option's registered aliases or shortcuts can be used for the name
parameter in the methods below.
-
func (parser *ArgParser) Count(name string) int
-
Returns the number of times the specified option was found.
-
func (parser *ArgParser) FloatValue(name string) float64
-
Returns the value of the specified float-valued option.
-
func (parser *ArgParser) FloatValues(name string) []float64
-
Returns the specified float-valued option's list of values.
-
func (parser *ArgParser) Found(name string) bool
-
Returns
true
if the specified flag or option was found. -
func (parser *ArgParser) IntValue(name string) int
-
Returns the value of the specified integer-valued option.
-
func (parser *ArgParser) IntValues(name string) []int
-
Returns the specified integer-valued option's list of values.
-
func (parser *ArgParser) StringValue(name string) string
-
Returns the value of the specified string-valued option.
-
func (parser *ArgParser) StringValues(name string) []string
-
Returns the specified string-valued option's list of values.
Positional Arguments
Positional arguments (also known as 'free' arguments, i.e. arguments that are not option values) are appended to a slice of strings which can be accessed via the parser's .Args
property.
The following convenience methods are also available for parsing the arguments as integers or floats.
-
func (parser *ArgParser) ArgsAsFloats() []float64
-
Attempts to parse and return the positional arguments as a slice of floats. Exits with an error message on failure.
-
func (parser *ArgParser) ArgsAsInts() []int
-
Attempts to parse and return the positional arguments as a slice of integers. Exits with an error message on failure.
Commands
-
func (parser *ArgParser) CommandName() string
-
Returns the command name, if the parser has found a command.
-
func (parser *ArgParser) CommandParser() *ArgParser
-
Returns the command's
ArgParser
instance, if the parser has found a command. -
func (parser *ArgParser) EnableHelpCommand(enable bool)
-
This boolean switch toggles support for an automatic
help
command which prints subcommand helptext. The value defaults tofalse
but gets toggled automatically totrue
whenever a command is registered. You can use this function to disable the feature if required. -
func (parser *ArgParser) HasCommand() bool
-
Returns
true
if the parser has found a command. -
func (parser *ArgParser) NewCommand(name string) *ArgParser
-
Registers a new command. The
name
parameter accepts an unlimited number of space-separated aliases for the command. Returns the command'sArgParser
instance.