Skip to content

Peropely determine allowed values for multiple values arguments #553

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/CommandLineUtils/HelpText/DefaultHelpTextGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,11 @@ private string[] ExtractNamesFromEnum(Type? type)
return ExtractNamesFromEnum(wrappedType2);
}

if (ReflectionHelper.IsEnumerableType(type, out var wrappedEnumerableType))
{
return ExtractNamesFromEnum(wrappedType2);
}

if (type.IsEnum)
{
return Enum.GetNames(type);
Expand Down
9 changes: 9 additions & 0 deletions src/CommandLineUtils/Internal/ReflectionHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,15 @@ public static bool IsSpecialTupleType(Type type, [NotNullWhen(true)] out Type? w
return result;
}

public static bool IsEnumerableType(Type type, [NotNullWhen(true)] out Type? wrappedType)
{
var result = type.IsGenericType &&
type.IsAssignableTo(typeof(IEnumerable<>));
wrappedType = result ? type.GenericTypeArguments[1] : null;

return result;
}

private static IEnumerable<MemberInfo> GetAllMembers(Type type)
{
while (type != null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ public void ShowHelp()
});
app.Option<(bool, SomeEnum)>("--enumOpt3 <E>", "nullable enum option desc.", CommandOptionType.SingleOrNoValue);
app.Option<SomeEnum?>("--enumOpt4 <E>", "nullable enum option desc.", CommandOptionType.SingleOrNoValue);
app.Option<SomeEnum[]>("--enumOpt5 <E>", "multiple enum option desc.", CommandOptionType.MultipleValue);
app.Argument("SomeStringArgument", "string arg desc.");
app.Argument("RestrictedStringArgument", "restricted string arg desc.", a => a.IsRequired().Accepts().Values("Foo", "Bar"));
app.Argument("DefaultValStringArgument", "string arg with default value desc.", a => a.DefaultValue = "Foo");
Expand Down Expand Up @@ -164,6 +165,8 @@ SomeNullableEnumArgument nullable enum arg desc.
Allowed values are: None, Normal, Extreme.
--enumOpt4[:<E>] nullable enum option desc.
Allowed values are: None, Normal, Extreme.
--enumOpt5[:<E>] multiple enum option desc.
Allowed values are: None, Normal, Extreme.

",
helpText,
Expand Down