Environment Variables
By default, goopt will not consider environment variables when parsing. In order to enable this, you need to set the SetEnvNameConverter function which will convert the environment variable name to the flag name.
Unless explicitly specified, goopt assumes that flag names are in local camel case format. Setting the SetEnvNameConverter will do the following:
- Convert an environment variable name to using the output of the
SetEnvNameConverterfunction - Checks the configuration for the flag name and if it exists, use the value from the environment variable.
Variables are evaluated in the following order (from highest precedence to lowest):
- command line flags
- flag defaults from external sources (such as JSON, YAML, etc.) set via the map supplied to the
ParseWithDefaultsfunction - environment variables
- defaults set via the
WithDefaultValuefunction or via struct tag annotations
Usage
parser.SetEnvNameConverter(func(s string) string {
return DefaultFlagNameConverter(s) // DefaultFlagNameConverter is the default implementation and converts ENV var names to lowerCamelCase
})