Usage Example only treats an Option of type Enum with inner value of 0 as default, regardless of Enum value chosen for Example (demo repo provided)
Please clone and execute this repo to see the problem in action.
This is similar to, but distinct from, issue #379.
Steps to re-pro
- Start with the "getting started" console app example on the project README.
- Add a verb via an option class with the
Verbattribute. - Add an
Optionof typeEnumwith at least two values, each with a default (not assigned) inner value (i.e. 0 and 1, respectively). - Set the
Defaultproperty of theOptionto an enum value other than the one assigned to 0. - Create a
Usagesection with twoExamples, one with the firstEnumoption chosen, another with any otherEnumoption chosen. - Run the program with the help text showing.

Expected behavior
Where the usage Example chooses the default value for the Option, the Option is omitted from the usage as it is redundant. If a different value from default is chosen for the Option, the specified option is displayed in the usage, since it's different.
Actual behavior
Only when the Enum inner value for the option is 0 will the usage treat it as the default value and omit it from the usage, regardless of whether the default value is chosen for a specific example.
Try changing the Enum such that each value is assigned something other than 0, like 1 and 2, then re-run the program with usage showing.

You'll notice that the option is always displayed, regardless of whether the chosen option matches the default.

It appears that when the Enum type is used for an Option, the Default for that option is not respected when it comes to displaying an Example under Usage on the help screen.
Agreed, it is a big problem and more simpler : ENUM with value 0 just doesn't display in USAGE. In order to see enum in usage, they must start with 1, but it's bad, as it force us to change the enum codes. Enum that start with 0 are not display in USAGE. Please dev, fix it, it's very annoying and it's like that since a long time.
@jdmallen , @obewan Do you mean that the expected result should be:
USAGE:
Run with enum option One selected:
CommandLineParserUsageBug.exe default --option OptionOne
Run with enum option Two selected:
CommandLineParserUsageBug.exe default //omited for it's the default value
by applying this Rule:
When option Type is Enum and has Default value then
Don't show option value in USAGE
It's fixed in v2.8.0
There is a new feature to skip the default values for enum.
Modify the code and use the Example constructor that provide the formatStyle, as below:
[Verb("default")]
public class DefaultOptions : IOptions
{
public string RequiredOption { get; set; }
public EnumOptions FakeOption { get; set; }
[Usage(ApplicationAlias = "CommandLineParserUsageBug.exe")]
public static IEnumerable<Example> Examples
{
get
{
var formatStyle = new List<UnParserSettings> { new UnParserSettings { SkipDefault = true } };
yield return new Example(
"Run with enum option One selected",
formatStyle,
new DefaultOptions
{
FakeOption = EnumOptions.OptionOne
});
yield return new Example(
"Run with enum option Two selected",
formatStyle,
new DefaultOptions
{
FakeOption = EnumOptions.OptionTwo
}
);
}
}
for the enum,
public enum EnumOptions
{
OptionOne=1,
OptionTwo
}
you get the help:
DemoClp 1.0.0
Copyright (C) 2020 DemoClp
ERROR(S):
Required option 'r, req' is missing.
USAGE:
Run with enum option One selected:
CommandLineParserUsageBug.exe default --option OptionOne
Run with enum option Two selected:
CommandLineParserUsageBug.exe default
-r, --req Required. fake required option to force usage to show
-o, --option (Default: OptionTwo) OptionTwo is set as default, but
OptionOne is never specified (as it would need to be) in the
usage examples.
--help Display this help screen.
--version Display version information.