Skip to content

Commit 6d5263f

Browse files
committed
move ArgumentConversionResult out of SymbolResult
1 parent b1399e4 commit 6d5263f

File tree

5 files changed

+56
-52
lines changed

5 files changed

+56
-52
lines changed

src/System.CommandLine/Parsing/ArgumentResult.cs

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ internal ArgumentResult(
2020

2121
public IArgument Argument { get; }
2222

23-
internal override ArgumentConversionResult ArgumentConversionResult =>
24-
ConversionResult ??= Convert(this, Argument);
23+
internal ArgumentConversionResult ArgumentConversionResult =>
24+
ConversionResult ??= Convert(Argument);
2525

2626
public override string ToString() => $"{GetType().Name} {Argument.Name}: {string.Join(" ", Tokens.Select(t => $"<{t.Value}>"))}";
2727

@@ -40,11 +40,10 @@ internal ParseError CustomError(Argument argument)
4040
return null;
4141
}
4242

43-
internal static ArgumentConversionResult Convert(
44-
ArgumentResult argumentResult,
43+
internal ArgumentConversionResult Convert(
4544
IArgument argument)
4645
{
47-
var parentResult = argumentResult.Parent;
46+
var parentResult = Parent;
4847

4948
if (ShouldCheckArity() &&
5049
ArgumentArity.Validate(parentResult,
@@ -65,12 +64,12 @@ internal static ArgumentConversionResult Convert(
6564
if (argument is Argument a &&
6665
a.ConvertArguments != null)
6766
{
68-
if (argumentResult.ConversionResult != null)
67+
if (ConversionResult != null)
6968
{
70-
return argumentResult.ConversionResult;
69+
return ConversionResult;
7170
}
7271

73-
var success = a.ConvertArguments(argumentResult, out var value);
72+
var success = a.ConvertArguments(this, out var value);
7473

7574
if (value is ArgumentConversionResult conversionResult)
7675
{
@@ -82,7 +81,7 @@ internal static ArgumentConversionResult Convert(
8281
}
8382
else
8483
{
85-
return ArgumentConversionResult.Failure(argument, argumentResult.ErrorMessage ?? $"Invalid: {parentResult.Token} {string.Join(" ", parentResult.Tokens.Select(t => t.Value))}");
84+
return ArgumentConversionResult.Failure(argument, this.ErrorMessage ?? $"Invalid: {parentResult.Token} {string.Join(" ", parentResult.Tokens.Select(t => t.Value))}");
8685
}
8786
}
8887

@@ -104,6 +103,5 @@ bool ShouldCheckArity()
104103
optionResult.IsImplicit);
105104
}
106105
}
107-
108106
}
109107
}

src/System.CommandLine/Parsing/CommandResult.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ namespace System.CommandLine.Parsing
99
{
1010
public class CommandResult : SymbolResult
1111
{
12+
private ArgumentConversionResultSet _results;
13+
1214
internal CommandResult(
1315
ICommand command,
1416
Token token,
@@ -77,5 +79,27 @@ public T ValueForOption<T>(string alias)
7779
return default;
7880
}
7981
}
82+
83+
internal ArgumentConversionResultSet ArgumentConversionResults
84+
{
85+
get
86+
{
87+
if (_results == null)
88+
{
89+
var results = Children
90+
.OfType<ArgumentResult>()
91+
.Select(r => r.Convert(r.Argument));
92+
93+
_results = new ArgumentConversionResultSet();
94+
95+
foreach (var result in results)
96+
{
97+
_results.Add(result);
98+
}
99+
}
100+
101+
return _results;
102+
}
103+
}
80104
}
81105
}

src/System.CommandLine/Parsing/OptionResult.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
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.Binding;
5+
using System.Linq;
6+
47
namespace System.CommandLine.Parsing
58
{
69
public class OptionResult : SymbolResult
710
{
11+
private ArgumentConversionResult _argumentConversionResult;
12+
813
internal OptionResult(
914
IOption option,
1015
Token token,
@@ -34,5 +39,23 @@ private protected override int RemainingArgumentCapacity
3439
return capacity;
3540
}
3641
}
42+
43+
internal ArgumentConversionResult ArgumentConversionResult
44+
{
45+
get
46+
{
47+
if (_argumentConversionResult == null)
48+
{
49+
var results = Children
50+
.OfType<ArgumentResult>()
51+
.Select(r => r.Convert(r.Argument));
52+
53+
_argumentConversionResult = results.SingleOrDefault() ??
54+
ArgumentConversionResult.None(Option.Argument);
55+
}
56+
57+
return _argumentConversionResult;
58+
}
59+
}
3760
}
3861
}

src/System.CommandLine/Parsing/ParseResultExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ argumentResult.Argument is Argument argument &&
124124
builder.Append(" ");
125125
}
126126

127-
switch (symbolResult.ArgumentConversionResult)
127+
switch (argumentResult.ArgumentConversionResult)
128128
{
129129
case SuccessfulArgumentConversionResult successful:
130130

src/System.CommandLine/Parsing/SymbolResult.cs

Lines changed: 0 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,13 @@
22
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
33

44
using System.Collections.Generic;
5-
using System.CommandLine.Binding;
65
using System.Linq;
76

87
namespace System.CommandLine.Parsing
98
{
109
public abstract class SymbolResult
1110
{
1211
private protected readonly List<Token> _tokens = new List<Token>();
13-
private ArgumentConversionResultSet _results;
1412
private ValidationMessages _validationMessages;
1513
private readonly Dictionary<IArgument, object> _defaultArgumentValues = new Dictionary<IArgument, object>();
1614

@@ -26,45 +24,6 @@ private protected SymbolResult(
2624
Parent = parent;
2725
}
2826

29-
internal virtual ArgumentConversionResult ArgumentConversionResult
30-
{
31-
get
32-
{
33-
var argument = Symbol switch {
34-
IOption o => o.Argument,
35-
IArgument a => a,
36-
_ => null
37-
};
38-
39-
return ArgumentConversionResults.SingleOrDefault() ??
40-
ArgumentConversionResult.None(argument);
41-
}
42-
}
43-
44-
internal ArgumentConversionResultSet ArgumentConversionResults
45-
{
46-
get
47-
{
48-
if (_results == null)
49-
{
50-
var results = Children
51-
.OfType<ArgumentResult>()
52-
.Select(r => ArgumentResult.Convert(r, r.Argument));
53-
54-
_results = new ArgumentConversionResultSet();
55-
56-
foreach (var result in results)
57-
{
58-
_results.Add(result);
59-
}
60-
61-
return _results;
62-
}
63-
64-
return _results;
65-
}
66-
}
67-
6827
public string ErrorMessage { get; set; }
6928

7029
public SymbolResultSet Children { get; } = new SymbolResultSet();

0 commit comments

Comments
 (0)