cobra icon indicating copy to clipboard operation
cobra copied to clipboard

Idea: Integrate environment variable management into Cobra

Open geeknoid opened this issue 5 years ago • 5 comments

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.

geeknoid avatar Nov 20 '18 19:11 geeknoid

This issue is being marked as stale due to a long period of inactivity

github-actions[bot] avatar Apr 07 '20 00:04 github-actions[bot]

+1024

TommyLike avatar Oct 13 '21 05:10 TommyLike

+1024

TommyLike avatar Oct 13 '21 05:10 TommyLike

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.

johnSchnake avatar Mar 07 '22 15:03 johnSchnake

Kingpin supports defining environment variable to override the default of a argument, see: https://pkg.go.dev/gopkg.in/alecthomas/kingpin.v2#ArgClause.Envar

weyert avatar Jun 02 '22 13:06 weyert