Skip to content

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) #457

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
jdmallen opened this issue Jun 6, 2019 · 3 comments

Comments

@jdmallen
Copy link

jdmallen commented Jun 6, 2019

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

  1. Start with the "getting started" console app example on the project README.
  2. Add a verb via an option class with the Verb attribute.
  3. Add an Option of type Enum with at least two values, each with a default (not assigned) inner value (i.e. 0 and 1, respectively).
  4. Set the Default property of the Option to an enum value other than the one assigned to 0.
  5. Create a Usage section with two Examples, one with the first Enum option chosen, another with any other Enum option chosen.
  6. Run the program with the help text showing.

image

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.

image

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

image

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.

@obewan
Copy link

obewan commented Sep 3, 2019

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.

@moh-hassan
Copy link
Collaborator

@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

@moh-hassan
Copy link
Collaborator

moh-hassan commented Jun 8, 2020

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants