commandline icon indicating copy to clipboard operation
commandline copied to clipboard

Enum list options not displayed in help text

Open joshmar opened this issue 2 years ago • 1 comments

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 avatar Jun 19 '23 10:06 joshmar

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.

joshmar avatar Jun 19 '23 11:06 joshmar