Documentation ¶
Index ¶
- func IsParserErr(err error) bool
- type App
- type Cmd
- func (cmd *Cmd) AddOperand(name string, modifer string)
- func (cmd *Cmd) Args() []string
- func (cmd *Cmd) Bool(long string, short rune, defaultValue bool, help ...string) *bool
- func (cmd *Cmd) BoolP(target *bool, long string, short rune, defaultValue bool, help ...string)
- func (cmd *Cmd) Choice(choices []string, long string, short rune, defaultValue string, help ...string) *string
- func (cmd *Cmd) ChoiceP(target *string, choices []string, long string, short rune, defaultValue string, ...)
- func (cmd *Cmd) Command(name, brief string, callback CmdCallback)
- func (cmd *Cmd) CommandMatch(name, brief string, callback MatchCallback)
- func (cmd *Cmd) CommandRun(name, brief string, callback RunCallback)
- func (cmd *Cmd) CustomP(target CustomArg, long string, short rune, defaultValue string, help ...string)
- func (cmd *Cmd) Err(handler ErrCallback)
- func (cmd *Cmd) Float32(long string, short rune, defaultValue float32, help ...string) *float32
- func (cmd *Cmd) Float32P(target *float32, long string, short rune, defaultValue float32, help ...string)
- func (cmd *Cmd) Float64(long string, short rune, defaultValue float64, help ...string) *float64
- func (cmd *Cmd) Float64P(target *float64, long string, short rune, defaultValue float64, help ...string)
- func (cmd *Cmd) GetBool(name string) *bool
- func (cmd *Cmd) GetChoice(name string) *string
- func (cmd *Cmd) GetCustom(name string) CustomArg
- func (cmd *Cmd) GetFloat32(name string) *float32
- func (cmd *Cmd) GetFloat64(name string) *float64
- func (cmd *Cmd) GetInt(name string) *int
- func (cmd *Cmd) GetInt16(name string) *int16
- func (cmd *Cmd) GetInt32(name string) *int32
- func (cmd *Cmd) GetInt64(name string) *int64
- func (cmd *Cmd) GetInt8(name string) *int8
- func (cmd *Cmd) GetString(name string) *string
- func (cmd *Cmd) GetUint(name string) *uint
- func (cmd *Cmd) GetUint16(name string) *uint16
- func (cmd *Cmd) GetUint32(name string) *uint32
- func (cmd *Cmd) GetUint64(name string) *uint64
- func (cmd *Cmd) GetUint8(name string) *uint8
- func (cmd *Cmd) Help()
- func (cmd *Cmd) Int(long string, short rune, defaultValue int, help ...string) *int
- func (cmd *Cmd) Int16(long string, short rune, defaultValue int16, help ...string) *int16
- func (cmd *Cmd) Int16P(target *int16, long string, short rune, defaultValue int16, help ...string)
- func (cmd *Cmd) Int32(long string, short rune, defaultValue int32, help ...string) *int32
- func (cmd *Cmd) Int32P(target *int32, long string, short rune, defaultValue int32, help ...string)
- func (cmd *Cmd) Int64(long string, short rune, defaultValue int64, help ...string) *int64
- func (cmd *Cmd) Int64P(target *int64, long string, short rune, defaultValue int64, help ...string)
- func (cmd *Cmd) Int8(long string, short rune, defaultValue int8, help ...string) *int8
- func (cmd *Cmd) Int8P(target *int8, long string, short rune, defaultValue int8, help ...string)
- func (cmd *Cmd) IntP(target *int, long string, short rune, defaultValue int, help ...string)
- func (cmd *Cmd) Match(callback MatchCallback)
- func (cmd *Cmd) Operand(name string) string
- func (cmd *Cmd) PrintHelp(writer io.Writer)
- func (cmd *Cmd) Run(callback RunCallback)
- func (cmd *Cmd) String(long string, short rune, defaultValue string, help ...string) *string
- func (cmd *Cmd) StringP(target *string, long string, short rune, defaultValue string, help ...string)
- func (cmd *Cmd) Uint(long string, short rune, defaultValue uint, help ...string) *uint
- func (cmd *Cmd) Uint16(long string, short rune, defaultValue uint16, help ...string) *uint16
- func (cmd *Cmd) Uint16P(target *uint16, long string, short rune, defaultValue uint16, help ...string)
- func (cmd *Cmd) Uint32(long string, short rune, defaultValue uint32, help ...string) *uint32
- func (cmd *Cmd) Uint32P(target *uint32, long string, short rune, defaultValue uint32, help ...string)
- func (cmd *Cmd) Uint64(long string, short rune, defaultValue uint64, help ...string) *uint64
- func (cmd *Cmd) Uint64P(target *uint64, long string, short rune, defaultValue uint64, help ...string)
- func (cmd *Cmd) Uint8(long string, short rune, defaultValue uint8, help ...string) *uint8
- func (cmd *Cmd) Uint8P(target *uint8, long string, short rune, defaultValue uint8, help ...string)
- func (cmd *Cmd) UintP(target *uint, long string, short rune, defaultValue uint, help ...string)
- type CmdCallback
- type CustomArg
- type ErrCallback
- type HelpCallback
- type MatchCallback
- type Options
- type RunCallback
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func IsParserErr ¶
IsParserErr returns true is the error is an error generated by the parsing process itself.
Types ¶
type App ¶
type App struct {
*Cmd
}
App defines the main application parser. An application can define one or more command-line arguments to parse, as well as define a chain of subcommands supported by the application.
To get a new instance of App, use the NewApp function.
type Cmd ¶
type Cmd struct { // The name to be used to invoke the command Name string // The brief description of the command Brief string // The long description of the command Long string // The text to be shown at the 'Usage' line of the help. // Set this value to "-" to omit the usage line Usage string // Options for this command Options Options // contains filtered or unexported fields }
Cmd defines a (sub)command of the application. Since commands cannot do much by themselves, you should create your commands by calling the Command method in the App instance.
Subcommands can be created by calling Command of an existing command.
func (*Cmd) AddOperand ¶
AddOperand documents an expected operand. The modifier parameter can be either '?' for optional operands or '*' for repeating ones. The documentation is printed in the order that was used to add the operands, so it is advisable to put them in an order that makes sense for the user (required, optional and repeating, in this order).
Note that this modifier is used only for documentation purposes; no special validation is done, except by the one documented in Options.StrictOperands.
func (*Cmd) Bool ¶
Bool defines a new bool argument. After parsing, the argument value will be available in the returned pointer.
func (*Cmd) BoolP ¶
BoolP defines a new bool argument. After parsing, the argument value will be available in the specified pointer.
func (*Cmd) Choice ¶
func (cmd *Cmd) Choice(choices []string, long string, short rune, defaultValue string, help ...string) *string
Choice defines a new string argument, ith tha values limited by choices. After parsing, the argument value will be available in the returned pointer.
If the defaultValue is always considered 'valid', even when not listed on the choices parameter.
func (*Cmd) ChoiceP ¶
func (cmd *Cmd) ChoiceP(target *string, choices []string, long string, short rune, defaultValue string, help ...string)
ChoiceP defines a new string argument, ith tha values limited by choices. After parsing, the argument value will be available in the specified pointer.
If the defaultValue is always considered 'valid', even when not listed on the choices parameter.
func (*Cmd) Command ¶
func (cmd *Cmd) Command(name, brief string, callback CmdCallback)
Command defines a new subcommand. The name is mandatory (otherwise, the command will never run), and the callback function gives the caller the opportunity to configure the newly created command, by defining new flags, subcommands, or specifying what to run when the command is activated.
func (*Cmd) CommandMatch ¶
func (cmd *Cmd) CommandMatch(name, brief string, callback MatchCallback)
CommandMatch is a shortcut to Command() followed by Match() on the provided command.
func (*Cmd) CommandRun ¶
func (cmd *Cmd) CommandRun(name, brief string, callback RunCallback)
CommandRun is a shortcut to Command() followed by Run() on the provided command.
func (*Cmd) CustomP ¶
func (cmd *Cmd) CustomP(target CustomArg, long string, short rune, defaultValue string, help ...string)
CustomP defines a new argument with custom type. During parsing, the argument is manipulated via Get and Set methods of the CustomArg interface.
func (*Cmd) Err ¶
func (cmd *Cmd) Err(handler ErrCallback)
Err registers a handler to be run when the parsing fails.
func (*Cmd) Float32 ¶
Float32 defines a new float32 argument. After parsing, the argument value will be available in the returned pointer.
func (*Cmd) Float32P ¶
func (cmd *Cmd) Float32P(target *float32, long string, short rune, defaultValue float32, help ...string)
Float32P defines a new float32 argument. After parsing, the argument value will be available in the specified pointer.
func (*Cmd) Float64 ¶
Float64 defines a new float64 argument. After parsing, the argument value will be available in the returned pointer.
func (*Cmd) Float64P ¶
func (cmd *Cmd) Float64P(target *float64, long string, short rune, defaultValue float64, help ...string)
Float64P defines a new float64 argument. After parsing, the argument value will be available in the specified pointer.
func (*Cmd) GetBool ¶
GetBool returns the bool pointer used as value for the argument 'name' (you can use either the short or long name). If the argument does not exist, this routine panics.
func (*Cmd) GetChoice ¶
GetChoice returns the string pointer used as value for the argument 'name' (you can use either the short or long name). If the argument does not exist, this routine panics.
func (*Cmd) GetCustom ¶
GetCustom returns the CustomArg value used as value for the argument 'name' (you can use either the short or long name). If the argument does not exist, this routine panics.
func (*Cmd) GetFloat32 ¶
GetFloat32 returns the float32 pointer used as value for the argument 'name' (you can use either the short or long name). If the argument does not exist, this routine panics.
func (*Cmd) GetFloat64 ¶
GetFloat64 returns the float64 pointer used as value for the argument 'name' (you can use either the short or long name). If the argument does not exist, this routine panics.
func (*Cmd) GetInt ¶
GetInt returns the int pointer used as value for the argument 'name' (you can use either the short or long name). If the argument does not exist, this routine panics.
func (*Cmd) GetInt16 ¶
GetInt16 returns the int16 pointer used as value for the argument 'name' (you can use either the short or long name). If the argument does not exist, this routine panics.
func (*Cmd) GetInt32 ¶
GetInt32 returns the int32 pointer used as value for the argument 'name' (you can use either the short or long name). If the argument does not exist, this routine panics.
func (*Cmd) GetInt64 ¶
GetInt64 returns the int64 pointer used as value for the argument 'name' (you can use either the short or long name). If the argument does not exist, this routine panics.
func (*Cmd) GetInt8 ¶
GetInt8 returns the int8 pointer used as value for the argument 'name' (you can use either the short or long name). If the argument does not exist, this routine panics.
func (*Cmd) GetString ¶
GetString returns the string pointer used as value for the argument 'name' (you can use either the short or long name). If the argument does not exist, this routine panics.
func (*Cmd) GetUint ¶
GetUint returns the uint pointer used as value for the argument 'name' (you can use either the short or long name). If the argument does not exist, this routine panics.
func (*Cmd) GetUint16 ¶
GetUint16 returns the uint16 pointer used as value for the argument 'name' (you can use either the short or long name). If the argument does not exist, this routine panics.
func (*Cmd) GetUint32 ¶
GetUint32 returns the uint32 pointer used as value for the argument 'name' (you can use either the short or long name). If the argument does not exist, this routine panics.
func (*Cmd) GetUint64 ¶
GetUint64 returns the uint64 pointer used as value for the argument 'name' (you can use either the short or long name). If the argument does not exist, this routine panics.
func (*Cmd) GetUint8 ¶
GetUint8 returns the uint8 pointer used as value for the argument 'name' (you can use either the short or long name). If the argument does not exist, this routine panics.
func (*Cmd) Help ¶
func (cmd *Cmd) Help()
Help prints the help text in the stdout. Normally, this method is not manually called, since it will be automatically executed on the following scenarios:
- When '-h' or '--help' is used;
- In a multi-command scenario, when a given command has subcommands but does not have a Run callback;
In the case defined by (1), you can suppress this behavior by setting the option SupressPrintHelpWhenSet. This will not call the help function when '-h' or '--help' is passed, but the flags will still be defined if you have at least one more flag defined. To suppress even the '-h' and '--help' flags creation, set the SuppressHelpFlag option.
In the sceenario defined by (2), you can either define a Run callback for the command or set the SuppressPrintHelpPartialCommand configuration option.
Last, but not least, if you need to override the actual text of the help, set the OnHelp field of the App options instance.
func (*Cmd) Int ¶
Int defines a new int argument. After parsing, the argument value will be available in the returned pointer.
func (*Cmd) Int16 ¶
Int16 defines a new int16 argument. After parsing, the argument value will be available in the returned pointer.
func (*Cmd) Int16P ¶
Int16P defines a new int16 argument. After parsing, the argument value will be available in the specified pointer.
func (*Cmd) Int32 ¶
Int32 defines a new int32 argument. After parsing, the argument value will be available in the returned pointer.
func (*Cmd) Int32P ¶
Int32P defines a new int32 argument. After parsing, the argument value will be available in the specified pointer.
func (*Cmd) Int64 ¶
Int64 defines a new int64 argument. After parsing, the argument value will be available in the returned pointer.
func (*Cmd) Int64P ¶
Int64P defines a new int64 argument. After parsing, the argument value will be available in the specified pointer.
func (*Cmd) Int8 ¶
Int8 defines a new int8 argument. After parsing, the argument value will be available in the returned pointer.
func (*Cmd) Int8P ¶
Int8P defines a new int8 argument. After parsing, the argument value will be available in the specified pointer.
func (*Cmd) IntP ¶
IntP defines a new int argument. After parsing, the argument value will be available in the specified pointer.
func (*Cmd) Match ¶
func (cmd *Cmd) Match(callback MatchCallback)
Match registers a callback to run when this command matches. A command matches if it is invoked and the parsing is successful.
func (*Cmd) Operand ¶
Operand returns the value of the named operand, if any. When specified using AddOperand, each unparsed arg is considered an operand and it's value is fetched - but not consumed - from the Args() method.
The behavior of this function is only guaranteed when used in a 'leaf' command or and Run() callback.
func (*Cmd) PrintHelp ¶
PrintHelp prints the help text to the specified writer. It functions exactly like the Help method.
func (*Cmd) Run ¶
func (cmd *Cmd) Run(callback RunCallback)
Run registers a callback to run when this command is matched (see Match) and no more subcommands were invoked.
func (*Cmd) String ¶
String defines a new string argument. After parsing, the argument value will be available in the returned pointer.
func (*Cmd) StringP ¶
func (cmd *Cmd) StringP(target *string, long string, short rune, defaultValue string, help ...string)
StringP defines a new string argument. After parsing, the argument value will be available in the specified pointer.
func (*Cmd) Uint ¶
Uint defines a new uint argument. After parsing, the argument value will be available in the returned pointer.
func (*Cmd) Uint16 ¶
Uint16 defines a new uint16 argument. After parsing, the argument value will be available in the returned pointer.
func (*Cmd) Uint16P ¶
func (cmd *Cmd) Uint16P(target *uint16, long string, short rune, defaultValue uint16, help ...string)
Uint16P defines a new uint16 argument. After parsing, the argument value will be available in the specified pointer.
func (*Cmd) Uint32 ¶
Uint32 defines a new uint32 argument. After parsing, the argument value will be available in the returned pointer.
func (*Cmd) Uint32P ¶
func (cmd *Cmd) Uint32P(target *uint32, long string, short rune, defaultValue uint32, help ...string)
Uint32P defines a new uint32 argument. After parsing, the argument value will be available in the specified pointer.
func (*Cmd) Uint64 ¶
Uint64 defines a new uint64 argument. After parsing, the argument value will be available in the returned pointer.
func (*Cmd) Uint64P ¶
func (cmd *Cmd) Uint64P(target *uint64, long string, short rune, defaultValue uint64, help ...string)
Uint64P defines a new uint64 argument. After parsing, the argument value will be available in the specified pointer.
func (*Cmd) Uint8 ¶
Uint8 defines a new uint8 argument. After parsing, the argument value will be available in the returned pointer.
type CmdCallback ¶
type CmdCallback func(cmd *Cmd)
CmdCallback is a routine that runs when new command is configured. If the command don't need to define new arguments or subcommands, you can safely run your custom code directly in this callback.
type CustomArg ¶
type CustomArg interface { Get() string Set(value string) error TypeName() string Explain(template string) string }
CustomArg is the interface used to create customized argument types. As long as you can read and write to a string, you can use this.
TypeName is the name to be used to represent this type on help messages (e. g. int, string, value, foo). This will only be used if the user does not supply a sencond help message.
Explain is a (optional) short string describing the custom type. This method receives a template string(that might be empty) in Printf style and 'injects' the type explanation on it. It may return different results based on the existence or not of a template string.
Note that a empty string ("") is assumed to be the zero value of your custom type
type ErrCallback ¶
ErrCallback is a callback that executes when the parser encounters an error. If you wish to recover (or ignore) the error, return a nil value to force the parser to continue it's normal process.
type HelpCallback ¶
HelpCallback is a callback that allows the user to customize the help text.
type MatchCallback ¶
type MatchCallback func(cmd *Cmd)
MatchCallback is a callback that is run when a command matches and and the parsing happens without raising an error. You can safely assume that the argument values are correctly loaded; however, since this callback runs before the subcommand processing occurs, this may not be the 'final' command to be executed by the user.
To run code when the final command is selected, take a look at the Run function and the RunCallback type.
type Options ¶
type Options struct { // When true, the automatic creation of help flags will // be suppressed. SuppressHelpFlag bool // When true, do not print the help automatically when a // help flag is set SupressPrintHelpWhenSet bool // When true, do not print the help automatically when a command with // subcommands and without a Run callback is executed SuppressPrintHelpPartialCommand bool // When true, returns an parsing error when the number of operands does not match // the expected number of required operands (optional and repeated operands are ignored). StrictOperands bool // When set, redirect the help output to the specified writer. // When it is nil, the help text will be printed to Stdout HelpOutput io.Writer // Function that overrides the auto-generated help text. OnHelp HelpCallback }
Options defines the configuration options of the main App instance. The zero value of this struct reflect the default set of options.
type RunCallback ¶
RunCallback is a callback that runs when a specified command is invoked. This callback only runs if the parsing did succeed.
You can safely assume that all parsed args are correctly loaded and no other subcommands are pending. If you return an error value in this callback, it will be available as a return to the Run or RunArgs on the App instance.