Documentation
¶
Overview ¶
Example (FlagSet) ¶
Use with fenv.EnvSet and flag.FlagSet
package main import ( "flag" "fmt" "log" "github.com/go-pa/fenv" ) func main() { var s1, s2 string fs := flag.NewFlagSet("example", flag.ContinueOnError) es := fenv.NewEnvSet(fs, fenv.Prefix("my_")) fs.StringVar(&s1, "test1", "", "") fs.StringVar(&s2, "test2", "", "") es.Var(&s2, "other", "") // es.Parse() or es.ParseEnv() to parse a custom environment if err := es.ParseEnv(map[string]string{ "MY_TEST1": "v1", "MY_TEST2": "v2", "OTHER": "v2.other", }); err != nil { log.Fatal(err) } if err := fs.Parse([]string{}); err != nil { log.Fatal(err) } fmt.Println(s1, s2) }
Output: v1 v2.other
Example (Package) ¶
Package level usage
package main import ( "flag" "fmt" "os" "strings" "github.com/go-pa/fenv" ) func main() { var s1, s2, s3 string fenv.CommandLinePrefix("HELLO_") // default env var name prefix flag.StringVar(&s1, "flag.1", "", "") // env var FLAG_1 is automatically added flag.StringVar(&s2, "flag2", "", "") fenv.Var(&s2, "alt-name", "") // registers env names ALT_NAME and FLAG2. flag.StringVar(&s3, "flag3", "", "") fenv.Var(&s3) // excludes the var from being parsed as an enviroment variable. os.Setenv("HELLO_FLAG_1", "v1") os.Setenv("ALT_NAME", "v2.alt") os.Setenv("HELLO_FLAG_2", "v2") fmt.Println("before Parse()") fenv.VisitAll(func(e fenv.EnvFlag) { // don't print go test flags in example if !strings.HasPrefix(e.Flag.Name, "test.") { fmt.Printf("%s:%s\n", e.Flag.Name, e.Name) } }) fenv.MustParse() // call fenv.Parse() before flag.Parse() flag.Parse() fmt.Println("after Parse()") fenv.VisitAll(func(e fenv.EnvFlag) { // don't print go test flags in example if !strings.HasPrefix(e.Flag.Name, "test.") { fmt.Printf("%s:%s\n", e.Flag.Name, e.Name) } }) fmt.Println("values", s1, s2, flag.Parsed()) }
Output: before Parse() flag.1: flag2: after Parse() flag.1:HELLO_FLAG_1 flag2:ALT_NAME values v1 v2.alt true
Index ¶
- Variables
- func CommandLinePrefix(prefix ...string)
- func MustParse()
- func OSEnv() map[string]string
- func Parse() error
- func ParseSet(fs *flag.FlagSet, opt ...Option) error
- func Parsed() bool
- func Var(v interface{}, names ...string)
- func VisitAll(fn func(e EnvFlag))
- type EnvFlag
- type EnvSet
- type FlagError
- type Option
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ErrAlreadyParsed = errors.New("the envset is already parsed")
ErrAlreadyParsed is returned by EnvSet.Parse() if the EnvSet already was parsed.
var ErrMultipleSet = errors.New("multiple errors encountered when calling flag.Set()")
ErrMultipleSet is returned by EnvSet.Parse() if the ContinueOnError is enabled and more than one flag failed to be set.
Functions ¶
func CommandLinePrefix ¶
func CommandLinePrefix(prefix ...string)
CommandLinePrefix sets the prefix used by the package level env set functions.
Types ¶
type EnvFlag ¶
type EnvFlag struct { // the associated flag.Flag Flag *flag.Flag // the environment variable name which the value for flag parsing was // extracted from. This field is set regardless if setting the flag succeeded or not // succeeds or fails. Name string // Value is the value of the environmet variable Name. Value string // all the environment variable names mapped associated with the flag. Names []string // Values for all corresponding Names which were present when EnvSet.Parse() was called. Env map[string]string // IsSet is true if the flag has been set by the owning EnvSet.Parse() function or by the associated FlagSet. IsSet bool // IsSelfSet is true if the flag value was successfully set by the // associated EnvSet.Parse() function. IsSelfSet bool // error caused by flag.Set when the envset tried to set it Err error }
EnvFlag is used bu the EnvSet.Visit* funcs.
type EnvSet ¶
type EnvSet struct {
// contains filtered or unexported fields
}
EnvSet adds environment variable support for flag.FlagSet.
func (*EnvSet) Var ¶
Var enables associattion with environment variable names other than the default auto generated ones
If no name argument is supplied the variable will be excluded from environment pasrsing and the EnvSet.VisitAll method. The special name value emtpy string "" will be translated to the automatically generated environment variable name. This function will panic if given an