dotnet / dotnet/command-line-api

Reading option values from environment variables

Open
#1,191 6 comments 8 reactions 0 assignees View on GitHub
Area-Help Area-Parser and Binder enhancement
Dominant language
C#
Stars
3.7k
Forks
428
PR merge metrics
No merged PRs in 30d

Description

Can I specify an environment variable to use as a source for an option's value if the option was not specified on the command line?

The simplest way I have found so far is:
```
new Option("--some-option", () => Environment.GetEnvironmentVariable("SOME_OPTION"), "description...")
```
However, this runs into a couple of hurdles:
- The environment variable is expanded and shown in the help. This might be an issue if the environment variable is being used on sensitive options like `--password`, as typing in a command incorrectly could print the option to standard out.
- The name of the environment variable isn't printed in the help unless it is manually added to the description.

It would be nice if I could do something like:
```
new Option("--some-option", () => "default-value", "description...") {
EnvironmentVariable = "SOME_OPTION", //Perhaps this could be an array instead?
};
```
The help builder could then present the environment variable information in a consistent & nice way like it does for defaults.

The issue with this is that it seems like the parser reading environment variables might be undesirable. An alternative may be to increase customization around defaults to let us print out default info differently with the existing help?

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.