Skip to content
This repository was archived by the owner on Jul 11, 2018. It is now read-only.

Commit 851779a

Browse files
author
Adam Byrd
committed
Fixed failed parsing on nullable options
Added unit test for nullable types
1 parent e0d0505 commit 851779a

File tree

4 files changed

+34
-0
lines changed

4 files changed

+34
-0
lines changed

src/CommandLine.Tests/CommandLine.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
<Compile Include="..\SharedAssemblyInfo.cs">
5959
<Link>Properties\SharedAssemblyInfo.cs</Link>
6060
</Compile>
61+
<Compile Include="Fakes\FakeOptionsWithNullable.cs" />
6162
<Compile Include="Fakes\FakeOptionsWithTwoIntegers.cs" />
6263
<Compile Include="Fakes\FakeInterfaceOptions.cs" />
6364
<Compile Include="Fakes\FakeOptionsWithHelpTextEnum.cs" />
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
namespace CommandLine.Tests.Fakes
2+
{
3+
class FakeOptionsWithNullable
4+
{
5+
[Option('n')]
6+
public int? NullableIntValue { get; set; }
7+
8+
[Option('c')]
9+
public Colors? NullableColorsValue { get; set; }
10+
}
11+
}

src/CommandLine.Tests/Unit/ParserTests.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,5 +227,25 @@ public void Parse_verbs_using_generic_overload()
227227
Assert.False(result.Errors.Any());
228228
// Teardown
229229
}
230+
231+
[Fact]
232+
public void Parse_nullable_options()
233+
{
234+
// Fixture setup
235+
var expectedOptions = new FakeOptionsWithNullable
236+
{
237+
NullableIntValue = 60,
238+
NullableColorsValue = Colors.Red
239+
};
240+
var sut = new Parser();
241+
242+
// Exercize system
243+
var result = sut.ParseArguments<FakeOptionsWithNullable>(new[] { "-n", "60", "-c", "Red" });
244+
245+
// Verify outcome
246+
result.Value.ShouldHave().AllProperties().EqualTo(expectedOptions);
247+
Assert.False(result.Errors.Any());
248+
// Teardown
249+
}
230250
}
231251
}

src/CommandLine/Core/TypeConverter.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ private static Maybe<object> ChangeType(string value, Type conversionType, Cultu
4141
{
4242
try
4343
{
44+
conversionType = Nullable.GetUnderlyingType(conversionType) ?? conversionType;
45+
4446
return Maybe.Just(
4547
MatchBoolString(value)
4648
? ConvertBoolString(value)

0 commit comments

Comments
 (0)