Documentation ¶
Overview ¶
Package flagex implements a command line parser. Not thread-safe.
Index ¶
- Variables
- type Flag
- func (f *Flag) Default() string
- func (f *Flag) Excl() bool
- func (f *Flag) Help() string
- func (f *Flag) Key() string
- func (f *Flag) Kind() FlagKind
- func (f *Flag) ParamHelp() string
- func (f *Flag) Parsed() bool
- func (f *Flag) ParsedVal() bool
- func (f *Flag) SetDefault(defval string)
- func (f *Flag) SetHelp(help string)
- func (f *Flag) SetParamHelp(help string)
- func (f *Flag) Shortkey() string
- func (f *Flag) Sub() *Flags
- func (f *Flag) Value() string
- type FlagKind
- type Flags
- func (f *Flags) Define(key, shortkey, help, paramhelp, defval string, typ FlagKind) (err error)
- func (f *Flags) DefineOptional(key, shortkey, help, paramhelp, defval string) (err error)
- func (f *Flags) DefineRequired(key, shortkey, help, paramhelp, defval string) (err error)
- func (f *Flags) DefineSub(key, shortkey, help string, sub *Flags) error
- func (f *Flags) DefineSwitch(key, shortkey, help string) (err error)
- func (f *Flags) GetKey(key string) (flag *Flag, truth bool)
- func (f *Flags) GetShort(shortkey string) (flag *Flag, truth bool)
- func (f *Flags) GetValue(key string) string
- func (f *Flags) Parse(args []string) error
- func (f *Flags) ParseMap() map[interface{}]interface{}
- func (f *Flags) Parsed(keys ...string) bool
- func (f *Flags) SetExclusive(keys ...string) error
- func (f *Flags) String() string
Constants ¶
This section is empty.
Variables ¶
var ( // ErrFlagex is the base flagex error. ErrFlagex = errorex.New("flagex") // ErrNoArgs is returned when Parse is called with empty arguments. ErrNoArgs = ErrFlagex.Wrap("no arguments") // ErrInvalid is returned when an invalid flag key is specified. ErrInvalid = ErrFlagex.WrapFormat("invalid key") // ErrNotFound is returned when a non existent key is requested. ErrNotFound = ErrFlagex.WrapFormat("key '%s' not found") // ErrDuplicate is returned when a flag with a duplicate key is being // registered. ErrDuplicate = ErrFlagex.WrapFormat("duplicate key '%s'") // ErrDupShort is returned when a flag with a duplicate shortkey is being // registered. ErrDupShort = ErrFlagex.WrapFormat("duplicate shortkey '%s'") // ErrExclusive is returned when a more than one flag from an exclusive set // is parsed. ErrExclusive = ErrFlagex.WrapFormat("'%s' is exclusive to '%s'") // ErrRequired is returned when a required flag was not parsed. ErrRequired = ErrFlagex.WrapFormat("required key '%s' not specified") // ErrReqVal is returned when no value was passed to a key that requires // one. ErrReqVal = ErrFlagex.WrapFormat("arg '%s' requires a param.") // ErrSwitch is returned when a switch was passed a param. ErrSwitch = ErrFlagex.WrapFormat("switch '%s' takes no params") // ErrSub is returned when a sub switch was parsed with no args following // it. ErrSub = ErrFlagex.WrapFormat("sub '%s' invoken with no params") // ErrNotSub is returned when a non-sub switch is combined with other // commands. ErrNotSub = ErrFlagex.WrapFormat("cannot combine key '%s', not a sub.") )
Functions ¶
This section is empty.
Types ¶
type Flag ¶
type Flag struct {
// contains filtered or unexported fields
}
Flag represents flag defined in Flags.
func (*Flag) SetDefault ¶
SetDefault sets flag's default value.
func (*Flag) SetParamHelp ¶
SetParamHelp sets flag's param help text.
type FlagKind ¶
type FlagKind byte
FlagKind specifies Flag kind.
const ( // KindOptional marks a flag as optional as well as its params. KindOptional FlagKind = iota // KindRequired marks a flag as required as well as is its params. KindRequired // KindSwitch marks a flag as optional that takes no params. // Flags with sub flags take no flags and are marked as KindSwitch. KindSwitch // KindSub marks a flag as a Flags subcategory prefix. KindSub )
type Flags ¶
type Flags struct {
// contains filtered or unexported fields
}
Flags holds a set of unique flags.
func (*Flags) Define ¶ added in v0.2.0
Define defines a flag under specified key and optional longkey with specified help and default value defval. key and shortkey must be unique in Flags, shortkey is optional. If a non-nil error is returned flag was not defined.
func (*Flags) DefineOptional ¶ added in v0.2.0
DefineOptional defines an optional flag with a required param.
func (*Flags) DefineRequired ¶ added in v0.2.0
DefineRequired defines a required flag with a required param.
func (*Flags) DefineSub ¶ added in v0.2.0
DefineSub defines child Flags under specified key and optional shortkey which must be unique in these Flags. When invoken rest of params are passed to it. help defines the flag help. If a non-nil error is returned flag was not defined.
func (*Flags) DefineSwitch ¶ added in v0.2.0
DefineSwitch defines an optional switch without a param.
func (*Flags) GetKey ¶ added in v0.2.0
GetKey returns Flag if under specified key and a truth if it exists.
func (*Flags) GetShort ¶ added in v0.2.0
GetShort returns Flag under specified shortkey and a truth if it exists.
func (*Flags) GetValue ¶ added in v0.2.0
GetValue will return current value of a key, if found. Returns an empty string otherwise. Check before if key was parsed with Parsed().
func (*Flags) ParseMap ¶
func (f *Flags) ParseMap() map[interface{}]interface{}
ParseMap returns a map of parsed Flag key:value pairs. Sub will return a map, Flags may return a string if parsed or nil if not parsed. ParseMap returns whichever args were parsed at last Parse. ParseMap is as valid as what Parse returned.
func (*Flags) Parsed ¶
Parsed returns if flags were parsed if no keys are specified. If one or more keys are specified, returns if all of the specified keys were specified and parsed.
func (*Flags) SetExclusive ¶ added in v0.2.0
SetExclusive sets specified keys as mutually exclusive in Flags. If more than one key from exclusive group are parsed, parse will error. Keys must already be defined. Subsequent calls redefine exclusivity.