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

Commit e2610bd

Browse files
author
Adam Byrd
committed
Allow null Option.DefaultValue
1 parent 851779a commit e2610bd

File tree

5 files changed

+37
-15
lines changed

5 files changed

+37
-15
lines changed

src/CommandLine.Tests/CommandLine.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
<Link>Properties\SharedAssemblyInfo.cs</Link>
6060
</Compile>
6161
<Compile Include="Fakes\FakeOptionsWithNullable.cs" />
62+
<Compile Include="Fakes\FakeOptionsWithNullDefault.cs" />
6263
<Compile Include="Fakes\FakeOptionsWithTwoIntegers.cs" />
6364
<Compile Include="Fakes\FakeInterfaceOptions.cs" />
6465
<Compile Include="Fakes\FakeOptionsWithHelpTextEnum.cs" />
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using System.Collections.Generic;
2+
3+
namespace CommandLine.Tests.Fakes
4+
{
5+
class FakeOptionsWithNullDefault
6+
{
7+
[Option('i', DefaultValue = null)]
8+
public IEnumerable<int> IntSequence
9+
{
10+
get { return intSequence; }
11+
set { intSequence = value; }
12+
}
13+
private IEnumerable<int> intSequence;
14+
}
15+
}

src/CommandLine.Tests/Unit/ParserTests.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,5 +247,19 @@ public void Parse_nullable_options()
247247
Assert.False(result.Errors.Any());
248248
// Teardown
249249
}
250+
251+
[Fact]
252+
public void Parse_allow_null_default_value()
253+
{
254+
// Fixture setup
255+
var sut = new Parser();
256+
257+
// Exercize system
258+
var result = sut.ParseArguments<FakeOptionsWithNullDefault>(new string[0]);
259+
260+
// Verify outcome
261+
Assert.Null(result.Value.IntSequence);
262+
// Teardown
263+
}
250264
}
251265
}

src/CommandLine/Core/InstanceBuilder.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -80,12 +80,12 @@ public static ParserResult<T> Build<T>(
8080
sp => sp.Value.FromJust())
8181
.SetProperties(specPropsWithValue,
8282
sp => sp.Value.IsNothing() && sp.Specification.DefaultValue.IsJust(),
83-
sp => sp.Specification.DefaultValue.FromJust())
84-
.SetProperties(specPropsWithValue,
85-
sp => sp.Value.IsNothing()
86-
&& sp.Specification.ConversionType.ToDescriptor() == DescriptorType.Sequence
87-
&& sp.Specification.DefaultValue.MatchNothing(),
88-
sp => sp.Property.PropertyType.GetGenericArguments().Single().CreateEmptyArray());
83+
sp => sp.Specification.DefaultValue.FromJust());
84+
//.SetProperties(specPropsWithValue,
85+
// sp => sp.Value.IsNothing()
86+
// && sp.Specification.ConversionType.ToDescriptor() == DescriptorType.Sequence
87+
// && sp.Specification.DefaultValue.MatchNothing(),
88+
// sp => sp.Property.PropertyType.GetGenericArguments().Single().CreateEmptyArray());
8989

9090
var validationErrors = specPropsWithValue.Validate(SpecificationPropertyRules.Lookup)
9191
.OfType<Just<Error>>().Select(e => e.Value);

src/CommandLine/OptionAttribute.cs

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -153,15 +153,7 @@ public int Max
153153
public object DefaultValue
154154
{
155155
get { return this.defaultValue; }
156-
set
157-
{
158-
if (value == null)
159-
{
160-
throw new ArgumentNullException("value");
161-
}
162-
163-
this.defaultValue = value;
164-
}
156+
set { this.defaultValue = value; }
165157
}
166158

167159
/// <summary>

0 commit comments

Comments
 (0)