commandline
commandline copied to clipboard
Enum list options not displayed in help text
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
- Create an option of type
IEnummerable<{Enum}> - 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);
});
- 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
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.