envflag

package module
v0.0.0-...-9111d83 Latest Latest
Warning

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

Go to latest
Published: Jul 20, 2014 License: BSD-3-Clause Imports: 5 Imported by: 126

README

envflag

Golang flags, but bolted onto the environment rather than the command-line.

Read the godocs.

Motivation

Some like the distinction that command-line flags control behavior while environment variables configure. Also 12-factor recommends the use of environment variables for configuration. The interface of the golang flag package is well designed and easy to use, and allows for other lists (os.Environ() vs os.Args) to be parsed as flags. It makes sense then to use the same interface, the same types, and the same parsing (caveat: there is some ugly string hacking to make environment variables look like flags) to the same ends.

Differences

Calling flag.Parse() will not parse environment flags. Calling envflag.Parse() will not parse command-line flags. There is no good reason to combine these two when the net savings is a single line in a func main(). Furthermore, doing so would require users to accept a precedence order of my choosing.

The presence of an environment variable named h or help will probably cause problems (print Usage and os.Exit(0)). Work around this by defining those flags somewhere (and ignoring them).

Before calling Flagset.Parse on EnvironmentFlags, the environment variables being passed to Parse are trimmed down using Lookup. This behavior is different from flag.Parse in that extra environment variables are ignored (and won't crash envflag.Parse).

Documentation

Overview

Package envflag adds environment variable flags to the flag package.

Usage:

Define flags using envflag.String(), Bool(), Int(), etc. This package works nearly the same as the stdlib flag package. Parsing the Environment flags is done by calling envflag.Parse()

It will *not* attempt to parse any normally-defined command-line flags. Command-line flags are explicitly left alone and separate.

Index

Constants

This section is empty.

Variables

View Source
var EnvironmentFlags = flag.NewFlagSet("environment", flag.ExitOnError)

EnvironmentFlags is the default set of environment flags, parsed from os.Environ(). The top-level functions such as BoolVar, Arg, and on are wrappers for the methods of EnvironmentFlags.

Functions

func Bool

func Bool(name string, value bool, usage string) *bool

Bool defines a bool flag with specified name, default value, and usage string. The return value is the address of a bool variable that stores the value of the flag.

func BoolVar

func BoolVar(p *bool, name string, value bool, usage string)

BoolVar defines a bool flag with specified name, default value, and usage string. The argument p points to a bool variable in which to store the value of the flag.

func Duration

func Duration(name string, value time.Duration, usage string) *time.Duration

Duration defines a time.Duration flag with specified name, default value, and usage string. The return value is the address of a time.Duration variable that stores the value of the flag.

func DurationVar

func DurationVar(p *time.Duration, name string, value time.Duration, usage string)

DurationVar defines a time.Duration flag with specified name, default value, and usage string. The argument p points to a time.Duration variable in which to store the value of the flag.

func Float64

func Float64(name string, value float64, usage string) *float64

Float64 defines a float64 flag with specified name, default value, and usage string. The return value is the address of a float64 variable that stores the value of the flag.

func Float64Var

func Float64Var(p *float64, name string, value float64, usage string)

Float64Var defines a float64 flag with specified name, default value, and usage string. The argument p points to a float64 variable in which to store the value of the flag.

func Int

func Int(name string, value int, usage string) *int

Int defines an int flag with specified name, default value, and usage string. The return value is the address of an int variable that stores the value of the flag.

func Int64

func Int64(name string, value int64, usage string) *int64

Int64 defines an int64 flag with specified name, default value, and usage string. The return value is the address of an int64 variable that stores the value of the flag.

func Int64Var

func Int64Var(p *int64, name string, value int64, usage string)

Int64Var defines an int64 flag with specified name, default value, and usage string. The argument p points to an int64 variable in which to store the value of the flag.

func IntVar

func IntVar(p *int, name string, value int, usage string)

IntVar defines an int flag with specified name, default value, and usage string. The argument p points to an int variable in which to store the value of the flag.

func Lookup

func Lookup(name string) *flag.Flag

Lookup returns the Flag structure of the named environment flag, returning nil if none exists.

func Parse

func Parse()

Parse parses the environment flags from os.Environ. Must be called after all flags are defined and before flags are accessed by the program.

func Parsed

func Parsed() bool

Parsed returns true if the environment flags have been parsed.

func PrintDefaults

func PrintDefaults()

PrintDefaults prints to standard error the default values of all defined environment flags.

func Set

func Set(name, value string) error

Set sets the value of the named environment flag.

func String

func String(name string, value string, usage string) *string

String defines a string flag with specified name, default value, and usage string. The return value is the address of a string variable that stores the value of the flag.

func StringVar

func StringVar(p *string, name string, value string, usage string)

StringVar defines a string flag with specified name, default value, and usage string. The argument p points to a string variable in which to store the value of the flag.

func Uint

func Uint(name string, value uint, usage string) *uint

Uint defines a uint flag with specified name, default value, and usage string. The return value is the address of a uint variable that stores the value of the flag.

func Uint64

func Uint64(name string, value uint64, usage string) *uint64

Uint64 defines a uint64 flag with specified name, default value, and usage string. The return value is the address of a uint64 variable that stores the value of the flag.

func Uint64Var

func Uint64Var(p *uint64, name string, value uint64, usage string)

Uint64Var defines a uint64 flag with specified name, default value, and usage string. The argument p points to a uint64 variable in which to store the value of the flag.

func UintVar

func UintVar(p *uint, name string, value uint, usage string)

UintVar defines a uint flag with specified name, default value, and usage string. The argument p points to a uint variable in which to store the value of the flag.

func Visit

func Visit(fn func(*flag.Flag))

Visit visits the environment flags in lexicographical order, calling fn for each. It visits only those flags that have been set.

func VisitAll

func VisitAll(fn func(*flag.Flag))

VisitAll visits the environment flags in lexicographical order, calling fn for each. It visits all flags, even those not set.

Types

This section is empty.

Jump to

Keyboard shortcuts

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