-
Notifications
You must be signed in to change notification settings - Fork 390
/
Copy pathCommandResult.cs
214 lines (184 loc) · 7.74 KB
/
CommandResult.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System.Collections.Generic;
using System.Linq;
namespace System.CommandLine.Parsing
{
/// <summary>
/// A result produced when parsing a <see cref="Command" />.
/// </summary>
public sealed class CommandResult : SymbolResult
{
internal CommandResult(
CliCommand command,
CliToken token,
SymbolResultTree symbolResultTree,
CommandResult? parent = null) :
base(symbolResultTree, parent)
{
Command = command ?? throw new ArgumentNullException(nameof(command));
IdentifierToken = token ?? throw new ArgumentNullException(nameof(token));
}
/// <summary>
/// The command to which the result applies.
/// </summary>
public CliCommand Command { get; }
// FIXME: should CliToken be public or internal?
/// <summary>
/// The token that was parsed to specify the command.
/// </summary>
internal CliToken IdentifierToken { get; }
/// <summary>
/// Child symbol results in the parse tree.
/// </summary>
public IEnumerable<SymbolResult> Children => SymbolResultTree.GetChildren(this);
public IReadOnlyList<ValueResult> ValueResults => Children.Select(GetValueResult).OfType<ValueResult>().ToList();
private ValueResult? GetValueResult(SymbolResult symbolResult)
=> symbolResult switch
{
ArgumentResult argumentResult => argumentResult.ValueResult,
OptionResult optionResult => optionResult.ValueResult,
_ => null!
};
/// <inheritdoc/>
public override string ToString() => $"{nameof(CommandResult)}: {IdentifierToken.Value} {string.Join(" ", Tokens.Select(t => t.Value))}";
internal override bool UseDefaultValueFor(ArgumentResult argumentResult)
=> argumentResult.Argument.HasDefaultValue && argumentResult.Tokens.Count == 0;
/// <param name="completeValidation">Only the inner most command goes through complete validation.</param>
internal void Validate(bool completeValidation)
{
if (completeValidation)
{
// TODO: invocation
// if (Command.Action is null && Command.HasSubcommands)
if (Command.HasSubcommands)
{
SymbolResultTree.InsertFirstError(
new CliDiagnostic(new("validateSubCommandError", "Validation Error", LocalizationResources.RequiredCommandWasNotProvided(), CliDiagnosticSeverity.Warning, null), [], symbolResult: this));
}
// TODO: validators
/*
if (Command.HasValidators)
{
int errorCountBefore = SymbolResultTree.ErrorCount;
for (var i = 0; i < Command.Validators.Count; i++)
{
Command.Validators[i](this);
}
if (SymbolResultTree.ErrorCount != errorCountBefore)
{
return;
}
}
*/
}
// TODO: Validation
if (Command.HasOptions)
{
ValidateOptions(completeValidation);
}
if (Command.HasArguments)
{
ValidateArguments(completeValidation);
}
}
private void ValidateOptions(bool completeValidation)
{
var options = Command.Options;
for (var i = 0; i < options.Count; i++)
{
var option = options[i];
// TODO: VersionOption, recursive options
// if (!completeValidation && !(option.Recursive || option.Argument.HasDefaultValue || option is VersionOption))
if (!completeValidation && !option.Argument.HasDefaultValue)
{
continue;
}
OptionResult optionResult;
ArgumentResult argumentResult;
if (!SymbolResultTree.TryGetValue(option, out SymbolResult? symbolResult))
{
if (option.Required || option.Argument.HasDefaultValue)
{
optionResult = new(option, SymbolResultTree, null, this);
SymbolResultTree.Add(optionResult.Option, optionResult);
argumentResult = new(optionResult.Option.Argument, SymbolResultTree, optionResult);
SymbolResultTree.Add(optionResult.Option.Argument, argumentResult);
if (option.Required && !option.Argument.HasDefaultValue)
{
argumentResult.AddError(LocalizationResources.RequiredOptionWasNotProvided(option.Name));
continue;
}
}
else
{
continue;
}
}
else
{
optionResult = (OptionResult)symbolResult;
argumentResult = (ArgumentResult)SymbolResultTree[option.Argument];
}
// When_there_is_an_arity_error_then_further_errors_are_not_reported
if (!ArgumentArity.Validate(argumentResult, out var error))
{
optionResult.AddError(error.ErrorMessage!);
continue;
}
// TODO: validators
/*
if (optionResult.Option.HasValidators)
{
int errorsBefore = SymbolResultTree.ErrorCount;
for (var j = 0; j < optionResult.Option.Validators.Count; j++)
{
optionResult.Option.Validators[j](optionResult);
}
if (errorsBefore != SymbolResultTree.ErrorCount)
{
continue;
}
}
*/
// TODO: Ensure all argument conversions are run for entered values
/*
_ = argumentResult.GetArgumentConversionResult();
*/
}
}
// TODO: Validation
private void ValidateArguments(bool completeValidation)
{
var arguments = Command.Arguments;
for (var i = 0; i < arguments.Count; i++)
{
CliArgument argument = arguments[i];
if (!completeValidation && !argument.HasDefaultValue)
{
continue;
}
ArgumentResult? argumentResult;
if (SymbolResultTree.TryGetValue(argument, out SymbolResult? symbolResult))
{
argumentResult = (ArgumentResult)symbolResult;
}
else if (argument.HasDefaultValue || argument.Arity.MinimumNumberOfValues > 0)
{
argumentResult = new ArgumentResult(argument, SymbolResultTree, this);
SymbolResultTree[argument] = argumentResult;
if (!argument.HasDefaultValue && argument.Arity.MinimumNumberOfValues > 0)
{
argumentResult.AddError(LocalizationResources.RequiredArgumentMissing(argumentResult));
continue;
}
}
else
{
continue;
}
_ = argumentResult.GetArgumentConversionResult();
}
}
}
}