Skip to content

Commit dbb29c3

Browse files
committed
simplify ParseArgument<T> to return the value directly
1 parent e2f1c95 commit dbb29c3

File tree

4 files changed

+44
-59
lines changed

4 files changed

+44
-59
lines changed

src/System.CommandLine.Tests/ArgumentTests.cs

Lines changed: 13 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public class CustomParsing
5959
[Fact]
6060
public void HasDefaultValue_can_be_set_to_true()
6161
{
62-
var argument = new Argument<FileSystemInfo>(result => true, true);
62+
var argument = new Argument<FileSystemInfo>(result => null, true);
6363

6464
argument.HasDefaultValue
6565
.Should()
@@ -69,7 +69,7 @@ public void HasDefaultValue_can_be_set_to_true()
6969
[Fact]
7070
public void HasDefaultValue_can_be_set_to_false()
7171
{
72-
var argument = new Argument<FileSystemInfo>(result => true, false);
72+
var argument = new Argument<FileSystemInfo>(result => null, false);
7373

7474
argument.HasDefaultValue
7575
.Should()
@@ -79,11 +79,7 @@ public void HasDefaultValue_can_be_set_to_false()
7979
[Fact]
8080
public void GetDefaultValue_returns_specified_value()
8181
{
82-
var argument = new Argument<string>(result =>
83-
{
84-
result.Value = "the-default";
85-
return true;
86-
}, isDefault: true);
82+
var argument = new Argument<string>(result => "the-default", isDefault: true);
8783

8884
argument.GetDefaultValue()
8985
.Should()
@@ -93,7 +89,7 @@ public void GetDefaultValue_returns_specified_value()
9389
[Fact]
9490
public void GetDefaultValue_returns_null_when_parse_delegate_returns_true_without_setting_a_value()
9591
{
96-
var argument = new Argument<string>(result => { return true; }, isDefault: true);
92+
var argument = new Argument<string>(result => null, isDefault: true);
9793

9894
argument.GetDefaultValue()
9995
.Should()
@@ -103,7 +99,7 @@ public void GetDefaultValue_returns_null_when_parse_delegate_returns_true_withou
10399
[Fact]
104100
public void GetDefaultValue_returns_null_when_parse_delegate_returns_true_and_sets_value_to_null()
105101
{
106-
var argument = new Argument<string>(result => { return true; }, isDefault: true);
102+
var argument = new Argument<string>(result => null, isDefault: true);
107103

108104
argument.GetDefaultValue()
109105
.Should()
@@ -113,7 +109,7 @@ public void GetDefaultValue_returns_null_when_parse_delegate_returns_true_and_se
113109
[Fact]
114110
public void GetDefaultValue_can_return_null()
115111
{
116-
var argument = new Argument<string>(result => { return true; }, isDefault: true);
112+
var argument = new Argument<string>(result => null, isDefault: true);
117113

118114
argument.GetDefaultValue()
119115
.Should()
@@ -126,7 +122,7 @@ public void validation_failure_message()
126122
var argument = new Argument<FileSystemInfo>(result =>
127123
{
128124
result.ErrorMessage = "oops!";
129-
return true;
125+
return null;
130126
});
131127

132128
argument.Parse("x")
@@ -142,12 +138,7 @@ public void validation_failure_message()
142138
[Fact]
143139
public void custom_parsing_of_scalar_value_from_an_argument_with_one_token()
144140
{
145-
var argument = new Argument<int>(result =>
146-
{
147-
result.Value = int.Parse(result.Tokens.Single().Value);
148-
149-
return true;
150-
});
141+
var argument = new Argument<int>(result => int.Parse(result.Tokens.Single().Value));
151142

152143
argument.Parse("123")
153144
.FindResultFor(argument)
@@ -159,12 +150,7 @@ public void custom_parsing_of_scalar_value_from_an_argument_with_one_token()
159150
[Fact]
160151
public void custom_parsing_of_sequence_value_from_an_argument_with_one_token()
161152
{
162-
var argument = new Argument<IEnumerable<int>>(result =>
163-
{
164-
result.Value = result.Tokens.Single().Value.Split(',').Select(int.Parse);
165-
166-
return true;
167-
});
153+
var argument = new Argument<IEnumerable<int>>(result => result.Tokens.Single().Value.Split(',').Select(int.Parse));
168154

169155
argument.Parse("1,2,3")
170156
.FindResultFor(argument)
@@ -178,8 +164,7 @@ public void custom_parsing_of_sequence_value_from_an_argument_with_multiple_toke
178164
{
179165
var argument = new Argument<IEnumerable<int>>(result =>
180166
{
181-
result.Value = result.Tokens.Select(t => int.Parse(t.Value)).ToArray();
182-
return true;
167+
return result.Tokens.Select(t => int.Parse(t.Value)).ToArray();
183168
});
184169

185170
argument.Parse("1 2 3")
@@ -192,13 +177,10 @@ public void custom_parsing_of_sequence_value_from_an_argument_with_multiple_toke
192177
[Fact]
193178
public void custom_parsing_of_scalar_value_from_an_argument_with_multiple_tokens()
194179
{
195-
var argument = new Argument<int>(result =>
180+
var argument = new Argument<int>(result => result.Tokens.Select(t => int.Parse(t.Value)).Sum())
196181
{
197-
result.Value = result.Tokens.Select(t => int.Parse(t.Value)).Sum();
198-
return true;
199-
});
200-
201-
argument.Arity = ArgumentArity.ZeroOrMore;
182+
Arity = ArgumentArity.ZeroOrMore
183+
};
202184

203185
argument.Parse("1 2 3")
204186
.FindResultFor(argument)

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

Lines changed: 12 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -75,15 +75,9 @@ public Argument(ParseArgument<T> parse, bool isDefault = false) : this()
7575
this,
7676
null);
7777

78-
if (parse(argumentResult))
79-
{
80-
argumentResult.ValueWasSpecified = true;
81-
return argumentResult.Value;
82-
}
83-
else
84-
{
85-
return null;
86-
}
78+
var result = parse(argumentResult);
79+
argumentResult.ValueWasSpecified = true;
80+
return result;
8781
});
8882
}
8983

@@ -98,25 +92,19 @@ public Argument(ParseArgument<T> parse, bool isDefault = false) : this()
9892
newResult.AddToken(token);
9993
}
10094

101-
if (parse(newResult))
95+
var result = parse(newResult);
96+
97+
if (string.IsNullOrEmpty(newResult.ErrorMessage))
10298
{
103-
if (string.IsNullOrEmpty(newResult.ErrorMessage))
104-
{
105-
newResult.ValueWasSpecified = true;
106-
value = newResult.Value;
107-
originalResult.Parent.Children.Remove(originalResult);
108-
originalResult.Parent.Children.Add(newResult);
109-
return true;
110-
}
111-
else
112-
{
113-
originalResult.ErrorMessage = newResult.ErrorMessage;
114-
value = default(T);
115-
return false;
116-
}
99+
newResult.ValueWasSpecified = true;
100+
value = result;
101+
originalResult.Parent.Children.Remove(originalResult);
102+
originalResult.Parent.Children.Add(newResult);
103+
return true;
117104
}
118105
else
119106
{
107+
originalResult.ErrorMessage = newResult.ErrorMessage;
120108
value = default(T);
121109
return false;
122110
}

src/System.CommandLine/Binding/TryConvertArgument.cs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,19 @@
55

66
namespace System.CommandLine.Binding
77
{
8-
public delegate bool TryConvertArgument(
9-
ArgumentResult symbolResult,
8+
internal delegate bool TryConvertArgument(
9+
ArgumentResult argumentResult,
1010
out object value);
1111

12+
/// <summary>
13+
/// Converts an <see cref="ArgumentResult"/> into an instance of <typeparamref name="T"/>.
14+
/// </summary>
15+
/// <typeparam name="T">The type to convert to.</typeparam>
16+
/// <param name="argumentResult">The <see cref="ArgumentResult"/> representing parsed input to be converted.</param>
17+
/// <param name="value">The converted result.</param>
18+
/// <returns>True if the conversion is successful; otherwise, false.</returns>
19+
/// <remarks>Validation errors can be returned by setting <see cref="SymbolResult.ErrorMessage"/>.</remarks>
1220
public delegate bool TryConvertArgument<T>(
13-
ArgumentResult symbolResult,
21+
ArgumentResult argumentResult,
1422
out T value);
1523
}

src/System.CommandLine/Parsing/ParseArgument{T}.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,12 @@
33

44
namespace System.CommandLine.Parsing
55
{
6-
public delegate bool ParseArgument<T>(ArgumentResult<T> result);
6+
/// <summary>
7+
/// Performs custom parsing of an argument.
8+
/// </summary>
9+
/// <typeparam name="T">The type which the argument is to be parsed as.</typeparam>
10+
/// <param name="result">The argument result.</param>
11+
/// <returns>The parsed value.</returns>
12+
/// <remarks>Validation errors can be returned by setting <see cref="SymbolResult.ErrorMessage"/>.</remarks>
13+
public delegate T ParseArgument<T>(ArgumentResult<T> result);
714
}

0 commit comments

Comments
 (0)