Skip to content

Environment variables

Adam Bajguz edited this page Aug 22, 2020 · 7 revisions

An option can be configured to use the value of an environment variable as a fallback. If the value for such an option is not directly specified in the arguments, it will be extracted from that environment variable instead.

Here's an example of a required option that can be either provided directly or extracted from the environment:

[Command]
public class AuthCommand : ICommand
{
    [CommandOption("token", IsRequired = true, EnvironmentVariableName = "AUTH_TOKEN")]
    public string AuthToken { get; set; }

    public ValueTask ExecuteAsync(IConsole console)
    {
        console.Output.WriteLine(AuthToken);

        return default;
    }
}
> $env:AUTH_TOKEN="test"

> myapp.exe

test

Environment variables can be used as fallback for options of enumerable types too. In this case, the value of the variable will be split by Path.PathSeparator (which is ; on Windows, : on Linux).

Clone this wiki locally