cobra
cobra copied to clipboard
Idea: Integrate environment variable management into Cobra
Use of Cobra provides consistent user experience for CLI users, and makes it simple to generate high-quality user docs for commands.
Imagine if you could also describe to Cobra the environment variables consumed by the program. These could be scoped so they can be global to the whole command, or specific to a subcommand. The interactive help would display the available env vars with descriptions. The programmer could supply default values if env vars aren't set, etc.
Seems like this would be a nice complement to the features already in place.
This issue is being marked as stale due to a long period of inactivity
+1024
+1024
I don't disagree with this use case. I don't often see other CLIs put those in the help but after using lots of different tools, I WISH they would.
That being said, viper is already doing the actual binding of variables to values. Likewise you can make your own usage string so that env vars are listed there.
So the feature request here is simply to have cobra's default usage string include information about env vars. Implementation-wise you could have a function just like for adding flags that 'adds' the env vars to the function. For parity it seems like you'd have to also implement persistent env vars as well and deal with merging them like the flags.
I'm open to reviewing a PR but other maintainers may feel differently. Going to tag and leave open despite the age.
Kingpin supports defining environment variable to override the default of a argument, see: https://pkg.go.dev/gopkg.in/alecthomas/kingpin.v2#ArgClause.Envar