Skip to content

Enum with 0 value not shown on Example #379

@DimonSmart

Description

@DimonSmart

public class MyOption
{
[Option('t', "type", Required = true, HelpText = "My entity")]
public EntityType MyEntityType { get; set; }
public enum MyEntityType
{
X0, // Example for X0 is empty. If change this line to X0 =1, everything is ok
X1,
X2
}

    [Usage(ApplicationAlias = "...")]
    public static IEnumerable<Example> Examples
    {
        get
        {
            foreach (EntityType entityType in Enum.GetValues(typeof(EntityType)))
            {
                yield return new Example(entityType.ToString(), new MyOption { MyEntityType = entityType});
            }
        }
    }
}

Activity

nemec

nemec commented on Dec 8, 2018

@nemec
Contributor

This looks like a bug around examples with Required set to true. The example is empty because X0 is the default value for this enum - if you remove Required you'll find that myapp.exe with no arguments sets MyEntityType to X0 automatically. myapp.exe and myapp.exe -t X0 produce the exact same result.

But the example generator should look at whether the option is required and include it whether or not it's the default value.

added this to the 2.6 milestone on Jun 2, 2019
njqdev

njqdev commented on Jun 2, 2019

@njqdev

I took a look at this again and noticed that other value types have the same issue.

   class Options_With_Examples_Having_Default_Values
    {
        [Option('t', "type", HelpText = "My entity")]
        public EntityType MyEntityType { get; set; }

        public enum EntityType
        {
            T0
        }

        [Option('v', "value")]
        public int Value { get; set; }
        [Option('d')]
        public double Double { get; set; }
        [Option('b')]
        public bool Bool { get; set; }

        [Usage(ApplicationAlias = "test.exe")]
        public static IEnumerable<Example> Examples
        {
            get
            {
                yield return new Example("1", new Options_With_Examples_Having_Default_Values { MyEntityType = EntityType.T0 });
                yield return new Example("2", new Options_With_Examples_Having_Default_Values { Value = 1, Double = 0.1, Bool = true });
            }
        }
    }

Returns

1:
  test.exe --type T0
2:
  test.exe --type T0

and the same example with non-default values returns

1:
  test.exe --type T0
2:
  test.exe -d 0,1 --type T0 --value 1

The enum is included in both examples too, because of the change I made in #452.

shubhiroy

shubhiroy commented on Aug 6, 2023

@shubhiroy

If you are looking for help on this.. I would be glad to take it up @nemec @ericnewton76 @moh-hassan

It would be my first open-source contribution

Thanks In Advance :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @nemec@ericnewton76@moh-hassan@DimonSmart@shubhiroy

        Issue actions

          Enum with 0 value not shown on Example · Issue #379 · commandlineparser/commandline