Documentation ¶
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( ErrFlagNotExists = errors.New("flag does not exist") ErrCustomAlreadyDefined = errors.New("custom flag already exists") ErrInvalidBoolFlagValue = errors.New("bool flag got value that was't 'true' or 'false'") ErrInvalidDurationFlagValue = errors.New("duration flag got value that was't parsable as a golang duration, example: 1m30s") )
Functions ¶
func Disable ¶
func Disable(name string)
Disable removes the given flag from using any environment variables. It must be called before the call to envy.Parse().
Example ¶
package main import ( "os" "github.com/fernferret/envy" "github.com/spf13/pflag" ) func main() { // Reset CommandLine flags for example, you don't need this in your code! pflag.CommandLine = pflag.NewFlagSet("test", pflag.PanicOnError) // Define a few flags pflag.String("url", "http://localhost:8080", "set the url") pflag.Bool("once", false, "only run processing once") // Don't allow FOO_ONCE to set the --once flag. envy.Disable("once") // Pre-parse with envy with a prefix of FOO_ envy.Parse("FOO") // Parse the flags pflag.Parse() // Output results to stdout instead of the default stderr pflag.CommandLine.SetOutput(os.Stdout) pflag.PrintDefaults() }
Output: --once only run processing once --url string set the url [FOO_URL] (default "http://localhost:8080")
func DisableOnFlagSet ¶
DisableOnFlagSet removes the given flag from using any environment variables. It must be called before the call to envy.Parse().
func Parse ¶
func Parse(pfx string)
ParseFlagSet will loop through defined flags in the default pflag.CommandLine and automatically add an environment variable parser for the flag name. This Parse func must be called before the call to pflag.Parse() and after you've defined all your flags.
Example ¶
package main import ( "os" "time" "github.com/fernferret/envy" "github.com/spf13/pflag" ) func main() { // Reset CommandLine flags for example, don't include these in your code! pflag.CommandLine = pflag.NewFlagSet("test", pflag.PanicOnError) os.Clearenv() // Define a few flags pflag.String("url", "http://localhost:8080", "set the url") pflag.Bool("once", false, "only run processing once") pflag.DurationP("interval", "i", time.Minute, "interval to check widgets") // Simulate COOL_APP_INTERVAL being set os.Setenv("COOL_APP_INTERVAL", "10m") // Pre-parse with envy with a prefix of COOL_APP_ envy.Parse("COOL_APP") // Don't sort the flags. pflag.CommandLine.SortFlags = false // Parse the flags pflag.Parse() // Output results to stdout instead of the default stderr pflag.CommandLine.SetOutput(os.Stdout) pflag.PrintDefaults() }
Output: --url string set the url [COOL_APP_URL] (default "http://localhost:8080") --once only run processing once [COOL_APP_ONCE] -i, --interval duration interval to check widgets [COOL_APP_INTERVAL 10m0s] (default 1m0s)
func ParseFlagSet ¶
ParseFlagSet will loop through defined flags in the given pflag.FlagSet and automatically add an environment variable parser for the flag name. This ParseFlagSet func must be called before the call to pflag.Parse() and after you've defined all your flags.
func SetEnvName ¶
func SetEnvName(name, envName string)
SetEnvName allows setting a custom environment variable for a given flag. It must be called before the call to envy.Parse().
Example ¶
package main import ( "os" "github.com/fernferret/envy" "github.com/spf13/pflag" ) func main() { // Reset CommandLine flags for example, you don't need this in your code! pflag.CommandLine = pflag.NewFlagSet("test", pflag.PanicOnError) // Define a few flags pflag.String("url", "http://localhost:8080", "set the url") pflag.Bool("once", false, "only run processing once") // Don't use FOO_URL, instead use MY_HTTP_URL as the env var envy.SetEnvName("url", "MY_HTTP_URL") // Pre-parse with envy with a prefix of FOO_ envy.Parse("FOO") pflag.Parse() // Output results to stdout instead of the default stderr pflag.CommandLine.SetOutput(os.Stdout) pflag.PrintDefaults() }
Output: --once only run processing once [FOO_ONCE] --url string set the url [MY_HTTP_URL] (default "http://localhost:8080")
func SetEnvNameOnFlagSet ¶
SetEnvNameOnFlagSet allows setting a custom environment variable for a given flag. It must be called before the call to envy.Parse().
Types ¶
This section is empty.