dotnet / dotnet/command-line-api
Question: How to show default/fallback value in Commandline help when using multiple configuration providers
- Dominant language
- C#
- Stars
- 3.7k
- Forks
- 428
- PR merge metrics
- No merged PRs in 30d
Description
I am using commandline in hosting and binding the configuration values, from a json configuration file and some values from commandline - [multiple configuration providers.](https://docs.microsoft.com/en-us/aspnet/core/fundamentals/configuration/?view=aspnetcore-5.0#configuration-providers)
I have a Configuration:
```csharp
internal class AaaCommandOptions : CommandOptionsBase
{
public static readonly ReportOutputFormat DefaultOutputFormat = ReportOutputFormat.PDF;
public static readonly string DefaultOutputDirector = @"%TEMP%\ABC";
public static readonly string DefaultCssFile = @"Default.css";
[Required]
public string ReportName { get; set; }
public string OutputDirectory { get; set; }
public string CssFile { get; set; } = DefaultCssFile;
public ReportOutputFormat OutputFormat { get; set; } = DefaultOutputFormat;
}
```
and i am Binding the values like that:
```csharp
host.ConfigureServices((ctx, services) =>
{
services.AddOptions()
.Bind(ctx.Configuration)
.BindCommandLine()
.ValidateDataAnnotations();
```
What I want to achieve is: having a possibility to describe a default (fall back value) which can be overwritten in either the config file (via environment variable) or at latest via commandline (if a option exists)
> A common practice is to add the Command-line configuration provider last in a series of providers to allow command-line arguments to override configuration set by the other providers.
What i also want to have is to show the default value in the command line help.
If i use the `Func getDefaultValue`:
```csharp
command.AddOption(new Option(new[]
{
$"--{nameof(AaaCommandOptions.OutputDirectory)}", "-o"
},
() => AaaCommandOptions.DefaultOutputDirectory)
{
Description = "Storage path",
});
```
This shows nicely the default value in the help output, but it also sets the value if the option is not provided on command line.
So the Fallback value is always set if no other value is provided via command line.
As workaround I am doing today something like that
```csharp
command.AddOption(new Option(new[]
{
$"--{nameof(AaaCommandOptions.OutputDirectory)}", "-o"
})
{
Description = $"Storage path. Default: {AaaCommandOptions.DefaultOutputDirectory}",
});
```
What would be a recommended way?
Contributor guide
Assessment
This issue has not been assessed yet.