22// Licensed under the MIT license. See LICENSE file in the project root for full license information.
33
44using System . Collections . Generic ;
5- using System . CommandLine . Help ;
65using System . CommandLine . Invocation ;
6+ using System . Linq ;
77
88namespace System . CommandLine . Parsing
99{
@@ -17,7 +17,6 @@ internal sealed class ParseOperation
1717
1818 private int _index ;
1919 private CommandResult _innermostCommandResult ;
20- private bool _isHelpRequested ;
2120 private bool _isTerminatingDirectiveSpecified ;
2221 private CommandLineAction ? _primaryAction ;
2322 private List < CommandLineAction > ? _preActions ;
@@ -63,21 +62,7 @@ internal ParseResult Parse()
6362
6463 ValidateAndAddDefaultResults ( ) ;
6564
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 (
8166 _configuration ,
8267 _rootCommandResult ,
8368 _innermostCommandResult ,
@@ -197,11 +182,6 @@ private void ParseOption()
197182 // directives have a precedence over --help and --version
198183 if ( ! _isTerminatingDirectiveSpecified )
199184 {
200- if ( option is HelpOption )
201- {
202- _isHelpRequested = true ;
203- }
204-
205185 if ( option . Action . Terminating )
206186 {
207187 _primaryAction = option . Action ;
@@ -386,11 +366,74 @@ private void ValidateAndAddDefaultResults()
386366 currentResult = currentResult . Parent as CommandResult ;
387367 }
388368
389- if ( _primaryAction is null &&
390- _innermostCommandResult is { Command : { Action : null , HasSubcommands : true } } )
369+ if ( _primaryAction is null )
391370 {
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+ }
394437 }
395438 }
396439 }
0 commit comments