flagenv

package module
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Mar 27, 2020 License: MIT Imports: 5 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 the standard flag package.

Usage

The library helps to reduce amount of code usually written to use environment variables as fallbacks for command-line options.

Simply replace flag.Parse() in your main application with flagenv.Parse() to add the functionality (the module also supports passing *flag.FlagSet to flagenv.ParseWithEnv for lower-level usage).

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

Environment variable names are added 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)
	}
}))

Contributing

All contributions are welcome. Please fill in an issue before submitting pull requests.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultMap = newDefaultMap()

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

Example: connect-timeout => CONNECT_TIMEOUT.

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.

type Option

type Option func(*parser)

Option is a parser configuration option.

func WithLookupEnv added in v0.4.0

func WithLookupEnv(fn func(key string) (string, bool)) Option

WithLookupEnv changes the default function that retrieves environment variables.

Default is os.LookupEnv.

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