Skip to content

Commit a904457

Browse files
committed
additional Option<T> constructors
1 parent 39d45e3 commit a904457

File tree

3 files changed

+88
-5
lines changed

3 files changed

+88
-5
lines changed

src/System.CommandLine.Tests/OptionTests.cs

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,23 @@
11
// Copyright (c) .NET Foundation and contributors. All rights reserved.
22
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
33

4+
using System.CommandLine.Invocation;
45
using System.CommandLine.Parsing;
56
using FluentAssertions;
67
using Xunit;
8+
using Xunit.Abstractions;
79

810
namespace System.CommandLine.Tests
911
{
1012
public class OptionTests : SymbolTests
1113
{
14+
private ITestOutputHelper _output;
15+
16+
public OptionTests(ITestOutputHelper output)
17+
{
18+
_output = output;
19+
}
20+
1221
[Fact]
1322
public void When_an_option_has_only_one_alias_then_that_alias_is_its_name()
1423
{
@@ -289,9 +298,17 @@ public void Option_T_Argument_cannot_be_set_to_Argument_of_incorrect_type(Type a
289298
[Fact]
290299
public void Option_T_default_value_can_be_set()
291300
{
292-
var option = new Option<int>("-x", parseArgument: parsed => , isDefault: );
293-
294-
throw new NotImplementedException("test not written");
301+
var option = new Option<int>(
302+
"-x",
303+
parseArgument: parsed => 123,
304+
isDefault: true);
305+
306+
var result = option
307+
.Parse("")
308+
.FindResultFor(option)
309+
.GetValueOrDefault()
310+
.Should()
311+
.Be(123);
295312
}
296313

297314
protected override Symbol CreateSymbol(string name) => new Option(name);

src/System.CommandLine/Argument{T}.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,11 @@ public Argument(TryConvertArgument<T> convert, Func<T> getDefaultValue = default
6767

6868
public Argument(ParseArgument<T> parse, bool isDefault = false) : this()
6969
{
70+
if (parse == null)
71+
{
72+
throw new ArgumentNullException(nameof(parse));
73+
}
74+
7075
if (isDefault)
7176
{
7277
SetDefaultValueFactory(() =>

src/System.CommandLine/Option{T}.cs

Lines changed: 63 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,81 @@
11
// Copyright (c) .NET Foundation and contributors. All rights reserved.
22
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
33

4+
using System.CommandLine.Parsing;
5+
46
namespace System.CommandLine
57
{
68
public class Option<T> : Option
79
{
8-
public Option(string alias, string description = null) : base(alias, description)
10+
public Option(string alias) : base(alias)
11+
{
12+
Argument = new Argument<T>();
13+
}
14+
15+
public Option(string alias, string description) : base(alias, description)
916
{
1017
Argument = new Argument<T>();
1118
}
1219

13-
public Option(string[] aliases, string description = null) : base(aliases, description)
20+
public Option(string[] aliases, string description) : base(aliases, description)
1421
{
1522
Argument = new Argument<T>();
1623
}
1724

25+
public Option(
26+
string alias,
27+
ParseArgument<T> parseArgument,
28+
bool isDefault = false,
29+
string description = null) : base(alias, description)
30+
{
31+
if (parseArgument is null)
32+
{
33+
throw new ArgumentNullException(nameof(parseArgument));
34+
}
35+
36+
Argument = new Argument<T>(parseArgument, isDefault);
37+
}
38+
39+
public Option(
40+
string[] aliases,
41+
ParseArgument<T> parseArgument,
42+
bool isDefault = false,
43+
string description = null) : base(aliases, description)
44+
{
45+
if (parseArgument is null)
46+
{
47+
throw new ArgumentNullException(nameof(parseArgument));
48+
}
49+
50+
Argument = new Argument<T>(parseArgument, isDefault);
51+
}
52+
53+
public Option(
54+
string alias,
55+
Func<T> getDefaultValue,
56+
string description = null) : base(alias, description)
57+
{
58+
if (getDefaultValue is null)
59+
{
60+
throw new ArgumentNullException(nameof(getDefaultValue));
61+
}
62+
63+
Argument = new Argument<T>(getDefaultValue);
64+
}
65+
66+
public Option(
67+
string[] aliases,
68+
Func<T> getDefaultValue,
69+
string description = null) : base(aliases, description)
70+
{
71+
if (getDefaultValue is null)
72+
{
73+
throw new ArgumentNullException(nameof(getDefaultValue));
74+
}
75+
76+
Argument = new Argument<T>(getDefaultValue);
77+
}
78+
1879
public override Argument Argument
1980
{
2081
set

0 commit comments

Comments
 (0)