diff options
| author | Felix Hanley <felix@userspace.com.au> | 2026-01-13 22:46:25 +0000 |
|---|---|---|
| committer | Felix Hanley <felix@userspace.com.au> | 2026-01-13 22:46:25 +0000 |
| commit | 3316d2ebc77cef2c2bd03869063910bd8453d704 (patch) | |
| tree | 83d45db99a68f7d6e11c16d2cadf31612f2fe892 | |
| parent | 87e4f8cc1cc88929700d0fd01d142816ff3c15c6 (diff) | |
| download | envflag-master.tar.gz envflag-master.tar.bz2 | |
| -rw-r--r-- | README.md | 19 | ||||
| -rw-r--r-- | example.go | 22 | ||||
| -rw-r--r-- | example_test.go | 29 | ||||
| -rw-r--r-- | flag.go | 16 |
4 files changed, 56 insertions, 30 deletions
@@ -1,16 +1,23 @@ # Environment variables and flags -Set flags using Go's `flag` package, and complement them with environment -variables. Flags, if set, take precedence over the environment. +Set flags using the standard library `flag` package, and complement them with +environment variables. -Go's standard `flag.FlagSet` can be initialised as normal and parse errors are -returned as expected. Environment variables can have custom prefixes, suffixes -etc. as can the usage displayed with the standard `-h` flag. +- All Go's standard `flag` functions are used as normal and parse errors are + returned as expected. + +- Flags, if set, take precedence over the environment. + +- Environment variables can have custom prefixes and suffixes. + +- Usage output can be displayed with the standard `-h` flag. + +- Usage output can be annotated with the configured environment variables. ```go fs := flag.NewFlagSet("test", flag.ContinueOnError) fs.String("db", "", "Database thing") -envflag.ParseFlagSet(fs, envflag.Prefix("APP_"), envflag.UsageSuffixer()) +_ = envflag.ParseFlagSet(fs, envflag.Prefix("APP_"), envflag.UsageSuffixer()) ``` produces the following usage diff --git a/example.go b/example.go deleted file mode 100644 index a5c459f..0000000 --- a/example.go +++ /dev/null @@ -1,22 +0,0 @@ -package envflag - -import ( - "flag" - "os" -) - -func ExampleFlag() { - flag.Bool("verbose", false, "Be verbose") - Parse() -} - -func ExampleFlagSet() { - fs := flag.NewFlagSet("example", flag.ExitOnError) - fs.Bool("verbose", false, "Be verbose") - ParseFlagSet(fs, os.Args[1:]) -} - -func ExamplePrefix() { - flag.String("db", "", "Database thing") - Parse(Prefix("APP_")) -} diff --git a/example_test.go b/example_test.go new file mode 100644 index 0000000..349a36b --- /dev/null +++ b/example_test.go @@ -0,0 +1,29 @@ +package envflag_test + +import ( + "flag" + "os" + + "userspace.com.au/envflag" +) + +func ExampleFlag() { + flag.Bool("verbose", false, "Be verbose") + envflag.Parse() +} + +func ExampleFlagSet() { + fs := flag.NewFlagSet("example", flag.ExitOnError) + fs.Bool("verbose", false, "Be verbose") + _ = envflag.ParseFlagSet(fs, os.Args[1:], envflag.UsageSuffixer()) +} + +func ExamplePrefix() { + flag.String("db", "", "Database thing") + _ = envflag.Parse(envflag.Prefix("APP_")) +} + +func ExampleUsageSuffixer() { + flag.String("db", "", "Database thing") + _ = envflag.Parse(envflag.Prefix("APP_")) +} @@ -20,7 +20,7 @@ type UsageUpdaterFunc func(key, usage string) string type config struct { prefix string - getenv func(string) string + getenv func(string) string flagConverter FlagConverterFunc usageUpdater UsageUpdaterFunc } @@ -55,6 +55,12 @@ func UsageUpdater(f UsageUpdaterFunc) Option { } // UsagePrefixer prefixes the flag usage with [envvar]. +// +// Example output: +// +// Usage of example: +// -verbose +// [VERBOSE] Be verbose func UsagePrefixer() Option { return func(cfg *config) { cfg.usageUpdater = usagePrefixer @@ -62,6 +68,12 @@ func UsagePrefixer() Option { } // UsageSuffixer suffixes the flag usage with [envvar]. +// +// Example output: +// +// Usage of example: +// -verbose +// Be verbose [VERBOSE] func UsageSuffixer() Option { return func(cfg *config) { cfg.usageUpdater = usageSuffixer @@ -88,7 +100,7 @@ func parseFlagSetWithEnv(fs *flag.FlagSet, arguments []string, opts ...Option) e } cfg := &config{ flagConverter: flagToEnv(), - getenv: os.Getenv, + getenv: os.Getenv, } for _, o := range opts { o(cfg) |
