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

Show allowed values in help text for Nullable/ValueTuple<bool,T> option/argument #390

Merged
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
19 changes: 17 additions & 2 deletions src/CommandLineUtils/HelpText/DefaultHelpTextGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -402,12 +402,27 @@ protected virtual string Format(CommandOption option)

private string[] ExtractNamesFromEnum(Type type)
{
if (type == null || !type.IsEnum)
if (type == null)
{
return Util.EmptyArray<string>();
}

return Enum.GetNames(type);
if (ReflectionHelper.IsNullableType(type, out Type wrappedType))
{
return ExtractNamesFromEnum(wrappedType);
}

if (ReflectionHelper.IsSpecialValueTupleType(type, out var fieldInfo))
{
return ExtractNamesFromEnum(fieldInfo.FieldType);
}

if (type.IsEnum)
{
return Enum.GetNames(type);
}

return Util.EmptyArray<string>();
}
}
}
10 changes: 10 additions & 0 deletions src/CommandLineUtils/Internal/ReflectionHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,16 @@ public static bool IsNullableType(Type type, out Type? wrappedType)
return result;
}

public static bool IsSpecialValueTupleType(Type type, out FieldInfo fieldInfo)
{
var result = type.IsGenericType &&
type.GetGenericTypeDefinition() == typeof(ValueTuple<,>) &&
type.GenericTypeArguments[0] == typeof(bool);
fieldInfo = result ? type.GetFields()[1] : null;

return result;
}

