Skip to content
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

Enum list options not displayed in help text #893

Open
joshmar opened this issue Jun 19, 2023 · 1 comment
Open

Enum list options not displayed in help text #893

joshmar opened this issue Jun 19, 2023 · 1 comment

Comments

@joshmar
Copy link

joshmar commented Jun 19, 2023

Apologies if this question/bug was already open

Describe the bug
The property: HelpText.AddenumValuesToHelpText does not work when the option is of type IEnummerable<{Enum}>. I'm guessing the list is masking the fact that its containing an enum.

Is it possible to have the same Valid values: {enum values} in the help text to be displayed when its a list of enums?

To Reproduce

  1. Create an option of type IEnummerable<{Enum}>
  2. configure a parser as follows:
var parser = new Parser(with =>
{
    with.CaseInsensitiveEnumValues = true;
    with.HelpWriter = null;
    with.AutoVersion = false;
});

    var parserResult = parser.ParseArguments<TArgs>(args.SplitArgs());
    parserResult
        .WithNotParsed(_ =>
        {
            var helpText = HelpText.AutoBuild(parserResult, h =>
            {
                h.AutoVersion = false;
                h.Heading = string.Empty;
                h.Copyright = string.Empty;
                h.AddEnumValuesToHelpText = true;

                return h;
            });
            Console.WriteLine(helpText);
        });
  1. use the --help to find no Valid values: {enum values} in the help text.

Expected behavior
I expected a list of enums to still show available options

Screenshots
image
image
image

@joshmar
Copy link
Author

joshmar commented Jun 19, 2023

Suggested solution

The issue is fixed when added the part below to:
CommandLine.Infrastructure.ReflectionHelper.GetNamesOfEnum(Type t)

public static IEnumerable<string> GetNamesOfEnum(Type t)
{
    if (t.IsEnum)
      return Enum.GetNames(t);
    Type u = Nullable.GetUnderlyingType(t);
    if (u != null && u.IsEnum)
      return Enum.GetNames(u);

    // ---Added this part---
    Type[] s = t.GetGenericArguments();
    if (s.Any())
      return GetNamesOfEnum(s.First());
    // ---

    return Enumerable.Empty<string>();
}

Notes

I have not tested/nor thought about any possible consequences this might have or cause.

All tests do still seem to pass.

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

1 participant