Documentation
¶
Overview ¶
Package opt is a simple, opinionated, options wrapper for pflag & cobra.
It aims to be a simpler alternative to viper.
By default, all options can be specified as:
- Default values (lowest precedent)
- Environment variables
- Command-line flags (highest precedent)
All commands have a name, which is used to set default values.
The default environment variable name is the option's name, but in upper case (see strings.ToUpper). To disallow an option from being specified as an environment variable, override it environment variable name to be "" using EnvName.
The default command-line flag name is the option's name. By default, flags are added to the cobra.Command's normal cobra.FlagSet. Use FlagIsPersistent to add it to the persistent cobra.FlagSet instead. To disallow an option from being specified as a command-line flag, override its flag name to be "" using FlagName.
Index ¶
- func Bool(cmd *cobra.Command, v *bool, name string, optFuncs ...OptFunc[bool])
- func String(cmd *cobra.Command, v *string, name string, optFuncs ...OptFunc[string])
- type OptFunc
- func Default[T OptType](v T) OptFunc[T]
- func EnvName[T OptType](envName string) OptFunc[T]
- func FlagIsPersistent[T OptType]() OptFunc[T]
- func FlagName[T OptType](flagName string) OptFunc[T]
- func FlagShorthand[T OptType](flagShorthand string) OptFunc[T]
- func IsDirname() OptFunc[string]
- func IsFilename() OptFunc[string]
- type OptType
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type OptFunc ¶ added in v0.0.3
type OptFunc[T OptType] func(*opt[T])
func EnvName ¶ added in v0.0.3
EnvName overrides the default environment variable name.
As stated in the package documentation, if the environment name is not overriden, it defaults to an upper-cased version of the option's name.
To disable environment lookup functionlity entirely for this option, override the environment name to be "".
func FlagIsPersistent ¶ added in v0.0.3
FlagIsPersistent specifies that the flag should be added to the cobra.Command's persistent pflag.FlagSet.
func FlagName ¶ added in v0.0.3
FlagName overrides the default flag name. As stated in the package documentation, if the flag name is not overriden, it defaults to the option's name.
To disable flag functionlity entirely for this option, override the flag name to be "".
func FlagShorthand ¶ added in v0.0.3
FlagShorthand specifies a shorthand that can be used for the flag.
Setting it to "" does nothing.
The shorthand is only applicable when the flag is not disabled. See FlagName's documentation for how to disable flags.
func IsDirname ¶ added in v0.0.3
IsDirname specifies that the option's value should be a directory name. This is only used with shell auto-completion; the caller must validate the actual value before use.
func IsFilename ¶ added in v0.0.3
IsFilename specifies that the option's value should be a file name. This is only used with shell auto-completion; the caller must validate the actual value before use.