diff options
| author | Felix Hanley <felix@userspace.com.au> | 2023-01-31 00:46:04 +0000 |
|---|---|---|
| committer | Felix Hanley <felix@userspace.com.au> | 2023-01-31 00:46:04 +0000 |
| commit | 9eada7f9f6df2e72c71c079c2ce795ae79963b40 (patch) | |
| tree | 9410725bc948b04b382890937524ba0a039e3a7b | |
| parent | dbff2d86126587604812e79f126fd15ea2fc7986 (diff) | |
| download | envflag-9eada7f9f6df2e72c71c079c2ce795ae79963b40.tar.gz envflag-9eada7f9f6df2e72c71c079c2ce795ae79963b40.tar.bz2 | |
Add prefix optionv0.1.0
| -rw-r--r-- | example.go | 5 | ||||
| -rw-r--r-- | flag.go | 24 | ||||
| -rw-r--r-- | flag_test.go | 16 |
3 files changed, 38 insertions, 7 deletions
@@ -15,3 +15,8 @@ func ExampleFlagSet() { fs.Bool("verbose", false, "Be verbose") ParseFlagSet(fs, os.Args[1:]) } + +func ExamplePrefix() { + flag.String("db", "", "Database thing") + Parse(Prefix("APP_")) +} @@ -19,10 +19,26 @@ type FlagConverterFunc func(flag string) string type UsageUpdaterFunc func(key, usage string) string type config struct { + prefix string flagConverter FlagConverterFunc usageUpdater UsageUpdaterFunc } +// FlagConverter converts the flag name to an environment variable key. +func FlagConverter(f FlagConverterFunc) Option { + return func(cfg *config) { + cfg.flagConverter = f + } +} + +// Prefix sets the prefix for all envvars. +// This is applied after the FlagConverter function. +func Prefix(s string) Option { + return func(cfg *config) { + cfg.prefix = s + } +} + // UsageUpdater enables configurable flag usage updates. func UsageUpdater(f UsageUpdaterFunc) Option { return func(cfg *config) { @@ -44,13 +60,6 @@ func UsageSuffixer() Option { } } -// FlagConverter converts the flag name to an environment variable key. -func FlagConverter(f FlagConverterFunc) Option { - return func(cfg *config) { - cfg.flagConverter = f - } -} - // ParseWithEnv parses the command-line flags from os.Args[1:]. Must be called // after all flags are defined and before flags are accessed by the program. func Parse(opts ...Option) error { @@ -78,6 +87,7 @@ func parseFlagSetWithEnv(fs *flag.FlagSet, arguments []string, opts ...Option) e var nerr error fs.VisitAll(func(f *flag.Flag) { envKey := cfg.flagConverter(f.Name) + envKey = cfg.prefix + envKey if cfg.usageUpdater != nil { f.Usage = cfg.usageUpdater(envKey, f.Usage) } diff --git a/flag_test.go b/flag_test.go index bed8d8d..f09bd82 100644 --- a/flag_test.go +++ b/flag_test.go @@ -74,6 +74,22 @@ func TestParseFlagSet(t *testing.T) { env: map[string]string{"TES_T_ING_123": "fromenv"}, expected: "fromenv", }, + "prefixed-no-flag": { + // Flags take precedence + name: "testing-123", + values: []string{}, + opts: []Option{Prefix("APP_")}, + env: map[string]string{"APP_TESTING_123": "fromenv"}, + expected: "fromenv", + }, + "prefixed-with-flag": { + // Flags take precedence + name: "testing-123", + values: []string{"-testing-123", "passed"}, + opts: []Option{Prefix("APP_")}, + env: map[string]string{"APP_TESTING_123": "fromenv"}, + expected: "passed", + }, } for name, tt := range tests { |
