Ask questionsProposal: Adding support for environment vars
I'm planning to integrate flaggy into an existing project, replacing the currently used urfave/cli package. This existing program also provides the option to pass in options via environment variables. The env part now could be solved on its own. But I was thinking if it wasn't a cool feature for flaggy itself?
It could look something like this:
flaggy.DefaultParser.EnableEnvFlags() flaggy.String(&stringFlag, "f", "flag", "A test string flag")
--flag was not passed to the program, we would use the env var
FLAG if it is set.
flaggy.DefaultParser.EnableEnvFlagsWithPrefix("MYAPP") flaggy.String(&stringFlag, "f", "flag", "A test string flag")
With a prefix we would look for
Precedence should be clear:
Lowercase flags would always relate to UPPERCASE env vars. Hyphens would be replaced with underscores. CamelCase will be split into separate words. So
--someFlag would relate to
By default the feature should be disabled, as I can see it could cause problems in some situations, where flags are named like common bash vars, e.g.
I'm not sure about subcommand flags. For my use case I would only need to add support for global flags - for functionality that would trigger special functionality on a global level, like
MYAPP_DEBUG=1. But IF we want that feature as well for subcommand flags, I guess it will make sense to include the subcommand name into the env var name, to avoid conflicts with flags from other subcommands.
flaggy.DefaultParser.EnableEnvFlagsWithPrefix("MYAPP") flaggy.String(&stringFlagA, "a", "flagA", "A test string flag (A)") subcommand := flaggy.NewSubcommand("subcommandExample") subcommand.String(&stringFlagB, "b", "flagB", "A test string flag (B)")
Corresponding env vars would be:
Happy to work on this. Just wanted to check if you were interested in this feature or would reject it.
I also do like the way urfave/cli has support for custom env vars per flag. But I don't see how we could implement this in a backwards compatible way into flaggy. The function signature of all flag functions would change.
Answer questions udondan
One thing to keep in mind is, that with
var myFlag = os.Getenv("MY_FLAG"), the help message would show the value of the env var as default value. This might be confusing, since it is a not a fixed default.
On the other hand this might also be used to the users advantage, e.g.
var flagLang = os.Getenv("LANG") var flagTime = os.Getenv("LC_TIME") ...
Related questionsNo questions were found.