Documentation ¶
Overview ¶
Package envflag parses command-line flags defined by package flag from the environment.
Usage:
package main import ( "flag" "fmt" "github.com/dcowgill/envflag" ) func main() { listenAddr := flag.String("listen-addr", ":8080", "server listen address") flag.Parse() envflag.SetPrefix("myapp") envflag.Parse() fmt.Printf("listenAddr is %q\n", *listenAddr) }
An environment variable will not override a flag, but will override its default value:
$ go run example.go listenAddr is ":8080" $ MYAPP_LISTEN_ADDR=:7070 go run example.go listenAddr is ":7070" $ MYAPP_LISTEN_ADDR=:7070 go run example.go -listen-addr=:9090 listenAddr is ":9090"
Flag names are automatically converted to environment variable keys according to the following rules:
- Non-ASCII runes are omitted.
- Uppercase letters, digits, and underscores are preserved.
- Lowercase letters are changed to uppercase.
- Hyphens are changed to underscores.
- All other runes are omitted.
- Prepend an underscore if a variable name would otherwise begin with a digit.
Index ¶
Constants ¶
This section is empty.
Variables ¶
var CommandLine = NewVarSet(flag.CommandLine)
CommandLine wraps flag.CommandLine, the default set of command-line flags. The top-level functions, such as SetPrefix and Parse, are wrappers for the methods of CommandLine.
Functions ¶
func Parse ¶
func Parse()
Parse sets the value of flags that were not provided on the command-line but are set in the environment.
func RenameFlag ¶
func RenameFlag(old, new string)
RenameFlag modifies a flag name before it is converted to an environment key. The new name will be transformed by the same process as any other flag name.
Types ¶
type VarSet ¶
type VarSet struct { // LookupEnv is an optional replacement for os.LookupEnv in this VarSet. LookupEnv func(key string) (string, bool) // contains filtered or unexported fields }
A VarSet wraps a flag.FlagSet. The zero value of a VarSet should not be used.
A program that uses the top-level functions in package flag should also use the top-level functions in this package. Otherwise, it must wrap each flag.FlagSet with a VarSet to connect it to the environment.
func NewVarSet ¶
NewVarSet creates a new VarSet with the specified flag set and error handling property.
func (*VarSet) Parse ¶
Parse sets the value of flags that were not provided on the command-line but are set in the environment.
func (*VarSet) RenameFlag ¶
RenameFlag modifies a flag name before it is converted to an environment key. The new name will be transformed by the same process as any other flag name.