Documentation
¶
Overview ¶
Package env implements command-line environment variables parsing.
Usage ¶
Define env vars using env.String, Bool, Int, etc.
This declares an integer env variable, N, stored in the pointer nEnvVar, with type *int:
import "github.com/negrel/configue/env"
var nEnvVar = env.Int("N", 1234, "help message for env var N")
If you like, you can bind the env var to a variable using the Var() functions.
var envVar int
func init() {
env.IntVar(&envVar, "ENVNAME", 1234, "help message for ENVNAME")
}
Or you can create custom env vars that satisfy the Value interface (with pointer receivers) and couple them to env var parsing by
env.Var(&envVar, "ENVNAME", "help message for ENVNAME")
For such env vars, the default value is just the initial value of the variable.
After all env vars are defined, call
env.Parse()
to parse the command line into the defined env vars.
Env vars may then be used directly. If you're using the env vars themselves, they are all pointers; if you bind to variables, they're values.
fmt.Println("ip has value ", *ip)
fmt.Println("envVar has value ", envVar)
Command line env var syntax ¶
See [`option`](../option#pkg-overview) documentation for options syntax of basic types.
The default set of command-line env vars is controlled by top-level functions. The EnvSet type allows one to define independent sets of env vars, such as to implement subcommands in a command-line interface. The methods of EnvSet are analogous to the top-level functions for the command-line env var set.
Index ¶
- Constants
- Variables
- func Bool(name string, value bool, usage string) *bool
- func BoolSlice(name string, value []bool, usage string) *[]bool
- func BoolSliceVar(p *[]bool, name string, value []bool, usage string)
- func BoolVar(p *bool, name string, value bool, usage string)
- func Duration(name string, value time.Duration, usage string) *time.Duration
- func DurationSlice(name string, value []time.Duration, usage string) *[]time.Duration
- func DurationSliceVar(p *[]time.Duration, name string, value []time.Duration, usage string)
- func DurationVar(p *time.Duration, name string, value time.Duration, usage string)
- func Float64(name string, value float64, usage string) *float64
- func Float64Slice(name string, value []float64, usage string) *[]float64
- func Float64SliceVar(p *[]float64, name string, value []float64, usage string)
- func Float64Var(p *float64, name string, value float64, usage string)
- func Func(name, usage string, fn func(string) error)
- func Int(name string, value int, usage string) *int
- func Int64(name string, value int64, usage string) *int64
- func Int64Slice(name string, value []int64, usage string) *[]int64
- func Int64SliceVar(p *[]int64, name string, value []int64, usage string)
- func Int64Var(p *int64, name string, value int64, usage string)
- func IntSlice(name string, value []int, usage string) *[]int
- func IntSliceVar(p *[]int, name string, value []int, usage string)
- func IntVar(p *int, name string, value int, usage string)
- func Parse() error
- func Parsed() bool
- func PrintDefaults()
- func Set(name, value string) error
- func String(name string, value string, usage string) *string
- func StringSlice(name string, value []string, usage string) *[]string
- func StringSliceVar(p *[]string, name string, value []string, usage string)
- func StringVar(p *string, name string, value string, usage string)
- func TextVar(p encoding.TextUnmarshaler, name string, value encoding.TextMarshaler, ...)
- func Uint(name string, value uint, usage string) *uint
- func Uint64(name string, value uint64, usage string) *uint64
- func Uint64Slice(name string, value []uint64, usage string) *[]uint64
- func Uint64SliceVar(p *[]uint64, name string, value []uint64, usage string)
- func Uint64Var(p *uint64, name string, value uint64, usage string)
- func UintSlice(name string, value []uint, usage string) *[]uint
- func UintSliceVar(p *[]uint, name string, value []uint, usage string)
- func UintVar(p *uint, name string, value uint, usage string)
- func UnquoteUsage(optValue option.Value, optUsage string) (name string, usage string)
- func Var(value option.Value, name string, usage string)
- func Visit(fn func(*EnvVar))
- func VisitAll(fn func(*EnvVar))
- type EnvSet
- func (es *EnvSet) Bool(name string, value bool, usage string) *bool
- func (es *EnvSet) BoolSlice(name string, value []bool, usage string) *[]bool
- func (es *EnvSet) BoolSliceVar(p *[]bool, name string, value []bool, usage string)
- func (es *EnvSet) BoolVar(p *bool, name string, value bool, usage string)
- func (es *EnvSet) Duration(name string, value time.Duration, usage string) *time.Duration
- func (es *EnvSet) DurationSlice(name string, value []time.Duration, usage string) *[]time.Duration
- func (es *EnvSet) DurationSliceVar(p *[]time.Duration, name string, value []time.Duration, usage string)
- func (es *EnvSet) DurationVar(p *time.Duration, name string, value time.Duration, usage string)
- func (es *EnvSet) Float64(name string, value float64, usage string) *float64
- func (es *EnvSet) Float64Slice(name string, value []float64, usage string) *[]float64
- func (es *EnvSet) Float64SliceVar(p *[]float64, name string, value []float64, usage string)
- func (es *EnvSet) Float64Var(p *float64, name string, value float64, usage string)
- func (es *EnvSet) Func(name, usage string, fn func(string) error)
- func (es *EnvSet) Init(name string, errorHandling ErrorHandling)
- func (es *EnvSet) Int(name string, value int, usage string) *int
- func (es *EnvSet) Int64(name string, value int64, usage string) *int64
- func (es *EnvSet) Int64Slice(name string, value []int64, usage string) *[]int64
- func (es *EnvSet) Int64SliceVar(p *[]int64, name string, value []int64, usage string)
- func (es *EnvSet) Int64Var(p *int64, name string, value int64, usage string)
- func (es *EnvSet) IntSlice(name string, value []int, usage string) *[]int
- func (es *EnvSet) IntSliceVar(p *[]int, name string, value []int, usage string)
- func (es *EnvSet) IntVar(p *int, name string, value int, usage string)
- func (es *EnvSet) Lookup(name string) *EnvVar
- func (es *EnvSet) Name() string
- func (es *EnvSet) Output() io.Writer
- func (es *EnvSet) Parse(envvars []string) error
- func (es *EnvSet) Parsed() bool
- func (es *EnvSet) PrintDefaults()
- func (es *EnvSet) Set(name, value string) error
- func (es *EnvSet) SetOutput(output io.Writer)
- func (es *EnvSet) String(name string, value string, usage string) *string
- func (es *EnvSet) StringSlice(name string, value []string, usage string) *[]string
- func (es *EnvSet) StringSliceVar(p *[]string, name string, value []string, usage string)
- func (es *EnvSet) StringVar(p *string, name string, value string, usage string)
- func (es *EnvSet) TextVar(p encoding.TextUnmarshaler, name string, value encoding.TextMarshaler, ...)
- func (es *EnvSet) Uint(name string, value uint, usage string) *uint
- func (es *EnvSet) Uint64(name string, value uint64, usage string) *uint64
- func (es *EnvSet) Uint64Slice(name string, value []uint64, usage string) *[]uint64
- func (es *EnvSet) Uint64SliceVar(p *[]uint64, name string, value []uint64, usage string)
- func (es *EnvSet) Uint64Var(p *uint64, name string, value uint64, usage string)
- func (es *EnvSet) UintSlice(name string, value []uint, usage string) *[]uint
- func (es *EnvSet) UintSliceVar(p *[]uint, name string, value []uint, usage string)
- func (es *EnvSet) UintVar(p *uint, name string, value uint, usage string)
- func (es *EnvSet) Var(val option.Value, name string, usage string)
- func (es *EnvSet) Visit(fn func(*EnvVar))
- func (es *EnvSet) VisitAll(fn func(*EnvVar))
- type EnvVar
- type ErrorHandling
Constants ¶
const ( ContinueOnError ErrorHandling = flag.ContinueOnError // Return a descriptive error. ExitOnError = flag.ExitOnError // Call os.Exit(2). PanicOnError = flag.PanicOnError // Call panic with a descriptive error. )
These constants cause EnvSet.Parse to behave as described if the parse fails.
Variables ¶
var ( // CommandLine is the default set of command-line env vars, parsed from // os.Environ. The top-level functions such as BoolVar, and so on are // wrappers for the methods of CommandLine. CommandLine = NewEnvSet("", ExitOnError) // Usage prints a usage message documenting all defined command-line env vars // to CommandLine's output, which by default is os.Stderr. It is called when // an error occurs while parsing env vars. The function is a variable that may // be changed to point to a custom function. By default it prints a simple // header and calls PrintDefaults; for details about the format of the Output // and how to control it, see the documentation for PrintDefaults. Custom // usage functions may choose to exit the program; by default exiting happens // anyway as the command line's error handling strategy is set to ExitOnError. Usage = func() { _, _ = fmt.Fprintf(CommandLine.Output(), "Usage of %s:\n", os.Args[0]) PrintDefaults() } // Ignore undefined variables instead of returning error. IgnoreUndefined = true )
Functions ¶
func Bool ¶
Bool defines a bool env var with specified name, default value, and usage string. The return value is the address of a bool variable that stores the value of the env var.
func BoolSlice ¶ added in v0.5.0
BoolSlice defines a slice of bool env var with specified name, default value, and usage string. The return value is the address of a slice of bool variable that stores the value of the env var.
func BoolSliceVar ¶ added in v0.5.0
BoolSliceVar defines a slice of bool env var with specified name, default value, and usage string. The argument p points to a slice of bool variable in which to store the value of the env var.
func BoolVar ¶
BoolVar defines a bool env var with specified name, default value, and usage string. The argument p points to a bool variable in which to store the value of the env var.
func Duration ¶
Duration defines a time.Duration env var 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 env var.
func DurationSlice ¶ added in v0.5.0
DurationSlice defines a slice of time.Duration env var with specified name, default value, and usage string. The return value is the address of a slice of time.Duration variable that stores the value of the env var.
func DurationSliceVar ¶ added in v0.5.0
DurationSliceVar defines a slice of time.Duration env var with specified name, default value, and usage string. The argument p points to a slice of time.Duration variable in which to store the value of the env var.
func DurationVar ¶
DurationVar defines a time.Duration env var 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 env var.
func Float64 ¶
Float64 defines a float64 env var with specified name, default value, and usage string. The return value is the address of a float64 variable that stores the value of the env var.
func Float64Slice ¶ added in v0.5.0
Float64Slice defines a slice of float64 env var with specified name, default value, and usage string. The return value is the address of a slice of float64 variable that stores the value of the env var.
func Float64SliceVar ¶ added in v0.5.0
Float64SliceVar defines a slice of float64 env var with specified name, default value, and usage string. The argument p points to a slice of float64 variable in which to store the value of the env var.
func Float64Var ¶
Float64Var defines a float64 env var with specified name, default value, and usage string. The argument p points to a float64 variable in which to store the value of the env var.
func Func ¶ added in v0.5.0
Func defines an env var with the specified name and usage string. Each time the env var is seen, fn is called with the value of the env var. If fn returns a non-nil error, it will be treated as a value parsing error.
func Int ¶
Int defines an int env var with specified name, default value, and usage string. The return value is the address of an int variable that stores the value of the env var.
func Int64 ¶
Int64 defines an int64 env var with specified name, default value, and usage string. The return value is the address of an int64 variable that stores the value of the env var.
func Int64Slice ¶ added in v0.5.0
Int64Slice defines a slice of int64 env var with specified name, default value, and usage string. The return value is the address of a slice of int64 variable that stores the value of the env var.
func Int64SliceVar ¶ added in v0.5.0
Int64SliceVar defines a slice of int64 env var with specified name, default value, and usage string. The argument p points to a slice of int64 variable in which to store the value of the env var.
func Int64Var ¶
Int64Var defines an int64 env var with specified name, default value, and usage string. The argument p points to an int64 variable in which to store the value of the env var.
func IntSlice ¶ added in v0.5.0
IntSlice defines a slice of int env var with specified name, default value, and usage string. The return value is the address of a slice of int variable that stores the value of the env var.
func IntSliceVar ¶ added in v0.5.0
IntSliceVar defines a slice of int env var with specified name, default value, and usage string. The argument p points to a slice of int variable in which to store the value of the env var.
func IntVar ¶
IntVar defines an int env var with specified name, default value, and usage string. The argument p points to an int variable in which to store the value of the env var.
func Parse ¶
func Parse() error
Parse parses the command-line env vars from os.Environ(). Must be called after all env vars are defined and before env vars are accessed by the program.
func PrintDefaults ¶
func PrintDefaults()
PrintDefaults prints, to standard error unless configured otherwise, a usage message showing the default settings of all defined env vars.
See the documentation *EnvSet.PrintDefaults for more information.
To change the destination for env var messages, call CommandLine.SetOutput.
func String ¶
String defines a string env var with specified name, default value, and usage string. The return value is the address of a string variable that stores the value of the env var.
func StringSlice ¶ added in v0.5.0
StringSlice defines a slice of string env var with specified name, default value, and usage string. The return value is the address of a slice of string variable that stores the value of the env var.
func StringSliceVar ¶ added in v0.5.0
StringSliceVar defines a slice of string env var with specified name, default value, and usage string. The argument p points to a slice of string variable in which to store the value of the env var.
func StringVar ¶
StringVar defines a string env var with specified name, default value, and usage string. The argument p points to a string variable in which to store the value of the env var.
func TextVar ¶
func TextVar( p encoding.TextUnmarshaler, name string, value encoding.TextMarshaler, usage string, )
TextVar defines an env var with a specified name, default value, and usage string. The argument p must be a pointer to a variable that will hold the value of the env var, and p must implement encoding.TextUnmarshaler. If the env var is used, the env var value will be passed to p's UnmarshalText method. The type of the default value must be the same as the type of p.
func Uint ¶
Uint defines an uint env var with specified name, default value, and usage string. The return value is the address of an uint variable that stores the value of the env var.
func Uint64 ¶
Uint64 defines an uint64 env var with specified name, default value, and usage string. The return value is the address of an uint64 variable that stores the value of the env var.
func Uint64Slice ¶ added in v0.5.0
Uint64Slice defines a slice of uint64 env var with specified name, default value, and usage string. The return value is the address of a slice of uint64 variable that stores the value of the env var.
func Uint64SliceVar ¶ added in v0.5.0
Uint64SliceVar defines a slice of uint64 env var with specified name, default value, and usage string. The argument p points to a slice of uint64 variable in which to store the value of the env var.
func Uint64Var ¶
Uint64Var defines an uint64 env var with specified name, default value, and usage string. The argument p points to an uint64 variable in which to store the value of the env var.
func UintSlice ¶ added in v0.5.0
UintSlice defines a slice of uint env var with specified name, default value, and usage string. The return value is the address of a slice of uint variable that stores the value of the env var.
func UintSliceVar ¶ added in v0.5.0
UintSliceVar defines a slice of uint env var with specified name, default value, and usage string. The argument p points to a slice of uint variable in which to store the value of the env var.
func UintVar ¶
UintVar defines an uint env var with specified name, default value, and usage string. The argument p points to an uint variable in which to store the value of the env var.
func UnquoteUsage ¶
UnquoteUsage extracts a back-quoted name from the usage string for an env var and returns it and the un-quoted usage. Given "a `name` to show" it returns ("name", "a name to show"). If there are no back quotes, the name is an educated guess of the type of the env var's value, or the empty string if the env var is boolean.
func Var ¶
Var defines an env var with the specified name and usage string. The type and value of the env var are represented by the first argument, of type [Value], which typically holds a user-defined implementation of [Value]. For instance, the caller could create an env var that turns a comma-separated string into a slice of strings by giving the slice the methods of [Value]; in particular, Set would decompose the comma-separated string into the slice.
Types ¶
type EnvSet ¶
type EnvSet struct {
Usage func()
// contains filtered or unexported fields
}
EnvSet represents a set of defined environment variables. The zero value of a EnvSet has no name and has ContinueOnError error handling.
EnvVar names must be unique within a EnvSet. An attempt to define an env var whose name is already in use will cause a panic.
func NewEnvSet ¶
func NewEnvSet(name string, errorHandling ErrorHandling) *EnvSet
NewEnvSet returns a new, empty env var set with the specified name and error handling property. If the name is not empty, it will be printed in the default usage message and in error messages.
func (*EnvSet) Bool ¶
Bool defines a bool option with specified name, default value, and usage string. The return value is the address of a bool variable that stores the value of the option.
func (*EnvSet) BoolSlice ¶ added in v0.5.0
Bool defines a slice of bool option with specified name, default value, and usage string. The return value is the address of a slice of bool variable that stores the value of the option.
func (*EnvSet) BoolSliceVar ¶ added in v0.5.0
BoolSliceVar defines a slice of bool option with specified name, default value, and usage string. The argument p points to a slice of bool variable in which to store the value of the option.
func (*EnvSet) BoolVar ¶
Bool defines a bool option with specified name, default value, and usage string. The argument p points to a bool variable in which to store the value of the option.
func (*EnvSet) Duration ¶
Duration defines a time.Duration option 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 option.
func (*EnvSet) DurationSlice ¶ added in v0.5.0
func (es *EnvSet) DurationSlice( name string, value []time.Duration, usage string, ) *[]time.Duration
Duration defines a slice of time.Duration option with specified name, default value, and usage string. The return value is the address of a slice of time.Duration variable that stores the value of the option.
func (*EnvSet) DurationSliceVar ¶ added in v0.5.0
func (es *EnvSet) DurationSliceVar( p *[]time.Duration, name string, value []time.Duration, usage string, )
DurationSliceVar defines a slice of time.Duration option with specified name, default value, and usage string. The argument p points to a slice of time.Duration variable in which to store the value of the option.
func (*EnvSet) DurationVar ¶
Duration defines a time.Duration option 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 option.
func (*EnvSet) Float64 ¶
Float64 defines a float64 option with specified name, default value, and usage string. The return value is the address of a float64 variable that stores the value of the option.
func (*EnvSet) Float64Slice ¶ added in v0.5.0
Float64 defines a slice of float64 option with specified name, default value, and usage string. The return value is the address of a slice of float64 variable that stores the value of the option.
func (*EnvSet) Float64SliceVar ¶ added in v0.5.0
Float64SliceVar defines a slice of float64 option with specified name, default value, and usage string. The argument p points to a slice of float64 variable in which to store the value of the option.
func (*EnvSet) Float64Var ¶
Float64 defines a float64 option with specified name, default value, and usage string. The argument p points to a float64 variable in which to store the value of the option.
func (*EnvSet) Func ¶ added in v0.5.0
Func defines an env var with the specified name and usage string. Each time the env var is seen, fn is called with the value of the env var. If fn returns a non-nil error, it will be treated as a value parsing error.
func (*EnvSet) Init ¶
func (es *EnvSet) Init(name string, errorHandling ErrorHandling)
Init sets the name and error handling property for a env var set. By default, the zero EnvSet uses an empty name and the ContinueOnError error handling policy.
func (*EnvSet) Int ¶
Int defines an int option with specified name, default value, and usage string. The return value is the address of an int variable that stores the value of the option.
func (*EnvSet) Int64 ¶
Int64 defines an int64 option with specified name, default value, and usage string. The return value is the address of an int64 variable that stores the value of the option.
func (*EnvSet) Int64Slice ¶ added in v0.5.0
Int64 defines a slice of int64 option with specified name, default value, and usage string. The return value is the address of a slice of int64 variable that stores the value of the option.
func (*EnvSet) Int64SliceVar ¶ added in v0.5.0
Int64SliceVar defines a slice of int64 option with specified name, default value, and usage string. The argument p points to a slice of int64 variable in which to store the value of the option.
func (*EnvSet) Int64Var ¶
Int64 defines an int64 option with specified name, default value, and usage string. The argument p points to a int64 variable in which to store the value of the option.
func (*EnvSet) IntSlice ¶ added in v0.5.0
Int defines a slice of int option with specified name, default value, and usage string. The return value is the address of a slice of int variable that stores the value of the option.
func (*EnvSet) IntSliceVar ¶ added in v0.5.0
IntSliceVar defines a slice of int option with specified name, default value, and usage string. The argument p points to a slice of int variable in which to store the value of the option.
func (*EnvSet) IntVar ¶
Int defines an int option with specified name, default value, and usage string. The argument p points to a int variable in which to store the value of the option.
func (*EnvSet) Lookup ¶
Lookup returns the EnvVar structure of the named env var, returning nil if none exists.
func (*EnvSet) Output ¶
Output returns the destination for usage and error messages. os.Stderr is returned if output was not set or was set to nil.
func (*EnvSet) Parse ¶
Parse parses env var definitions from the environment variable list. Must be called after all env vars in the EnvSet are defined and before env vars are accessed by the program.
func (*EnvSet) PrintDefaults ¶
func (es *EnvSet) PrintDefaults()
PrintDefaults prints, to standard error unless configured otherwise, the default values of all defined env vars in the set. For an integer valued env var X, the default output has the form
X int usage-message-for-x (default 7)
The usage message will appear on a separate line for anything but a bool env var with a one-byte name. For bool env vars, the type is omitted and if the env var name is one byte the usage message appears on the same line. The parenthetical default is omitted if the default is the zero value for the type. The listed type, here int, can be changed by placing a back-quoted name in the env var's usage string; the first such item in the message is taken to be a parameter name to show in the message and the back quotes are stripped from the message when displayed. For instance, given
env.String("I", "", "search `directory` for include files")
the output will be
I directory search directory for include files.
To change the destination for env var messages, call *EnvSet.SetOutput.
func (*EnvSet) SetOutput ¶
SetOutput sets the destination for usage and error messages. If output is nil, os.Stderr is used.
func (*EnvSet) String ¶
String defines a string option with specified name, default value, and usage string. The return value is the address of a string variable that stores the value of the option.
func (*EnvSet) StringSlice ¶ added in v0.5.0
String defines a slice of string option with specified name, default value, and usage string. The return value is the address of a slice of string variable that stores the value of the option.
func (*EnvSet) StringSliceVar ¶ added in v0.5.0
StringSliceVar defines a slice of string option with specified name, default value, and usage string. The argument p points to a slice of string variable in which to store the value of the option.
func (*EnvSet) StringVar ¶
String defines a string option with specified name, default value, and usage string. The argument p points to a string variable in which to store the value of the option.
func (*EnvSet) TextVar ¶
func (es *EnvSet) TextVar( p encoding.TextUnmarshaler, name string, value encoding.TextMarshaler, usage string, )
TextVar defines an option with a specified name, default value, and usage string. The argument p must be a pointer to a variable that will hold the value of the option, and p must implement encoding.TextUnmarshal. If the option is used, the option value will be passed to p's UnmarshalText method. The type of the default value must be the same as the type of p.
func (*EnvSet) Uint ¶
Uint defines an uint option with specified name, default value, and usage string. The return value is the address of an uint variable that stores the value of the option.
func (*EnvSet) Uint64 ¶
Uint64 defines an uint64 option with specified name, default value, and usage string. The return value is the address of an uint64 variable that stores the value of the option.
func (*EnvSet) Uint64Slice ¶ added in v0.5.0
Uint64 defines a slice of uint64 option with specified name, default value, and usage string. The return value is the address of a slice of uint64 variable that stores the value of the option.
func (*EnvSet) Uint64SliceVar ¶ added in v0.5.0
Uint64SliceVar defines a slice of uint64 option with specified name, default value, and usage string. The argument p points to a slice of uint64 variable in which to store the value of the option.
func (*EnvSet) Uint64Var ¶
Uint64 defines an uint64 option with specified name, default value, and usage string. The argument p points to a uint64 variable in which to store the value of the option.
func (*EnvSet) UintSlice ¶ added in v0.5.0
Uint defines a slice of uint option with specified name, default value, and usage string. The return value is the address of a slice of uint variable that stores the value of the option.
func (*EnvSet) UintSliceVar ¶ added in v0.5.0
UintSliceVar defines a slice of uint option with specified name, default value, and usage string. The argument p points to a slice of uint variable in which to store the value of the option.
func (*EnvSet) UintVar ¶
Uint defines an uint option with specified name, default value, and usage string. The argument p points to a uint variable in which to store the value of the option.
func (*EnvSet) Var ¶
Var defines an environment variable with the specified name and usage string. The type and value of the env var are represented by the first argument, of type option.Value, which typically holds a user-defined implementation of option.Value. For instance, the caller could create an env var that turns a comma-separated string into a slice of strings by giving the slice the methods of option.Value; in particular, Set would decompose the comma-separated string into the slice.
type ErrorHandling ¶
type ErrorHandling = flag.ErrorHandling
ErrorHandling defines how EnvSet.Parse behaves if the parse fails.