private static IEnumerable<MemberInfo> GetAllMembers(Type type)
{
while (type != null)
Expand Down
36 changes: 30 additions & 6 deletions test/CommandLineUtils.Tests/DefaultHelpTextGeneratorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -112,14 +112,17 @@ public void ShowHelp()
app.Option("--rStrOpt <E>", "restricted str option desc.", CommandOptionType.SingleValue, o => o.IsRequired().Accepts().Values("Foo", "Bar"));
app.Option<int>("--intOpt <E>", "int option desc.", CommandOptionType.SingleValue);
app.Option<SomeEnum>("--enumOpt <E>", "enum option desc.", CommandOptionType.SingleValue);
app.Option<SomeEnum>("--rEnumOpt <E>", "restricted enum option desc.", CommandOptionType.SingleValue, o => o.Accepts().Values("None", "Normal"));
app.Option<SomeEnum>("--enumOpt2 <E>", "restricted enum option desc.", CommandOptionType.SingleValue, o => o.Accepts().Values("None", "Normal"));
app.Option<(bool, SomeEnum)>("--enumOpt3 <E>", "nullable enum option desc.", CommandOptionType.SingleOrNoValue);
app.Option<SomeEnum?>("--enumOpt4 <E>", "nullable enum option desc.", CommandOptionType.SingleOrNoValue);
app.Argument("SomeStringArgument", "string arg desc.");
app.Argument("RestrictedStringArgument", "restricted string arg desc.", a => a.IsRequired().Accepts().Values("Foo", "Bar"));
app.Argument<SomeEnum>("SomeEnumArgument", "enum arg desc.");
app.Argument<SomeEnum>("RestrictedEnumArgument", "restricted enum arg desc.", a => a.Accepts().Values("None", "Normal"));
app.Argument<(bool, SomeEnum)>("SomeNullableEnumArgument", "nullable enum arg desc.");
var helpText = GetHelpText(app);

Assert.Equal(@"Usage: [options] <SomeStringArgument> <RestrictedStringArgument> <SomeEnumArgument> <RestrictedEnumArgument>
Assert.Equal(@"Usage: [options] <SomeStringArgument> <RestrictedStringArgument> <SomeEnumArgument> <RestrictedEnumArgument> <SomeNullableEnumArgument>

Arguments:
SomeStringArgument string arg desc.
Expand All @@ -129,6 +132,8 @@ SomeEnumArgument enum arg desc.
Allowed values are: None, Normal, Extreme.
RestrictedEnumArgument restricted enum arg desc.
Allowed values are: None, Normal.
SomeNullableEnumArgument nullable enum arg desc.
Allowed values are: None, Normal, Extreme.

Options:
-?|-h|--help Show help information.
Expand All @@ -138,8 +143,12 @@ RestrictedEnumArgument restricted enum arg desc.
--intOpt <E> int option desc.
--enumOpt <E> enum option desc.
Allowed values are: None, Normal, Extreme.
--rEnumOpt <E> restricted enum option desc.
--enumOpt2 <E> restricted enum option desc.
Allowed values are: None, Normal.
--enumOpt3[:<E>] nullable enum option desc.
Allowed values are: None, Normal, Extreme.
--enumOpt4[:<E>] nullable enum option desc.
Allowed values are: None, Normal, Extreme.

",
helpText,
Expand All @@ -154,7 +163,7 @@ public void ShowHelpFromAttributes()
app.Conventions.UseDefaultConventions();
var helpText = GetHelpText(app);

Assert.Equal(@"Usage: test [options] <SomeStringArgument> <RestrictedStringArgument> <SomeEnumArgument> <RestrictedEnumArgument>
Assert.Equal(@"Usage: test [options] <SomeStringArgument> <RestrictedStringArgument> <SomeEnumArgument> <RestrictedEnumArgument> <SomeNullableEnumArgument>

Arguments:
SomeStringArgument string arg desc.
Expand All @@ -164,6 +173,8 @@ SomeEnumArgument enum arg desc.
Allowed values are: None, Normal, Extreme.
RestrictedEnumArgument restricted enum arg desc.
Allowed values are: None, Normal.
SomeNullableEnumArgument nullable enum arg desc.
Allowed values are: None, Normal, Extreme.

Options:
-strOpt|--str-opt <STR_OPT> str option desc.
Expand All @@ -172,8 +183,12 @@ RestrictedEnumArgument restricted enum arg desc.
-intOpt|--int-opt <INT_OPT> int option desc.
-enumOpt|--verbosity <VERBOSITY> enum option desc.
Allowed values are: None, Normal, Extreme.
-rEnumOpt|--verb2 <VERB2> restricted enum option desc.
-enumOpt2|--verb2 <VERB2> restricted enum option desc.
Allowed values are: None, Normal.
-enumOpt3|--verb3[:<VERB3>] nullable enum option desc.
Allowed values are: None, Normal, Extreme.
-enumOpt4|--verb4[:<VERB4>] nullable enum option desc.
Allowed values are: None, Normal, Extreme.
-?|-h|--help Show help information.

",
Expand All @@ -197,10 +212,16 @@ public class MyApp
[Option(ShortName = "enumOpt", Description = "enum option desc.")]
public SomeEnum Verbosity { get; set; }

[Option(ShortName = "rEnumOpt", Description = "restricted enum option desc.")]
[Option(ShortName = "enumOpt2", Description = "restricted enum option desc.")]
[AllowedValues("None", "Normal")]
public SomeEnum Verb2 { get; set; }

[Option(ShortName = "enumOpt3", Description = "nullable enum option desc.")]
public (bool HasValue, SomeEnum Value) Verb3 { get; set; }

[Option(CommandOptionType.SingleOrNoValue, ShortName = "enumOpt4", Description = "nullable enum option desc.")]
public SomeEnum? Verb4 { get; set; }

[Argument(0, Description = "string arg desc.")]
public string SomeStringArgument { get; set; }

Expand All @@ -215,6 +236,9 @@ public class MyApp
[Argument(3, Description = "restricted enum arg desc.")]
[AllowedValues("None", "Normal")]
public SomeEnum RestrictedEnumArgument { get; set; }

[Argument(4, Description = "nullable enum arg desc.")]
public (bool HasValue, SomeEnum Value) SomeNullableEnumArgument { get; set; }
}

[Theory]
Expand Down