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

add support for flags enums #623

Merged
merged 1 commit into from
Jun 15, 2020
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
11 changes: 10 additions & 1 deletion src/CommandLine/Core/TypeConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -129,11 +129,20 @@ private static object ToEnum(this string value, Type conversionType, bool ignore
{
throw new FormatException();
}
if (Enum.IsDefined(conversionType, parsedValue))
if (IsDefinedEx(parsedValue))
{
return parsedValue;
}
throw new FormatException();
}

private static bool IsDefinedEx(object enumValue)
{
char firstChar = enumValue.ToString()[0];
if (Char.IsDigit(firstChar) || firstChar == '-')
return false;

return true;
}
}
}
20 changes: 20 additions & 0 deletions tests/CommandLine.Tests/Unit/Core/TypeConverterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@ enum TestEnum
ValueB = 2
}

[Flags]
enum TestFlagEnum
{
ValueA = 0x1,
ValueB = 0x2
}

[Theory]
[MemberData(nameof(ChangeType_scalars_source))]
public void ChangeType_scalars(string testValue, Type destinationType, bool expectFail, object expectedResult)
Expand Down Expand Up @@ -94,6 +101,19 @@ public static IEnumerable<object[]> ChangeType_scalars_source
new object[] {((int) TestEnum.ValueB + 1).ToString(), typeof (TestEnum), true, null},
new object[] {((int) TestEnum.ValueA - 1).ToString(), typeof (TestEnum), true, null},

new object[] {"ValueA", typeof (TestFlagEnum), false, TestFlagEnum.ValueA},
new object[] {"VALUEA", typeof (TestFlagEnum), false, TestFlagEnum.ValueA},
new object[] {"ValueB", typeof(TestFlagEnum), false, TestFlagEnum.ValueB},
new object[] {"ValueA,ValueB", typeof (TestFlagEnum), false, TestFlagEnum.ValueA | TestFlagEnum.ValueB},
new object[] {"ValueA, ValueB", typeof (TestFlagEnum), false, TestFlagEnum.ValueA | TestFlagEnum.ValueB},
new object[] {"VALUEA,ValueB", typeof (TestFlagEnum), false, TestFlagEnum.ValueA | TestFlagEnum.ValueB},
new object[] {((int) TestFlagEnum.ValueA).ToString(), typeof (TestFlagEnum), false, TestFlagEnum.ValueA},
new object[] {((int) TestFlagEnum.ValueB).ToString(), typeof (TestFlagEnum), false, TestFlagEnum.ValueB},
new object[] {((int) (TestFlagEnum.ValueA | TestFlagEnum.ValueB)).ToString(), typeof (TestFlagEnum), false, TestFlagEnum.ValueA | TestFlagEnum.ValueB},
new object[] {((int) TestFlagEnum.ValueB + 2).ToString(), typeof (TestFlagEnum), true, null},
new object[] {((int) TestFlagEnum.ValueA - 1).ToString(), typeof (TestFlagEnum), true, null},


// Failed before #339
new object[] {"false", typeof (int), true, 0},
new object[] {"true", typeof (int), true, 0}
Expand Down