Skip to content

Commit eaec1c5

Browse files
committed
move SymbolResult.Token to OptionResult and CommandResult
1 parent 6d5263f commit eaec1c5

File tree

11 files changed

+55
-43
lines changed

11 files changed

+55
-43
lines changed

src/System.CommandLine.Tests/Binding/TypeConversionTests.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -261,14 +261,14 @@ public void When_argument_cannot_be_parsed_as_the_specified_type_then_getting_va
261261
{
262262
new Option(new[] { "-o", "--one" })
263263
{
264-
Argument = new Argument<int>((ArgumentResult symbol, out int value) =>
264+
Argument = new Argument<int>((ArgumentResult argumentResult, out int value) =>
265265
{
266-
if (int.TryParse(symbol.Tokens.Select(t => t.Value).Single(), out value))
266+
if (int.TryParse(argumentResult.Tokens.Select(t => t.Value).Single(), out value))
267267
{
268268
return true;
269269
}
270270

271-
symbol.ErrorMessage = $"'{symbol.Token.Value}' is not an integer";
271+
argumentResult.ErrorMessage = $"'{argumentResult.Tokens.Single().Value}' is not an integer";
272272

273273
return false;
274274
}),
@@ -984,10 +984,10 @@ public async Task Custom_argument_converter_is_only_called_once()
984984
callCount.Should().Be(1);
985985
handlerWasCalled.Should().BeTrue();
986986

987-
bool TryConvertInt(SymbolResult result, out int value)
987+
bool TryConvertInt(ArgumentResult result, out int value)
988988
{
989989
callCount++;
990-
return int.TryParse(result.Token.Value, out value);
990+
return int.TryParse(result.Tokens.Single().Value, out value);
991991
}
992992

993993
void Run(int value) => handlerWasCalled = true;

src/System.CommandLine/ArgumentArity.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@ internal static FailedArgumentConversionArityResult Validate(
5959
throw new ArgumentException("");
6060
}
6161

62-
6362
var tokenCount = argumentResult?.Tokens.Count ?? 0;
6463

6564
if (tokenCount < minimumNumberOfValues)
@@ -87,7 +86,7 @@ internal static FailedArgumentConversionArityResult Validate(
8786
return new TooManyArgumentsConversionResult(
8887
argument,
8988
symbolResult.ValidationMessages.ExpectsFewerArguments(
90-
symbolResult.Token,
89+
symbolResult.Tokens.Last(),
9190
tokenCount,
9291
maximumNumberOfValues));
9392
}

src/System.CommandLine/Parsing/ArgumentResult.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@ public class ArgumentResult : SymbolResult
1010
{
1111
internal ArgumentResult(
1212
IArgument argument,
13-
Token token,
14-
SymbolResult parent) : base(argument, token, parent)
13+
SymbolResult parent) : base(argument, parent)
1514
{
1615
Argument = argument;
1716
}
@@ -81,7 +80,7 @@ internal ArgumentConversionResult Convert(
8180
}
8281
else
8382
{
84-
return ArgumentConversionResult.Failure(argument, this.ErrorMessage ?? $"Invalid: {parentResult.Token} {string.Join(" ", parentResult.Tokens.Select(t => t.Value))}");
83+
return ArgumentConversionResult.Failure(argument, ErrorMessage ?? $"Invalid: {parentResult.Token()} {string.Join(" ", parentResult.Tokens.Select(t => t.Value))}");
8584
}
8685
}
8786

src/System.CommandLine/Parsing/CommandResult.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,11 @@ internal CommandResult(
1616
Token token,
1717
CommandResult parent = null) :
1818
base(command ?? throw new ArgumentNullException(nameof(command)),
19-
token ?? throw new ArgumentNullException(nameof(token)),
2019
parent)
2120
{
2221
Command = command;
22+
23+
Token = token ?? throw new ArgumentNullException(nameof(token));
2324
}
2425

2526
public ICommand Command { get; }
@@ -31,6 +32,9 @@ public OptionResult OptionResult(string alias)
3132
return Children[alias] as OptionResult;
3233
}
3334

35+
public Token Token { get; }
36+
37+
3438
internal virtual RootCommandResult Root => (Parent as CommandResult)?.Root;
3539

3640
internal bool TryGetValueForArgument(

src/System.CommandLine/Parsing/CommandResultExtensions.cs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,19 +26,21 @@ public static T GetArgumentValueOrDefault<T>(
2626
}
2727

2828
internal static bool TryGetValueForOption(
29-
this CommandResult commandResult,
30-
IValueDescriptor valueDescriptor,
29+
this CommandResult commandResult,
30+
IValueDescriptor valueDescriptor,
3131
out object value)
3232
{
33-
var children = commandResult.Children
34-
.Where(o => valueDescriptor.ValueName.IsMatch(o.Symbol))
35-
.ToArray();
33+
var children = commandResult
34+
.Children
35+
.Where(o => valueDescriptor.ValueName.IsMatch(o.Symbol))
36+
.ToArray();
3637

3738
SymbolResult symbolResult = null;
3839

3940
if (children.Length > 1)
4041
{
41-
throw new ArgumentException($"Ambiguous match while trying to bind parameter {valueDescriptor.ValueName} among: {String.Join(",", children.Select(o => o.Symbol.Name))}");
42+
throw new ArgumentException(
43+
$"Ambiguous match while trying to bind parameter {valueDescriptor.ValueName} among: {string.Join(",", children.Select(o => o.Symbol.Name))}");
4244
}
4345

4446
if (children.Length == 1)

src/System.CommandLine/Parsing/OptionResult.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,18 @@ internal OptionResult(
1515
Token token,
1616
CommandResult parent = null) :
1717
base(option ?? throw new ArgumentNullException(nameof(option)),
18-
token ?? throw new ArgumentNullException(nameof(token)),
1918
parent)
2019
{
2120
Option = option;
21+
Token = token ?? throw new ArgumentNullException(nameof(token));
2222
}
2323

2424
public IOption Option { get; }
2525

2626
public bool IsImplicit => Token is ImplicitToken;
2727

28+
public Token Token { get; }
29+
2830
private protected override int RemainingArgumentCapacity
2931
{
3032
get

src/System.CommandLine/Parsing/ParseResultExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ argumentResult.Argument is Argument argument &&
165165
else
166166
{
167167
builder.Append("[ ");
168-
builder.Append(symbolResult.Token.Value);
168+
builder.Append(symbolResult.Token().Value);
169169

170170
foreach (var child in symbolResult.Children)
171171
{

src/System.CommandLine/Parsing/ParseResultVisitor.cs

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -89,9 +89,8 @@ protected override void VisitCommandArgumentNode(
8989
argumentResult =
9090
new ArgumentResult(
9191
argumentNode.Argument,
92-
argumentNode.Token,
9392
commandResult);
94-
93+
9594
commandResult.Children.Add(argumentResult);
9695
}
9796

@@ -131,7 +130,6 @@ protected override void VisitOptionArgumentNode(
131130
argumentResult =
132131
new ArgumentResult(
133132
argumentNode.Argument,
134-
argumentNode.Token,
135133
optionResult);
136134
optionResult.Children.Add(argumentResult);
137135
}
@@ -158,7 +156,7 @@ protected override void Stop(SyntaxNode node)
158156

159157
ValidateCommandResult();
160158

161-
foreach (var result in _innermostCommandResult.Children)
159+
foreach (var result in _innermostCommandResult.Children.ToArray())
162160
{
163161
switch (result)
164162
{
@@ -331,7 +329,6 @@ private void PopulateDefaultValues()
331329
optionResult.Children.Add(
332330
new ArgumentResult(
333331
option.Argument,
334-
token,
335332
optionResult));
336333

337334
commandResult.Children.Add(optionResult);
@@ -346,11 +343,11 @@ private void PopulateDefaultValues()
346343

347344
var argumentResult = new ArgumentResult(
348345
argument,
349-
implicitToken,
350346
commandResult);
351347

352348
commandResult.Children.Add(argumentResult);
353349
commandResult.AddToken(implicitToken);
350+
argumentResult.AddToken(implicitToken);
354351
_rootCommandResult.AddToSymbolMap(argumentResult);
355352

356353
break;
@@ -364,7 +361,6 @@ private void PopulateDefaultValues()
364361
o.Children.Add(
365362
new ArgumentResult(
366363
o.Option.Argument,
367-
new ImplicitToken(true, TokenType.Argument),
368364
o));
369365
}
370366
}
@@ -381,7 +377,6 @@ private void PopulateDefaultValues()
381377
_unparsedTokens,
382378
_unmatchedTokens,
383379
_errors,
384-
_rawInput
385-
);
380+
_rawInput);
386381
}
387382
}

src/System.CommandLine/Parsing/SymbolResult.cs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,10 @@ public abstract class SymbolResult
1414

1515
private protected SymbolResult(
1616
ISymbol symbol,
17-
Token token,
1817
SymbolResult parent = null)
1918
{
2019
Symbol = symbol ?? throw new ArgumentNullException(nameof(symbol));
2120

22-
Token = token ?? throw new ArgumentNullException(nameof(token));
23-
2421
Parent = parent;
2522
}
2623

@@ -32,8 +29,6 @@ private protected SymbolResult(
3229

3330
public ISymbol Symbol { get; }
3431

35-
public Token Token { get; }
36-
3732
public IReadOnlyList<Token> Tokens => _tokens;
3833

3934
internal bool IsArgumentLimitReached => RemainingArgumentCapacity <= 0;
@@ -84,15 +79,16 @@ internal bool UseDefaultValueFor(IArgument argument)
8479
}
8580

8681
if (this is CommandResult &&
87-
Children.ResultFor(argument)?.Token is ImplicitToken)
82+
Children.ResultFor(argument)?.Tokens is {} tokens &&
83+
tokens.All(t => t is ImplicitToken))
8884
{
8985
return true;
9086
}
9187

9288
return _defaultArgumentValues.ContainsKey(argument);
9389
}
9490

95-
public override string ToString() => $"{GetType().Name}: {Token}";
91+
public override string ToString() => $"{GetType().Name}: {this.Token()}";
9692

9793
internal ParseError UnrecognizedArgumentError(Argument argument)
9894
{

src/System.CommandLine/Parsing/SymbolResultExtensions.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,18 @@ internal static IEnumerable<SymbolResult> AllSymbolResults(this SymbolResult sym
2323
yield return item;
2424
}
2525
}
26+
27+
internal static Token Token(this SymbolResult symbolResult)
28+
{
29+
switch (symbolResult)
30+
{
31+
case CommandResult commandResult:
32+
return commandResult.Token;
33+
case OptionResult optionResult:
34+
return optionResult.Token;
35+
default:
36+
throw new ArgumentOutOfRangeException(nameof(symbolResult));
37+
}
38+
}
2639
}
2740
}

0 commit comments

Comments
 (0)