Documentation ¶
Overview ¶
Package clapper processes the command-line arguments of getopt(3) syntax. This package provides the ability to process the root command, sub commands, command-line arguments and command-line flags.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Arg ¶
type Arg struct { // name of the argument Name string // variadic argument can take multiple values IsVariadic bool // default value of the argument DefaultValue string // value of the argument (provided by the user) Value string }
Arg type holds the structured information about an argument.
type CommandConfig ¶ added in v1.0.8
type CommandConfig struct { // name of the sub-command ("" for the root command) Name string // command-line flags Flags map[string]*Flag // registered command argument values Args map[string]*Arg // list of the argument names (for ordered iteration) ArgNames []string // contains filtered or unexported fields }
CommandConfig type holds the structure and values of the command-line arguments of command.
func (*CommandConfig) AddArg ¶ added in v1.0.8
func (commandConfig *CommandConfig) AddArg(name string, defaultValue string) (*Arg, bool)
AddArg registers an argument configuration with the command. The `name` argument represents the name of the argument. If value of the `name` argument ends with `...` suffix, then it is a variadic argument. Variadic argument can accept multiple argument values and it should be the last registered argument. Values of a variadic argument will be concatenated using comma (,). The `defaultValue` argument represents the default value of the argument. All arguments without a default value must be registered first. If an argument with given `name` is already registered, then argument registration is skipped and registered `*Arg` object returned. If the argument is already registered, second return value will be `true`.
func (*CommandConfig) AddFlag ¶ added in v1.0.8
func (commandConfig *CommandConfig) AddFlag(name string, shortName string, isBool bool, defaultValue string) (*Flag, bool)
AddFlag method registers a command-line flag with the command. The `name` argument is the long-name of the flag and it should not start with `--` prefix. The `shortName` argument is the short-name of the flag and it should not start with `-` prefix. The `isBool` argument indicates whether the flag holds a boolean value. A boolean flag doesn't accept an input value such as `--flag=<value>` and its default value is "true". The `defaultValue` argument represents the default value of the flag. In case of a boolean flag, the `defaultValue` is redundant. If the `name` value starts with `no-` prefix, then it is considered as an inverted flag. An inverted flag is registered with the name `<flag>` produced by removing `no-` prefix from `no-<flag>` and its defaut value is "true". When command-line arguments contain `--no-<flag>`, the value of the `<flag>` becomes "false". If a flag with given `name` is already registered, then flag registration is skipped and registered `*Flag` object returned. If the flag is already registered, second return value will be `true`.
type ErrorUnknownCommand ¶
type ErrorUnknownCommand struct {
Name string
}
ErrorUnknownCommand represents an error when command-line arguments contain an unregistered command.
func (ErrorUnknownCommand) Error ¶
func (e ErrorUnknownCommand) Error() string
type ErrorUnknownFlag ¶
type ErrorUnknownFlag struct {
Name string
}
ErrorUnknownFlag represents an error when command-line arguments contain an unregistered flag.
func (ErrorUnknownFlag) Error ¶
func (e ErrorUnknownFlag) Error() string
type ErrorUnsupportedFlag ¶ added in v1.0.3
type ErrorUnsupportedFlag struct {
Name string
}
ErrorUnsupportedFlag represents an error when command-line arguments contain an unsupported flag.
func (ErrorUnsupportedFlag) Error ¶ added in v1.0.3
func (e ErrorUnsupportedFlag) Error() string
type Flag ¶
type Flag struct { // long name of the flag Name string // short name of the flag ShortName string // if the flag holds boolean value IsBoolean bool // if the flag is an inverted flag (with `--no-` prefix) IsInverted bool // default value of the flag DefaultValue string // value of the flag (provided by the user) Value string }
Flag type holds the structured information about a flag.
type Registry ¶
type Registry map[string]*CommandConfig
Registry holds the configuration of the registered commands.
func (Registry) Parse ¶
func (registry Registry) Parse(values []string) (*CommandConfig, error)
Parse method parses command-line arguments and returns an appropriate "*CommandConfig" object registered in the registry. If command is not registered, it return `ErrorUnknownCommand` error. If there is an error parsing a flag, it can return an `ErrorUnknownFlag` or `ErrorUnsupportedFlag` error.
func (Registry) Register ¶
func (registry Registry) Register(name string) (*CommandConfig, bool)
Register method registers a command. The "name" argument should be a simple string. If "name" is an empty string, it is considered as a root command. If a command is already registered, the registered `*CommandConfig` object is returned. If the command is already registered, second return value will be `true`.