envflag: github.com/dcowgill/envflag Index | Files

package envflag

import "github.com/dcowgill/envflag"

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

Package Files

envflag.go

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.

func Parse Uses

func Parse()

Parse sets the value of flags that were not provided on the command-line but are set in the environment.

func RenameFlag Uses

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.

func SetPrefix Uses

func SetPrefix(prefix string)

SetPrefix specifies a string to prepend to all environment variable keys. An underscore is automatically inserted between the prefix and variable key.

type VarSet Uses

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 Uses

func NewVarSet(fs *flag.FlagSet) *VarSet

NewVarSet creates a new VarSet with the specified flag set and error handling property.

func (*VarSet) Parse Uses

func (vs *VarSet) Parse() error

Parse sets the value of flags that were not provided on the command-line but are set in the environment.

func (*VarSet) RenameFlag Uses

func (vs *VarSet) 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.

func (*VarSet) SetPrefix Uses

func (vs *VarSet) SetPrefix(prefix string)

SetPrefix specifies a string to prepend to all environment variable keys. An underscore is automatically inserted between the prefix and variable key.

Package envflag imports 4 packages (graph). Updated 2018-12-10. Refresh now. Tools for package owners.