namsral/flag

prefix for environment variables

cmorent opened this issue · 6 comments

It would be great to have a function like "SetEnvPrefix" or something to be able to add a prefix to the environment variables. As an example:

var age int

flag.IntVar(&age, "age", 27, "my age")
flag.SetEnvPrefix("svc")

And then be able to do the following:

$ SVC_AGE=25 go run gopher.go

What do you think ?

Yes, this would be fantastic! Generally options on the command line don't need any context, but environment variables can easily overlap common terms like host, address, etc.

fancyapp --address=127.0.0.1 vs. FANCY_ADDRESS=127.0.0.1 fancyapp

I think this will do it: #24

Any ETA on the merge ?

I'm just another user, but it's already possible to do either:

flag.CommandLine = flag.NewFlagSetWithEnvPrefix("my-set", "MY_PREFIX", flag.ExitOnError)
...
flag.Parse()

Or to use your own FlagSet directly:

set := flag.NewFlagSetWithEnvPrefix("my-set", "MY_PREFIX", flag.ExitOnError)
...
set.Parse(os.Args[1:])

@smyrman Hey you're right - wow that function was hidden in extras and I never saw it. 😄

Indeed.
It was just missing from the doc.

@smyrman: kudos to you