File tree Expand file tree Collapse file tree 2 files changed +38
-1
lines changed
System.CommandLine/Parsing Expand file tree Collapse file tree 2 files changed +38
-1
lines changed Original file line number Diff line number Diff line change @@ -531,5 +531,27 @@ public void Default_value_is_used_when_option_with_ZeroOrOne_arity_is_parsed_wit
531531
532532 parseResult . GetValue ( option ) . Should ( ) . Be ( 42 ) ;
533533 }
534+
535+ [ Fact ] // https://github.com/dotnet/command-line-api/issues/2621
536+ public void Option_with_default_value_has_Action_added_to_PreActions_when_not_specified_but_has_default ( )
537+ {
538+ var preactionWasInvoked = false ;
539+ var option = new Option < bool > ( "--quiet" )
540+ {
541+ DefaultValueFactory = _ => true ,
542+ Action = new SynchronousTestAction ( _ => preactionWasInvoked = true , terminating : false )
543+ } ;
544+
545+ var rootCommand = new RootCommand
546+ {
547+ option
548+ } ;
549+ rootCommand . SetAction ( _ => { } ) ;
550+
551+ var parseResult = rootCommand . Parse ( "" ) ;
552+ parseResult . Invoke ( ) ;
553+
554+ preactionWasInvoked . Should ( ) . BeTrue ( ) ;
555+ }
534556 }
535557}
Original file line number Diff line number Diff line change @@ -333,7 +333,7 @@ void ParseDirective()
333333 }
334334
335335 private void AddPreAction ( CommandLineAction action )
336- {
336+ {
337337 if ( _preActions is null )
338338 {
339339 _preActions = new ( ) ;
@@ -342,6 +342,18 @@ private void AddPreAction(CommandLineAction action)
342342 _preActions . Add ( action ) ;
343343 }
344344
345+ private void AddPreActionsForImplicitOptions ( )
346+ {
347+ foreach ( var kvp in _symbolResultTree )
348+ {
349+ if ( kvp is { Key : Option { Action : { Terminating : false } action } , Value : OptionResult { Implicit : true } } &&
350+ _primaryAction != action )
351+ {
352+ AddPreAction ( action ) ;
353+ }
354+ }
355+ }
356+
345357 private void AddCurrentTokenToUnmatched ( )
346358 {
347359 if ( CurrentToken . Type == TokenType . DoubleDash )
@@ -435,6 +447,9 @@ private void ValidateAndAddDefaultResults()
435447 }
436448 }
437449 }
450+
451+ // FIX: (ValidateAndAddDefaultResults) AddPreActionsForImplicitOptions();
452+
438453 }
439454 }
440455}
You can’t perform that action at this time.
0 commit comments