Documentation ¶
Index ¶
- Variables
- func DefaultUsage(flagSet *FlagSet)
- func GetIgnoredArgs() []string
- func Parse() error
- func PrintDefaults()
- func PrintFlagSetDefaults(flagSet *FlagSet)
- func SetAllowParsingMultipleAliases(allow bool)
- func SetIgnoreUnknown(ignore bool)
- func SetIgnoreUnknownAmbiguousAsBoolFlags(treatAsBool bool)
- func StructVar(p any, ignoredFields ...any) error
- func StructVarWithPrefix(p any, flagsPrefix string, ignoredFields ...any) error
- type FlagSet
- func (fls *FlagSet) GetIgnoredArgs() []string
- func (fls *FlagSet) Parse(arguments []string) error
- func (fls *FlagSet) PrintDefaults()
- func (fls *FlagSet) SetAllowParsingMultipleAliases(allow bool)
- func (fls *FlagSet) SetIgnoreUnknown(ignore bool)
- func (fls *FlagSet) SetIgnoreUnknownAmbiguousAsBoolFlags(treatAsBool bool)
- func (fls *FlagSet) StructVar(p any, ignoredFields ...any) error
- func (fls *FlagSet) StructVarWithPrefix(p any, flagsPrefix string, ignoredFields ...any) (err error)
Constants ¶
This section is empty.
Variables ¶
var CommandLine = Wrap(flag.CommandLine)
CommandLine is a default FlagSet that is used by the package functions. It's a wrapper around flag.CommandLine to follow the same pattern as in the stdlib.
var ErrFlagRedefined = errors.New("flag redefined")
var ErrIsRequired = errors.New("flag is required")
var ErrMultipleAliases = errors.New("multiple aliases for the same flag are used")
var Usage = func() { printUsageTitle(CommandLine.FlagSet, os.Args[0]) PrintDefaults() }
Functions ¶
func DefaultUsage ¶
func DefaultUsage(flagSet *FlagSet)
DefaultUsage prints the default FlagSet usage to flagSet.Output grouping alternative flag names
func GetIgnoredArgs ¶ added in v1.0.1
func GetIgnoredArgs() []string
GetIgnoredArgs returns a slice of arguments that were ignored during the last call to Parse() because of SetIgnoreUnknown(true), nil otherwise
func PrintDefaults ¶
func PrintDefaults()
PrintDefaults prints the default FlagSet usage to stdout grouping alternative flag names
func PrintFlagSetDefaults ¶
func PrintFlagSetDefaults(flagSet *FlagSet)
PrintFlagSetDefaults prints flag names and usage grouping alternative flag names
func SetAllowParsingMultipleAliases ¶
func SetAllowParsingMultipleAliases(allow bool)
SetAllowParsingMultipleAliases sets the behavior of Parse() when multiple tag names assigned to same field are passed. If `true`, it will be ignored and only the last value will be used. If `false`, Parse() will return an error. Default value is `false`.
func SetIgnoreUnknown ¶ added in v1.0.0
func SetIgnoreUnknown(ignore bool)
SetIgnoreUnknown sets the behavior of Parse() when unknown flags are passed. If `true`, they will be ignored. If `false`, Parse() will return an error. Default value is `false`.
func SetIgnoreUnknownAmbiguousAsBoolFlags ¶ added in v1.1.0
func SetIgnoreUnknownAmbiguousAsBoolFlags(treatAsBool bool)
SetIgnoreUnknownAmbiguousAsBoolFlags sets the behavior of Parse() when unknown flags are passed, and they are ambiguous with known bool flags. If `true`, they will be treated as bool flags. If `false`, Parse() will return an error. Default value is `false`.
func StructVar ¶
StructVar registers the given struct with the default FlagSet `ignoredFields` is a slice of pointers to fields that should be ignored and not registered as flags See FlagSet.StructVar
func StructVarWithPrefix ¶
StructVarWithPrefix registers the given struct with the default FlagSet using the given prefix for flag names. `ignoredFields` is a slice of pointers to fields that should be ignored and not registered as flags See FlagSet.StructVarWithPrefix
Types ¶
type FlagSet ¶
FlagSet is a wrapper around *flag.FlagSet that allows to register structs parsing their fields as flags.
func NewFlagSet ¶
func NewFlagSet(name string, errorHandling flag.ErrorHandling) *FlagSet
NewFlagSet creates a new FlagSet wrapping new flag.FlagSet with the given name and error handling policy and assigns its Usage to the own implementation that groups alternative flag names
func Wrap ¶
Wrap creates a new FlagSet wrapping the given `stdFlagSet` and does not set stdFlagSet.Usage
func (*FlagSet) GetIgnoredArgs ¶ added in v1.0.1
GetIgnoredArgs returns a slice of arguments that were ignored during the last call to Parse() because of SetIgnoreUnknown(true), nil otherwise
func (*FlagSet) Parse ¶
Parse parses the command-line flags calling Parse on the wrapped FlagSet and then sets values of the registered structs fields for flags that were actually parsed.
func (*FlagSet) PrintDefaults ¶
func (fls *FlagSet) PrintDefaults()
PrintDefaults prints the default FlagSet usage to wrapped FlagSet.Output grouping alternative flag names
func (*FlagSet) SetAllowParsingMultipleAliases ¶
SetAllowParsingMultipleAliases sets the behavior of Parse() when multiple tag names assigned to same field are passed. If `true`, it will be ignored and only the last value will be used. If `false`, Parse() will return an error. Default value is `false`.
func (*FlagSet) SetIgnoreUnknown ¶ added in v1.0.0
SetIgnoreUnknown sets the behavior of Parse() when unknown flags are passed. If `true`, they will be ignored. If `false`, Parse() will return an error. Default value is `false`.
func (*FlagSet) SetIgnoreUnknownAmbiguousAsBoolFlags ¶ added in v1.1.0
SetIgnoreUnknownAmbiguousAsBoolFlags specifies behaviour of stripping ambiguous unknown flags if SetIgnoreUnknown(true) is set. Unknown flag without inline value can be treated as bool flag or as flag name that should be followed by value. If the flag is unknown, we can't know if it's bool flag or not. Example: "-a -b" can be treated as: 1. {a: "-b"} - id - if treatAsBool == false OR 2. {a: true, b: true} - if treatAsBool == true Default values is `false`. It doesn't affect flags from fields that are defined in a registered struct but were passed as ignored to Parse() call. Information about their type will be used to determine if they are bool flags or not.
func (*FlagSet) StructVar ¶
StructVar registers the fields of the given struct as a flags `ignoredFields` is a slice of pointers to fields that should be ignored and not registered as flags
func (*FlagSet) StructVarWithPrefix ¶
func (fls *FlagSet) StructVarWithPrefix(p any, flagsPrefix string, ignoredFields ...any) (err error)
StructVarWithPrefix registers the fields of the given struct as a flags with names prefixed with `flagsPrefix` `ignoredFields` is a slice of pointers to fields that should be ignored and not registered as flags