You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This is a simple library that allows that enables reading the value of flags from environment variables.
Why?
The Go flags package is well-thought-out, battle-tested, and extensively used, but modern
deployment practices tend to favour storing configuration in environment variables rather
than passing them in on the command-line.
envflag and its predecessors bridge this gap by mapping environment variables onto flags.
How?
Import this package, and use envflag.Parse instead of flag.Parse:
package main
import (
"flag""github.com/csmith/envflag/v2"
)
var (
myFlag=flag.String("my-flag", "woohoo", "Something or other")
)
funcmain() {
envflag.Parse()
println(*myFlag)
}
What?
In its default configuration, envflag will:
Map all flag names to appropriate environment variable names (my-flag -> MY_FLAG)
Update the usage information of flags to include the environment variable name
Show usage and exit with an appropriate status code on error
Use the default set of flags provided by flag.CommandLine
Call flag.Parse to also parse any command-line flags
You can customise the behaviour of envflag by passing in options:
Prefix all environment variables
envflag.Parse(envflag.WithPrefix("MYAPP_"))
Changes the mapping of environment variables to always include the given prefix
(e.g. my-flag -> MYAPP_MY_FLAG).
Use a different flag set
envflag.Parse(envflag.WithFlagSet(someFlagSet))
Use the given flag.FlagSet instead of flag.CommandLine.
Don't show environment variable names in usage
envflag.Parse(envflag.WithShowInUsage(false))
Suppresses the default behaviour of updating flag usage to include the environment variable names.