flagenv

package module
v0.3.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 28, 2020 License: MIT Imports: 4 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

flagenv

Environment variables support for golang's flag package.

Usage

The library helps to reduce amount of code that's usually written in every command-line application like this:

flag.UintVar(&timeoutFlag, "timeout", 10, "connection timeout in seconds [$TIMEOUT]")
flag.Parse()

var changed bool
flag.Visit(func(f *flag.Flag) {
	if f.Name == "timeout" {
		changed = true
	}
})

if s := os.Getenv("TIMEOUT"); s != "" && !changed { // awkward way to detect unset flags
	timeout, err := strconv.ParseUint(s, 10, 0)
	if err != nil {
		return fmt.Errorf("invalid value %q for flag -%s [$%s]: %s",
			s, "timeout", "TIMEOUT", err,
		)
	}
	timeoutFlag = uint(timeout)
}

Simply call flagenv.Parse() function after registering all necessary flags that wraps around flag.Parse() enabling environment variables as fallback values:

flag.UintVar(&timeoutFlag, "timeout", 10, "connection timeout")
flagenv.Parse()

Environment is only applied to the flags that are not set through the command-line arguments.

It also adds environment variable names to each flag in the help message:

Usage of main:
  -timeout uint
        connection timeout [$TIMEOUT] (default 10)

Configuration

In case you're using short flag names you may employ WithMap option to override environment variable names with more descriptive alternatives:

flag.StringVar(&addrFlag, "a", ":8080", "address to connect to")
flag.UintVar(&timeoutFlag, "t", 0, "connection timeout")
flag.BoolVar(&forceFlag, "f", false, "force the action")
flagenv.Parse(flagenv.WithMap(func(name string) string {
	switch name {
	case "a":
		return "ADDR"
	case "t":
		return "TIMEOUT"
	case "f":
		// ignore '-f' flag
		return "" 
	default:
		// fall back to the default behaviour
		return flagenv.DefaultMap(name)
	}
}))

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Parse

func Parse(opts ...Option)

Parse should be used in place of `flag.Parse()` to add support of environment variables to the default command-line flags parser.

func ParseWithEnv

func ParseWithEnv(fs *flag.FlagSet, argv []string, opts ...Option) error

ParseWithEnv enables environment variables support for the given flag set. It adds environment variable names to each value usage string. Values from the environment only applied when flag hasn't been set from the command-line arguments and the value is not empty.

Panics when fs is already parsed.

Types

type MapFunc

type MapFunc func(name string) string

MapFunc maps flag names to environment variable names.

If returned value is an empty string the flag is ignored.

var DefaultMap MapFunc = func(name string) string {
	return strings.ToUpper(strings.Replace(name, "-", "_", -1))
}

DefaultMap is default variable mapping function, it replaces hyphens with underscores and upper-cases the result.

Example: connect-timeout => CONNECT_TIMEOUT.

type Option

type Option func(*parser)

Option is a parser configuration option.

func WithMap

func WithMap(fn MapFunc) Option

WithMap sets callback that maps flag names to environment variable names.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL