dotnet / dotnet/command-line-api
Don't show empty default values in help
- Dominant language
- C#
- Stars
- 3.7k
- Forks
- 428
- PR merge metrics
- No merged PRs in 30d
Description
Consider this application:
```c#
static int Main(string[] args)
{
var rootCommand = new RootCommand
{
new Argument("work-dir", () => null, "The working directory"),
new Option("--base-dir", "The base directory")
{
IsRequired = false,
}
};
rootCommand.Handler = CommandHandler.Create((workDir, baseDir) =>
{
Console.WriteLine($"The value for 'work-dir' is: {workDir}");
Console.WriteLine($"The value for '--base-dir' is: {baseDir}");
});
return rootCommand.Invoke(args);
}
```
It defines a single positional parameter (`work-dir`) that's optional and an option `--base-dir` that's also optional.
When executing this application with the `--help` parameter, it generates something like this:
```
Usage:
CommandLineReproducer [options] []
Arguments:
The working directory [default: ]
Options:
--base-dir The base directory
--version Show version information
-?, -h, --help Show help and usage information
```
Notice how the description for `` says `[default: ]`.
I've looked but I couldn't find a way to hide this output.
With the `Option` I can remove the default value and set `IsRequired = false` to achieve this. But with `Argument` the default value seems to be the only way to define whether the argument is optional or required.
My suggestion is add the condition `argument.GetDefaultValue() != null` to `HelpBuilder.ShouldShowDefaultValueHint()` like this:
```c#
internal bool ShouldShowDefaultValueHint(IArgument argument)
{
return argument.HasDefaultValue && ShouldShowHelp(argument) && argument.GetDefaultValue() != null;
}
```
However, I'm not sure whether this has any other side effects.
Contributor guide
Assessment
This issue has not been assessed yet.