Skip to content

Commit e33eb5e

Browse files
committed
Support parsing nullable enums
1 parent 8b8d96b commit e33eb5e

File tree

3 files changed

+27
-7
lines changed

3 files changed

+27
-7
lines changed

src/CommandLineUtils/Internal/ValueParserProvider.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,12 @@ public IValueParser GetParser(Type type)
4848
if (typeInfo.IsGenericType && typeInfo.GetGenericTypeDefinition() == typeof(Nullable<>))
4949
{
5050
var wrappedType = type.GetTypeInfo().GetGenericArguments().First();
51+
52+
if (wrappedType.GetTypeInfo().IsEnum)
53+
{
54+
return new NullableValueParser(new EnumParser(wrappedType));
55+
}
56+
5157
if (_parsers.TryGetValue(wrappedType, out parser))
5258
{
5359
return new NullableValueParser(parser);

src/CommandLineUtils/Properties/Strings.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,19 +41,19 @@ public static string DuplicateSubcommandName(string commandName)
4141
=> $"The subcommand name '{commandName}' has already been been specified. Subcommand names are case insensitive and must be unique.";
4242

4343
public static string BothOptionAndArgumentAttributesCannotBeSpecified(PropertyInfo prop)
44-
=> $"Cannot specify both {nameof(OptionAttribute)} and {nameof(ArgumentAttribute)} on a property {prop.DeclaringType.Name}.{prop.Name}.";
44+
=> $"Cannot specify both {nameof(OptionAttribute)} and {nameof(ArgumentAttribute)} on property {prop.DeclaringType.Name}.{prop.Name}.";
4545

4646
public static string BothOptionAndHelpOptionAttributesCannotBeSpecified(PropertyInfo prop)
47-
=> $"Cannot specify both {nameof(OptionAttribute)} and {nameof(HelpOptionAttribute)} on a property {prop.DeclaringType.Name}.{prop.Name}.";
47+
=> $"Cannot specify both {nameof(OptionAttribute)} and {nameof(HelpOptionAttribute)} on property {prop.DeclaringType.Name}.{prop.Name}.";
4848

4949
public static string BothOptionAndVersionOptionAttributesCannotBeSpecified(PropertyInfo prop)
50-
=> $"Cannot specify both {nameof(OptionAttribute)} and {nameof(VersionOptionAttribute)} on a property {prop.DeclaringType.Name}.{prop.Name}.";
50+
=> $"Cannot specify both {nameof(OptionAttribute)} and {nameof(VersionOptionAttribute)} on property {prop.DeclaringType.Name}.{prop.Name}.";
5151

5252
internal static string UnsupportedParameterTypeOnMethod(string methodName, ParameterInfo methodParam)
5353
=> $"Unsupported type on {methodName} '{methodParam.ParameterType.FullName}' on parameter {methodParam.Name}";
5454

5555
public static string BothHelpOptionAndVersionOptionAttributesCannotBeSpecified(PropertyInfo prop)
56-
=> $"Cannot specify both {nameof(HelpOptionAttribute)} and {nameof(VersionOptionAttribute)} on a property {prop.DeclaringType.Name}.{prop.Name}.";
56+
=> $"Cannot specify both {nameof(HelpOptionAttribute)} and {nameof(VersionOptionAttribute)} on property {prop.DeclaringType.Name}.{prop.Name}.";
5757

5858
public static string DuplicateArgumentPosition(int order, PropertyInfo first, PropertyInfo second)
5959
=> $"Duplicate value for argument order. Both {first.DeclaringType.FullName}.{first.Name} and {second.DeclaringType.FullName}.{second.Name} have set Order = {order}";
@@ -62,7 +62,7 @@ public static string OnlyLastArgumentCanAllowMultipleValues(string lastArgName)
6262
=> $"The last argument '{lastArgName}' accepts multiple values. No more argument can be added.";
6363

6464
public static string CannotDetermineParserType(PropertyInfo prop)
65-
=> $"Could not automatically determine how to convert string values into {prop.PropertyType.FullName}.";
65+
=> $"Could not automatically determine how to convert string values into {prop.PropertyType.FullName} on property {prop.DeclaringType.Name}.{prop.Name}.";
6666

6767
public static string MultipleValuesArgumentShouldBeCollection
6868
= "ArgumentAttribute.MultipleValues should be true if the property type is an array or collection.";

test/CommandLineUtils.Tests/ValueParserProviderTests.cs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,10 @@ private class Program
7777
public ISet<string> StringSet { get; }
7878

7979
[Option("--color")]
80-
public Color ColorOption { get; }
80+
public Color Color { get; }
81+
82+
[Option("--color-opt")]
83+
public Color? ColorOpt { get; }
8184
}
8285

8386
public static IEnumerable<object[]> GetFloatingPointSymbolsData()
@@ -296,7 +299,18 @@ public void ParsesStringSet()
296299
public void ParsesEnum(Color color)
297300
{
298301
var parsed = CommandLineParser.ParseArgs<Program>("--color", color.ToString().ToLowerInvariant());
299-
Assert.Equal(color, parsed.ColorOption);
302+
Assert.Equal(color, parsed.Color);
303+
}
304+
305+
[Theory]
306+
[InlineData(Color.Blue)]
307+
[InlineData(Color.Red)]
308+
[InlineData(Color.Green)]
309+
public void ParsesNullableEnum(Color? color)
310+
{
311+
var parsed = CommandLineParser.ParseArgs<Program>("--color-opt", color.ToString().ToLowerInvariant());
312+
Assert.True(parsed.ColorOpt.HasValue, "Option should have value");
313+
Assert.Equal(color, parsed.ColorOpt);
300314
}
301315

302316
private class ArgumentProgram

0 commit comments

Comments
 (0)