Documentation ¶
Overview ¶
Package varflag implements command-line flag parsing into compatible with package https://github.com/happy-sdk/happy/pkg/vars.
Originally based of https://pkg.go.dev/github.com/mkungla/varflag/v5
Index ¶
- Constants
- Variables
- func AsFlag[FLAG FlagIface[VAR, VAL], VAR vars.VariableIface[VAL], VAL vars.ValueIface](in Flag) FLAG
- func NewFlagSetAs[FLAGS FlagsIface[FLAGSET, FLAG, VAR, VAL], ...](name string, argsn int) (FLAGS, error)
- func Parse(flags []Flag, args []string) error
- func SetArgcMax(flags Flags, max int) error
- func ValidFlagName(s string) bool
- type BexpFlag
- type BoolFlag
- type Common
- func (f *Common) Aliases() []string
- func (f *Common) AttachTo(cmdname string)
- func (f *Common) BelongsTo() string
- func (f *Common) Default() vars.Variable
- func (f *Common) Flag() string
- func (f *Common) Global() bool
- func (f *Common) Hidden() bool
- func (f *Common) Hide()
- func (f *Common) Input() []string
- func (f *Common) MarkAsRequired()
- func (f *Common) Name() string
- func (f *Common) Parse(args []string) (bool, error)
- func (f *Common) Pos() int
- func (f *Common) Present() bool
- func (f *Common) Required() bool
- func (f *Common) String() string
- func (f *Common) Unset()
- func (f *Common) Usage() string
- func (f *Common) UsageAliases() string
- func (f *Common) Value() string
- func (f *Common) Var() vars.Variable
- type DurationFlag
- type Flag
- type FlagCreateFunc
- func BoolFunc(name string, value bool, usage string, aliases ...string) FlagCreateFunc
- func DurationFunc(name string, value time.Duration, usage string, aliases ...string) FlagCreateFunc
- func Float64Func(name string, value float64, usage string, aliases ...string) FlagCreateFunc
- func IntFunc(name string, value int, usage string, aliases ...string) FlagCreateFunc
- func OptionFunc(name string, value []string, opts []string, usage string, aliases ...string) FlagCreateFunc
- func StringFunc(name string, value string, usage string, aliases ...string) FlagCreateFunc
- func UintFunc(name string, value uint, usage string, aliases ...string) FlagCreateFunc
- type FlagIface
- type FlagSet
- func (s *FlagSet) AcceptsArgs() bool
- func (s *FlagSet) Add(flag ...Flag) error
- func (s *FlagSet) AddSet(set ...Flags) error
- func (s *FlagSet) Args() []vars.Value
- func (s *FlagSet) Flags() []Flag
- func (s *FlagSet) Get(name string) (Flag, error)
- func (s *FlagSet) GetActiveSets() []Flags
- func (s *FlagSet) Len() int
- func (s *FlagSet) Name() string
- func (s *FlagSet) Parse(args []string) error
- func (s *FlagSet) Pos() int
- func (s *FlagSet) Present() bool
- func (s *FlagSet) Sets() []Flags
- type Flags
- type FlagsIface
- type FlagsSetIface
- type Float64Flag
- type GenericFlag
- func (f *GenericFlag[VAR, VAL]) Aliases() []string
- func (f *GenericFlag[VAR, VAL]) AttachTo(cmdname string)
- func (f *GenericFlag[VAR, VAL]) BelongsTo() string
- func (f *GenericFlag[VAR, VAL]) Default() VAR
- func (f *GenericFlag[VAR, VAL]) Flag() string
- func (f *GenericFlag[VAR, VAL]) Global() bool
- func (f *GenericFlag[VAR, VAL]) Hidden() bool
- func (f *GenericFlag[VAR, VAL]) Hide()
- func (f *GenericFlag[VAR, VAL]) Input() []string
- func (f *GenericFlag[VAR, VAL]) MarkAsRequired()
- func (f *GenericFlag[VAR, VAL]) Name() string
- func (f *GenericFlag[VAR, VAL]) Parse(args []string) (bool, error)
- func (f *GenericFlag[VAR, VAL]) Pos() int
- func (f *GenericFlag[VAR, VAL]) Present() bool
- func (f *GenericFlag[VAR, VAL]) Required() bool
- func (f *GenericFlag[VAR, VAL]) String() string
- func (f *GenericFlag[VAR, VAL]) Unset()
- func (f *GenericFlag[VAR, VAL]) Usage() string
- func (f *GenericFlag[VAR, VAL]) UsageAliases() string
- func (f *GenericFlag[VAR, VAL]) Var() (v VAR)
- type GenericFlagSet
- func (s *GenericFlagSet[FLAGS, FLAGSET, FLAG, VAR, VAL]) AcceptsArgs() bool
- func (s *GenericFlagSet[FLAGS, FLAGSET, FLAG, VAR, VAL]) Add(flag ...FLAG) error
- func (s *GenericFlagSet[FLAGS, FLAGSET, FLAG, VAR, VAL]) AddSet(set ...FLAGS) error
- func (s *GenericFlagSet[FLAGS, FLAGSET, FLAG, VAR, VAL]) Args() []VAL
- func (s *GenericFlagSet[FLAGS, FLAGSET, FLAG, VAR, VAL]) Flags() []FLAG
- func (s *GenericFlagSet[FLAGS, FLAGSET, FLAG, VAR, VAL]) Get(name string) (f FLAG, err error)
- func (s *GenericFlagSet[FLAGS, FLAGSET, FLAG, VAR, VAL]) GetActiveSets() []FLAGSET
- func (s *GenericFlagSet[FLAGS, FLAGSET, FLAG, VAR, VAL]) Len() int
- func (s *GenericFlagSet[FLAGS, FLAGSET, FLAG, VAR, VAL]) Name() string
- func (s *GenericFlagSet[FLAGS, FLAGSET, FLAG, VAR, VAL]) Parse(args []string) error
- func (s *GenericFlagSet[FLAGS, FLAGSET, FLAG, VAR, VAL]) Pos() int
- func (s *GenericFlagSet[FLAGS, FLAGSET, FLAG, VAR, VAL]) Present() bool
- func (s *GenericFlagSet[FLAGS, FLAGSET, FLAG, VAR, VAL]) Sets() []FLAGSET
- type IntFlag
- type OptionFlag
- type UintFlag
Constants ¶
const (
// FlagRe check flag name against following expression.
FlagRe = "^[a-z][a-z0-9_-]*[a-z0-9]$"
)
Variables ¶
var ( // ErrFlag is returned when flag fails to initialize. ErrFlag = errors.New("flag error") // ErrFlag is returned when flag fails to initialize. ErrFlagExists = errors.New("flag already exists") // ErrParse is used to indicate parse errors. ErrParse = errors.New("flag parse error") // ErrMissingValue is used when flag value was not in parsed args. ErrMissingValue = errors.New("missing value for flag") // ErrInvalidValue is used when invalid value was provided. ErrInvalidValue = errors.New("invalid value for flag") // ErrFlagAlreadyParsed is returned when this flag was already parsed. ErrFlagAlreadyParsed = errors.New("flag is already parsed") // ErrMissingRequired indicates that required flag was not in argument set. ErrMissingRequired = errors.New("missing required flag") // ErrMissingOptions is returned when option flag parser does not find options. ErrMissingOptions = errors.New("missing options") // ErrNoNamedFlag is returned when flag lookup can not find named flag. ErrNoNamedFlag = errors.New("no such flag") ErrInvalidArguments = errors.New("invalid arguments") )
var ErrInvalidFlagSetName = errors.New("invalid flag set name")
Functions ¶
func AsFlag ¶
func AsFlag[ FLAG FlagIface[VAR, VAL], VAR vars.VariableIface[VAL], VAL vars.ValueIface, ](in Flag) FLAG
func NewFlagSetAs ¶
func NewFlagSetAs[ FLAGS FlagsIface[FLAGSET, FLAG, VAR, VAL], FLAGSET FlagsSetIface[FLAG, VAR, VAL], FLAG FlagIface[VAR, VAL], VAR vars.VariableIface[VAL], VAL vars.ValueIface, ](name string, argsn int) (FLAGS, error)
NewFlagSet is wrapper to parse flags together. e.g. under specific command. Where "name" is command name to search before parsing the flags under this set. argsn is number of command line arguments allowed within this set. If argsn is -gt 0 then parser will stop after finding argsn+1 argument which is not a flag.
func Parse ¶
Parse parses flags against provided args, If one of the flags fails to parse, it will return error.
func SetArgcMax ¶ added in v0.6.0
func ValidFlagName ¶
ValidFlagName returns true if s is string which is valid flag name.
Types ¶
type BexpFlag ¶
type BexpFlag struct { Common // contains filtered or unexported fields }
BexpFlag expands flag args with bash brace expansion.
type BoolFlag ¶
type BoolFlag struct { Common // contains filtered or unexported fields }
BoolFlag is boolean flag type with default value "false".
type Common ¶
type Common struct {
// contains filtered or unexported fields
}
Common is default string flag. Common flag ccan be used to as base for custom flags by owerriding .Parse func.
func (*Common) AttachTo ¶
AttachTo marks flag non global and belonging to provided named command. Parsing the flag will only succeed if naemd command was found before the flag. This is useful to have same flag both global and sub command level. Special case is .BelongsTo("*") which marks flag to be parsed if any subcommand is present. e.g. verbose flag: you can define 2 BoolFlag's with name "verbose" and alias "v" and mark one of these with BelongsTo("*"). BelongsTo(os.Args[0] | "/") are same as global and will be.
func (*Common) BelongsTo ¶
CommandName returns empty string if command is not set with .BelongsTo When BelongsTo is set to wildcard "*" then this function will return name of the command which triggered this flag to be parsed.
func (*Common) Global ¶
IsGlobal reports whether this flag is global. By default all flags are global flags. You can mark flag non-global by calling .BelongsTo(cmdname string).
func (*Common) MarkAsRequired ¶
func (f *Common) MarkAsRequired()
Required sets this flag as required.
func (*Common) Pos ¶
Pos returns flags position after command and case of global since app name min value is 1 which means first global flag or first flag after command.
func (*Common) UsageAliases ¶
UsageAliases returns string representing flag aliases. e.g. used in help menu.
type DurationFlag ¶ added in v0.10.0
type DurationFlag struct { Common // contains filtered or unexported fields }
type Flag ¶
type Flag interface { // Get primary name for the flag. Usually that is long option Name() string // Get flag default value Default() vars.Variable // Usage returns a usage description for that flag Usage() string // Flag returns flag with leading - or -- // useful for help menus Flag() string // Return flag aliases Aliases() []string UsageAliases() string // IsHidden reports whether to show that flag in help menu or not. Hidden() bool // Hide flag from help menu. Hide() // IsGlobal reports whether this flag was global and was set before any command or arg Global() bool // BelongsTo marks flag non global and belonging to provided named command. AttachTo(cmdname string) // BelongsTo returns empty string if command is not set with .AttachTo // When AttachTo is set to wildcard "*" then this function will return // name of the command which triggered this flag to be parsed. BelongsTo() string // Pos returns flags position after command. In case of mulyflag first position is reported Pos() int // Unset unsets the value for the flag if it was parsed, handy for cases where // one flag cancels another like --debug cancels --verbose Unset() // Present reports whether flag was set in commandline Present() bool // Var returns vars.Variable for this flag. // where key is flag and Value flags value. Var() vars.Variable // Required sets this flag as required MarkAsRequired() // IsRequired returns true if this flag is required Required() bool // Parse value for the flag from given string. It returns true if flag // was found in provided args string and false if not. // error is returned when flag was set but had invalid value. Parse([]string) (bool, error) // String calls Value().String() String() string Input() []string }
Flag is howi/cli/flags.Flags interface.
type FlagCreateFunc ¶
func BoolFunc ¶
func BoolFunc(name string, value bool, usage string, aliases ...string) FlagCreateFunc
func DurationFunc ¶ added in v0.10.0
func Float64Func ¶
func Float64Func(name string, value float64, usage string, aliases ...string) FlagCreateFunc
func OptionFunc ¶
func StringFunc ¶ added in v0.7.0
func StringFunc(name string, value string, usage string, aliases ...string) FlagCreateFunc
type FlagIface ¶
type FlagIface[VAR vars.VariableIface[VAL], VAL vars.ValueIface] interface { Name() string Default() VAR Usage() string Flag() string Aliases() []string UsageAliases() string Hidden() bool Hide() Global() bool AttachTo(cmdname string) BelongsTo() string Pos() int Unset() Present() bool Var() VAR MarkAsRequired() Required() bool Parse([]string) (bool, error) Input() []string }
type FlagSet ¶
type FlagSet struct {
// contains filtered or unexported fields
}
FlagSet holds collection of flags for parsing e.g. global, sub command or custom flag collection.
func NewFlagSet ¶
NewFlagSet is wrapper to parse flags together. e.g. under specific command. Where "name" is command name to search before parsing the flags under this set. argsn is number of command line arguments allowed within this set. If argsn is -gt 0 then parser will stop after finding argsn+1 argument which is not a flag.
func (*FlagSet) AcceptsArgs ¶
Args returns parsed arguments for this flag set.
type Flags ¶
type Flags interface { // Name of the flag set Name() string // Len returns number of flags in this set // not including subset flags. Len() int // Add flag to flag set Add(...Flag) error // Get named flag Get(name string) (Flag, error) // Add sub set of flags to flag set AddSet(...Flags) error // GetActiveSets. GetActiveSets() []Flags // Position of flag set Pos() int // Was flagset (sub command present) Present() bool Args() []vars.Value // AcceptsArgs returns true if set accepts any arguments. AcceptsArgs() bool // Flags returns slice of flags in this set Flags() []Flag // Sets retruns subsets of flags under this flagset. Sets() []Flags // Parse all flags and sub sets Parse(args []string) error }
Flags provides interface for flag set.
type FlagsIface ¶
type FlagsIface[ FLAGS FlagsSetIface[FLAG, VAR, VAL], FLAG FlagIface[VAR, VAL], VAR vars.VariableIface[VAL], VAL vars.ValueIface, ] interface { FlagsSetIface[FLAG, VAR, VAL] GetActiveSets() []FLAGS Sets() []FLAGS }
type FlagsSetIface ¶
type Float64Flag ¶
type Float64Flag struct { Common // contains filtered or unexported fields }
Float64Flag defines a float64 flag with specified name.
func Float64 ¶
func Float64(name string, value float64, usage string, aliases ...string) (flag *Float64Flag, err error)
Float64 returns new float flag. Argument "a" can be any nr of aliases.
func (*Float64Flag) Parse ¶
func (f *Float64Flag) Parse(args []string) (bool, error)
Parse float flag.
func (*Float64Flag) Value ¶
func (f *Float64Flag) Value() float64
Value return float64 flag value, it returns default value if not present or 0 if default is also not set.
type GenericFlag ¶
type GenericFlag[VAR vars.VariableIface[VAL], VAL vars.ValueIface] struct { // contains filtered or unexported fields }
func (*GenericFlag[VAR, VAL]) Aliases ¶
func (f *GenericFlag[VAR, VAL]) Aliases() []string
Return flag aliases
func (*GenericFlag[VAR, VAL]) AttachTo ¶
func (f *GenericFlag[VAR, VAL]) AttachTo(cmdname string)
BelongsTo marks flag non global and belonging to provided named command.
func (*GenericFlag[VAR, VAL]) BelongsTo ¶
func (f *GenericFlag[VAR, VAL]) BelongsTo() string
BelongsTo returns empty string if command is not set with .BelongsTo When BelongsTo is set to wildcard "*" then this function will return name of the command which triggered this flag to be parsed.
func (*GenericFlag[VAR, VAL]) Default ¶
func (f *GenericFlag[VAR, VAL]) Default() VAR
func (*GenericFlag[VAR, VAL]) Flag ¶
func (f *GenericFlag[VAR, VAL]) Flag() string
Flag returns flag with leading - or -- useful for help menus
func (*GenericFlag[VAR, VAL]) Global ¶
func (f *GenericFlag[VAR, VAL]) Global() bool
IsGlobal reports whether this flag was global and was set before any command or arg
func (*GenericFlag[VAR, VAL]) Hidden ¶
func (f *GenericFlag[VAR, VAL]) Hidden() bool
IsHidden reports whether to show that flag in help menu or not.
func (*GenericFlag[VAR, VAL]) Hide ¶
func (f *GenericFlag[VAR, VAL]) Hide()
Hide flag from help menu.
func (*GenericFlag[VAR, VAL]) Input ¶
func (f *GenericFlag[VAR, VAL]) Input() []string
func (*GenericFlag[VAR, VAL]) MarkAsRequired ¶
func (f *GenericFlag[VAR, VAL]) MarkAsRequired()
Required sets this flag as required
func (*GenericFlag[VAR, VAL]) Name ¶
func (f *GenericFlag[VAR, VAL]) Name() string
Get primary name for the flag. Usually that is long option
func (*GenericFlag[VAR, VAL]) Parse ¶
func (f *GenericFlag[VAR, VAL]) Parse(args []string) (bool, error)
Parse value for the flag from given string. It returns true if flag was found in provided args string and false if not. error is returned when flag was set but had invalid value.
func (*GenericFlag[VAR, VAL]) Pos ¶
func (f *GenericFlag[VAR, VAL]) Pos() int
Pos returns flags position after command. In case of mulyflag first position is reported
func (*GenericFlag[VAR, VAL]) Present ¶
func (f *GenericFlag[VAR, VAL]) Present() bool
Present reports whether flag was set in commandline
func (*GenericFlag[VAR, VAL]) Required ¶
func (f *GenericFlag[VAR, VAL]) Required() bool
IsRequired returns true if this flag is required
func (*GenericFlag[VAR, VAL]) String ¶
func (f *GenericFlag[VAR, VAL]) String() string
String calls Value().String()
func (*GenericFlag[VAR, VAL]) Unset ¶
func (f *GenericFlag[VAR, VAL]) Unset()
Unset unsets the value for the flag if it was parsed, handy for cases where one flag cancels another like --debug cancels --verbose
func (*GenericFlag[VAR, VAL]) Usage ¶
func (f *GenericFlag[VAR, VAL]) Usage() string
Usage returns a usage description for that flag
func (*GenericFlag[VAR, VAL]) UsageAliases ¶
func (f *GenericFlag[VAR, VAL]) UsageAliases() string
func (*GenericFlag[VAR, VAL]) Var ¶
func (f *GenericFlag[VAR, VAL]) Var() (v VAR)
type GenericFlagSet ¶
type GenericFlagSet[ FLAGS FlagsIface[FLAGSET, FLAG, VAR, VAL], FLAGSET FlagsSetIface[FLAG, VAR, VAL], FLAG FlagIface[VAR, VAL], VAR vars.VariableIface[VAL], VAL vars.ValueIface, ] struct { // contains filtered or unexported fields }
func (*GenericFlagSet[FLAGS, FLAGSET, FLAG, VAR, VAL]) AcceptsArgs ¶
func (s *GenericFlagSet[FLAGS, FLAGSET, FLAG, VAR, VAL]) AcceptsArgs() bool
func (*GenericFlagSet[FLAGS, FLAGSET, FLAG, VAR, VAL]) Add ¶
func (s *GenericFlagSet[FLAGS, FLAGSET, FLAG, VAR, VAL]) Add(flag ...FLAG) error
func (*GenericFlagSet[FLAGS, FLAGSET, FLAG, VAR, VAL]) AddSet ¶
func (s *GenericFlagSet[FLAGS, FLAGSET, FLAG, VAR, VAL]) AddSet(set ...FLAGS) error
func (*GenericFlagSet[FLAGS, FLAGSET, FLAG, VAR, VAL]) Args ¶
func (s *GenericFlagSet[FLAGS, FLAGSET, FLAG, VAR, VAL]) Args() []VAL
func (*GenericFlagSet[FLAGS, FLAGSET, FLAG, VAR, VAL]) Flags ¶
func (s *GenericFlagSet[FLAGS, FLAGSET, FLAG, VAR, VAL]) Flags() []FLAG
func (*GenericFlagSet[FLAGS, FLAGSET, FLAG, VAR, VAL]) Get ¶
func (s *GenericFlagSet[FLAGS, FLAGSET, FLAG, VAR, VAL]) Get(name string) (f FLAG, err error)
func (*GenericFlagSet[FLAGS, FLAGSET, FLAG, VAR, VAL]) GetActiveSets ¶
func (s *GenericFlagSet[FLAGS, FLAGSET, FLAG, VAR, VAL]) GetActiveSets() []FLAGSET
func (*GenericFlagSet[FLAGS, FLAGSET, FLAG, VAR, VAL]) Len ¶
func (s *GenericFlagSet[FLAGS, FLAGSET, FLAG, VAR, VAL]) Len() int
func (*GenericFlagSet[FLAGS, FLAGSET, FLAG, VAR, VAL]) Name ¶
func (s *GenericFlagSet[FLAGS, FLAGSET, FLAG, VAR, VAL]) Name() string
func (*GenericFlagSet[FLAGS, FLAGSET, FLAG, VAR, VAL]) Parse ¶
func (s *GenericFlagSet[FLAGS, FLAGSET, FLAG, VAR, VAL]) Parse(args []string) error
func (*GenericFlagSet[FLAGS, FLAGSET, FLAG, VAR, VAL]) Pos ¶
func (s *GenericFlagSet[FLAGS, FLAGSET, FLAG, VAR, VAL]) Pos() int
func (*GenericFlagSet[FLAGS, FLAGSET, FLAG, VAR, VAL]) Present ¶
func (s *GenericFlagSet[FLAGS, FLAGSET, FLAG, VAR, VAL]) Present() bool
func (*GenericFlagSet[FLAGS, FLAGSET, FLAG, VAR, VAL]) Sets ¶
func (s *GenericFlagSet[FLAGS, FLAGSET, FLAG, VAR, VAL]) Sets() []FLAGSET
type IntFlag ¶
type IntFlag struct { Common // contains filtered or unexported fields }
IntFlag defines an int flag with specified name,.
type OptionFlag ¶
type OptionFlag struct { Common // contains filtered or unexported fields }
OptionFlag is string flag type which can have value of one of the options.
func Option ¶
func Option(name string, value []string, opts []string, usage string, aliases ...string) (flag *OptionFlag, err error)
Option returns new string flag. Argument "opts" is string slice of options this flag accepts.
func (*OptionFlag) Parse ¶
func (f *OptionFlag) Parse(args []string) (ok bool, err error)
Parse the OptionFlag.
func (*OptionFlag) Usage ¶ added in v0.6.0
func (f *OptionFlag) Usage() string
Usage returns a usage description for that flag.