Documentation
¶
Overview ¶
Package commandline implements a command line parser.
Index ¶
- Variables
- type Command
- type CommandFunc
- type Commands
- type Context
- type Param
- type Params
- func (p *Params) AddParam(long, short, help string, required bool, value interface{}) error
- func (p *Params) AddRawParam(name, help string, required bool, value interface{}) error
- func (p *Params) MustAddParam(long, short, help string, required bool, value interface{}) *Command
- func (p *Params) MustAddRawParam(name, help string, required bool, value interface{}) *Command
- func (p *Params) ParamCount() int
- func (p *Params) Parsed(name string) bool
- func (p *Params) RawArgs() []string
- func (p *Params) RawValue(name string) string
- type Parser
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( // ErrNoArgs is returned by Parse if no arguments were specified on // command line and there are defined Commands or Params. ErrNoArgs = errors.New("commandline: no arguments") // ErrInvalidName is returned by Add* methods when an invalid Command or // Param long or short name is specified. ErrInvalidName = errors.New("commandline: invalid name") // ErrInvalidValue is returned by Add* methods or during parsing if an // invalid parameter is given for a Param value, i.e. not a valid pointer // to a Go value. ErrInvalidValue = errors.New("commandline: invalid value") )
Functions ¶
This section is empty.
Types ¶
type Command ¶
type Command struct {
Params // Params are this Command's Params.
Commands // Commands are this Command's Commands.
// contains filtered or unexported fields
}
Command is a command definition. A Command can contain sub-Commands and propagate Parser args further down the Commands chain. It can have zero or more defined Param instances in its' Params.
type CommandFunc ¶
CommandFunc is a prototype of a function that handles the event of a Command being parsed from command line arguments.
Parser parses Command's Params and pauses parsing when it finds next Command in arguments or it exhausts arguments, invokes parsed Command's CommandFunc carrying parsed Params then either continues parsing if the handler returns nil or stops and returns the error that the handler returned back to the Parse method whose caller is responsible for interpreting that error.
If the invoked Command has any raw Params registered, parsing will not continue after CommandFunc invocation.
type Commands ¶
type Commands struct {
// contains filtered or unexported fields
}
Commands holds a set of Commands with a unique name.
func (*Commands) AddCommand ¶
func (c *Commands) AddCommand(name, help string, f CommandFunc) (*Command, error)
AddCommand registers a new Command under specified name and help text that invokes f when parsed from arguments if it is not nil. Name is the only required parameter.
If Commands is the root set in a Parser it can register a single Command with an empty name to be an unnamed container in a global params pattern.
If a registration error occurs it is returned with a nil *Command.
func (*Commands) GetCommand ¶
GetCommand returns a *Command by name if found and truth if found.
func (*Commands) MustAddCommand ¶ added in v0.0.2
func (c *Commands) MustAddCommand(name, help string, f CommandFunc) *Command
MustAddCommand is like AddCommand except the function panics on error. Returns added *Command.
func (*Commands) MustGetCommand ¶ added in v0.0.2
MustGetCommand is like GetCommand but panics if Command is not found.
type Context ¶ added in v0.0.3
type Context interface {
// Parsed will return if the param under specified long name was parsed.
Parsed(string) bool
// RawValue will return the string value of param, if parsed.
RawValue(string) string
// RawArgs will return a slice of raw values if this CommandFunc has no
// defined params and custom handles params.
RawArgs() []string
}
Context is a CommandFunc context.
type Param ¶
type Param struct {
// contains filtered or unexported fields
}
Param defines a Command parameter contained in a Params.
type Params ¶
type Params struct {
// contains filtered or unexported fields
}
A Params defines a set of Command Params unique by long name.
func (*Params) AddParam ¶
AddParam registers a new Param in these Params.
Long param name is required, short is optional and can be empty, as is help.
If required is specified value must be a pointer to a supported Go value which will be updated to a value parsed from an argument following param. If a required Param or its' value is not found in command line args an error is returned.
If Param is not marked as required, specifying a value parameter is optional but dictates that: If nil, a value for the Param will not be parsed from args. If valid, the parser will parse the argument following the Param into it.
If an error occurs Param is not registered.
func (*Params) AddRawParam ¶
AddRawParam registers a raw Param under specified name which must be unique in long Params names. Raw params can only be defined after prefixed params or only raw params. Calls to AddParam after AddRawParam will error.
Parsed arguments are applied to raw Params in order as they are defined. If value is a pointer to a valid Go value argument will be converted to that Go value. Specifying a value is optional and if nil, parsed argument will not be parsed into the value.
A single non-required raw Param is allowed and it must be the last one.
If an error occurs it is returned and the Param is not registered.
func (*Params) MustAddParam ¶ added in v0.0.2
MustAddParam is like AddParam except the function panics on error. Returns a Command that the param was added to.
func (*Params) MustAddRawParam ¶ added in v0.0.2
MustAddRawParam is like AddRawParam except the function panics on error. Returns a Command that the param was added to.
func (*Params) ParamCount ¶ added in v0.0.2
ParamCount returns number of defined params.
func (*Params) Parsed ¶
Parsed returns if the param under specified name was parsed. If the Param under specified name is not registered, returns false.
func (*Params) RawArgs ¶
RawArgs returns arguments of raw Params in order as passed on command line. It will be an empty slice if no arguments were passed to the param or the param is not raw type.
type Parser ¶
type Parser struct {
// Commands is the root command set.
//
// Root Commands as an exception allows a single Command
// with an empty name that serves as "global flag" container.
Commands
// contains filtered or unexported fields
}
Parser is a command line parser. Its' Parse method is to be invoked with a slice of command line arguments passed to program. For example:
err := Parser.Parse(os.Args[1:])
It is command oriented, meaning that one or more Command instances can be defined in Parser's Commands which when parsed from command line arguments invoke CommandFuncs registered for those Command instances. Command can have its' own Commands so a Command hierarchy can be defined.
Root Commands, as an exception, allow for one Command with an empty name to be defined. This is to allow that program args need not start with a Command and to allow Params to be passed first which can act as "global". e.g. "--verbose list users"
Command can have one or more Param instances defined in its' Params which can have names, help text, be required or optional and have an optional pointer to a Go value which is written from a value following Param in command line arguments.
If a pointer to a Go value is registered with a Param, the Param will require an argument following it that the parser will try to convert to the Go value registered with Param. Otherwise the Param will act as a simple flag which can be checked if parsed in the handler by checking the result of handler's Params.Parsed("long param name").
Parser supports prefixed and raw params which can be combined on a Command with a caveat that the Command that has one or more raw params registered cannot have sub-Commands because of ambiguities in parsing command names and raw parameters as well as the fact that one last raw param can be optional.
Prefixed params are explicitly addressed on a command line and can have short and long forms. They can be marked optional or required and be registered in any order, but before any raw params.
Short Param names have the "-" prefix, can be one character long and can be combined together following the short form prefix if none of the combined Params require a Param Value. They are optional per Param.
Long Param names have the "--" prefix and cannot be combined.
Raw params are not addressed but are instead matched against registered raw Params in order of registration as they appear on command line, respectively.
Prefixed and raw params can both be registered for a Command but raw params must be registered last and specified after prefixed Params on the command line. e.g. "rmdir -v /home/me/stuff" where "rmdir" is a command, "-v" is a prefixed param and "/home/me/stuff" is a raw parameter.
If Params are defined as optional they do not cause a parse error if not parsed from program args and return a parse error if defined as required and not parsed from command line.
Command can have a CommandFunc registered optionaly so that a Command can serve solely as sub-Command selector. For more details see CommandFunc.
If no Params were defined on a Command all command line arguments following the command invocation will be passed to Command handler via Params.RawArgs.
If no params were defined on a Command and the command has no CommandFunc registered an error is returned.
Example ¶
cl := New()
var barVal string
cl.MustAddCommand("", "", nil).MustAddParam("verbose", "v", "Verbose output.", false, nil)
cl.MustAddCommand("foo", "Do the foo.", nil).MustAddParam("bar", "r", "Enable bar.", true, &barVal)
cl.MustAddCommand("baz", "Do the baz.", nil).MustAddRawParam("bat", "Enable bat.", false, nil)
cl.Parse([]string{"--verbose", "foo", "--bar", "bar", "baz", "bat"})
fmt.Println(cl.Print())