2
2
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3
3
4
4
using System . Collections . Generic ;
5
- using System . CommandLine . Help ;
6
5
using System . CommandLine . Invocation ;
6
+ using System . Linq ;
7
7
8
8
namespace System . CommandLine . Parsing
9
9
{
@@ -17,7 +17,6 @@ internal sealed class ParseOperation
17
17
18
18
private int _index ;
19
19
private CommandResult _innermostCommandResult ;
20
- private bool _isHelpRequested ;
21
20
private bool _isTerminatingDirectiveSpecified ;
22
21
private CommandLineAction ? _primaryAction ;
23
22
private List < CommandLineAction > ? _preActions ;
@@ -63,21 +62,7 @@ internal ParseResult Parse()
63
62
64
63
ValidateAndAddDefaultResults ( ) ;
65
64
66
-
67
- if ( _isHelpRequested )
68
- {
69
- _symbolResultTree . Errors ? . Clear ( ) ;
70
- }
71
-
72
- if ( _primaryAction is null )
73
- {
74
- if ( _symbolResultTree . ErrorCount > 0 )
75
- {
76
- _primaryAction = new ParseErrorAction ( ) ;
77
- }
78
- }
79
-
80
- return new (
65
+ return new (
81
66
_configuration ,
82
67
_rootCommandResult ,
83
68
_innermostCommandResult ,
@@ -197,11 +182,6 @@ private void ParseOption()
197
182
// directives have a precedence over --help and --version
198
183
if ( ! _isTerminatingDirectiveSpecified )
199
184
{
200
- if ( option is HelpOption )
201
- {
202
- _isHelpRequested = true ;
203
- }
204
-
205
185
if ( option . Action . Terminating )
206
186
{
207
187
_primaryAction = option . Action ;
@@ -386,11 +366,74 @@ private void ValidateAndAddDefaultResults()
386
366
currentResult = currentResult . Parent as CommandResult ;
387
367
}
388
368
389
- if ( _primaryAction is null &&
390
- _innermostCommandResult is { Command : { Action : null , HasSubcommands : true } } )
369
+ if ( _primaryAction is null )
391
370
{
392
- _symbolResultTree . InsertFirstError (
393
- new ParseError ( LocalizationResources . RequiredCommandWasNotProvided ( ) , _innermostCommandResult ) ) ;
371
+ if ( _innermostCommandResult is { Command : { Action : null , HasSubcommands : true } } )
372
+ {
373
+ _symbolResultTree . InsertFirstError (
374
+ new ParseError ( LocalizationResources . RequiredCommandWasNotProvided ( ) , _innermostCommandResult ) ) ;
375
+ }
376
+
377
+ if ( _innermostCommandResult is { Command . Action . ClearsParseErrors : true } &&
378
+ _symbolResultTree . Errors is not null )
379
+ {
380
+ var errorsNotUnderInnermostCommand = _symbolResultTree
381
+ . Errors
382
+ . Where ( e => e . SymbolResult != _innermostCommandResult )
383
+ . ToList ( ) ;
384
+
385
+ _symbolResultTree . Errors = errorsNotUnderInnermostCommand ;
386
+ }
387
+ else if ( _symbolResultTree . ErrorCount > 0 )
388
+ {
389
+ _primaryAction = new ParseErrorAction ( ) ;
390
+ }
391
+ }
392
+ else
393
+ {
394
+ if ( _symbolResultTree . ErrorCount > 0 &&
395
+ _primaryAction . ClearsParseErrors &&
396
+ _symbolResultTree . Errors is not null )
397
+ {
398
+ foreach ( var kvp in _symbolResultTree )
399
+ {
400
+ var symbol = kvp . Key ;
401
+ if ( symbol is Option { Action : { } optionAction } option )
402
+ {
403
+ if ( _primaryAction == optionAction )
404
+ {
405
+ var errorsForPrimarySymbol = _symbolResultTree
406
+ . Errors
407
+ . Where ( e => e . SymbolResult is OptionResult r && r . Option == option )
408
+ . ToList ( ) ;
409
+
410
+ _symbolResultTree . Errors = errorsForPrimarySymbol ;
411
+
412
+ return ;
413
+ }
414
+ }
415
+
416
+ if ( symbol is Command { Action : { } commandAction } command )
417
+ {
418
+ if ( _primaryAction == commandAction )
419
+ {
420
+ var errorsForPrimarySymbol = _symbolResultTree
421
+ . Errors
422
+ . Where ( e => e . SymbolResult is CommandResult r && r . Command == command )
423
+ . ToList ( ) ;
424
+
425
+ _symbolResultTree . Errors = errorsForPrimarySymbol ;
426
+
427
+ return ;
428
+ }
429
+ }
430
+ }
431
+
432
+ if ( _symbolResultTree . ErrorCount > 0 )
433
+ {
434
+ _symbolResultTree . Errors ? . Clear ( ) ;
435
+ }
436
+ }
394
437
}
395
438
}
396
439
}
0 commit comments