Documentation ¶
Overview ¶
Package envflag facilitates setting standard Go flags using environment variables.
Basic usage ¶
Simply replace the typical call to flag.Parse() with envflag.Parse(). Flag names are mapped to environment variables by converting them to uppercase, and replacing dashes with underscores (e.g. `my-flag` => `MY_FLAG`). Command-line arguments will take precedence over environment variables where both are specified.
Advanced usage ¶
You can customise the behaviour of envflag by passing in options to the Parse() method. For example, to add a prefix to all environment variables:
envflag.Parse(envflag.WithPrefix("MYAPP_"))
This will map a flag named `my-flag` to the environment variable `MYAPP_MY_FLAG`.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Parse ¶
func Parse(opts ...Option)
Parse parses flag values from the environment and the arguments list. Must be called after all flags are defined and before flags are accessed by the program.
The parsing behaviour can be customised by passing one or more options, but will use sensible defaults if no options are passed.
If an error occurs trying to set a flag, error and usage information will be printed to the flag.FlagSet's Output and os.Exit(2) will be called. This is the same behaviour as flag.ExitOnError, which is the default behaviour for the flag.CommandLine set.
NOTE: This method will call flag.Parse(). Callers do not need to call it directly.
Types ¶
type Option ¶
type Option func(*config)
Option defines a configuration option for the envflag package.
func WithArguments ¶
WithArguments specifies the arguments that should be parsed into flags. If a flag is specified in both an environment variable and in the given arguments, the one in the arguments will be used. Defaults to the command-line arguments (os.Args[1:]).
func WithFlagSet ¶
WithFlagSet specifies which FlagSet envflag should operate on. If not specified, defaults to flag.CommandLine.
func WithPrefix ¶
WithPrefix alters the mapping of flag names to environment variables so that they are prefixed with the given string. If not specified, no prefix is used.
func WithShowInUsage ¶
WithShowInUsage specifies whether the name of environment variables should be prepended to the usage text of each flag. If not specified, defaults to true.