@@ -265,35 +265,112 @@ public void Nonterminating_option_action_does_not_short_circuit_command_action()
265265 commandActionWasCalled . Should ( ) . BeTrue ( ) ;
266266 }
267267
268- [ Fact ]
269- public void When_multiple_options_with_actions_are_present_then_only_the_last_one_is_invoked ( )
268+ [ Theory ]
269+ [ InlineData ( true ) ]
270+ [ InlineData ( false ) ]
271+ public async Task When_multiple_options_with_terminating_actions_are_present_then_only_the_last_one_is_invoked ( bool invokeAsync )
270272 {
271273 bool optionAction1WasCalled = false ;
272274 bool optionAction2WasCalled = false ;
273275 bool optionAction3WasCalled = false ;
274276
275- SynchronousTestAction optionAction1 = new ( _ => optionAction1WasCalled = true ) ;
276- SynchronousTestAction optionAction2 = new ( _ => optionAction2WasCalled = true ) ;
277- SynchronousTestAction optionAction3 = new ( _ => optionAction3WasCalled = true ) ;
277+ SynchronousTestAction optionAction1 = new ( _ =>
278+ {
279+ optionAction1WasCalled = true ;
280+ } , terminating : true ) ;
281+ SynchronousTestAction optionAction2 = new ( _ =>
282+ {
283+ optionAction2WasCalled = true ;
284+ } , terminating : true ) ;
285+ SynchronousTestAction optionAction3 = new ( _ =>
286+ {
287+ optionAction3WasCalled = true ;
288+ } , terminating : true ) ;
278289
279- Command command = new Command ( "cmd" )
290+ var command = new RootCommand
280291 {
281- new Option < bool > ( "--1" ) { Action = optionAction1 } ,
282- new Option < bool > ( "--2" ) { Action = optionAction2 } ,
283- new Option < bool > ( "--3" ) { Action = optionAction3 }
292+ Action = new AsynchronousTestAction ( _ => { } ) ,
293+ Options =
294+ {
295+ new Option < bool > ( "--1" ) { Action = optionAction1 } ,
296+ new Option < bool > ( "--2" ) { Action = optionAction2 } ,
297+ new Option < bool > ( "--3" ) { Action = optionAction3 } ,
298+ }
284299 } ;
285300
286- ParseResult parseResult = command . Parse ( "cmd --1 true --3 false --2 true " ) ;
301+ ParseResult parseResult = command . Parse ( "--1 --3 --2" ) ;
287302
288303 using var _ = new AssertionScope ( ) ;
289304
290305 parseResult . Action . Should ( ) . Be ( optionAction2 ) ;
291- parseResult . Invoke ( ) . Should ( ) . Be ( 0 ) ;
306+
307+ if ( invokeAsync )
308+ {
309+ ( await parseResult . InvokeAsync ( ) ) . Should ( ) . Be ( 0 ) ;
310+ }
311+ else
312+ {
313+ parseResult . Invoke ( ) . Should ( ) . Be ( 0 ) ;
314+ }
315+
292316 optionAction1WasCalled . Should ( ) . BeFalse ( ) ;
293317 optionAction2WasCalled . Should ( ) . BeTrue ( ) ;
294318 optionAction3WasCalled . Should ( ) . BeFalse ( ) ;
295319 }
296320
321+ [ Theory ]
322+ [ InlineData ( true ) ]
323+ [ InlineData ( false ) ]
324+ public async Task When_multiple_options_with_nonterminating_actions_are_present_then_all_are_invoked ( bool invokeAsync )
325+ {
326+ bool optionAction1WasCalled = false ;
327+ bool optionAction2WasCalled = false ;
328+ bool optionAction3WasCalled = false ;
329+ bool commandActionWasCalled = false ;
330+
331+ SynchronousTestAction optionAction1 = new ( _ =>
332+ {
333+ optionAction1WasCalled = true ;
334+ } , terminating : false ) ;
335+ SynchronousTestAction optionAction2 = new ( _ =>
336+ {
337+ optionAction2WasCalled = true ;
338+ } , terminating : false ) ;
339+ SynchronousTestAction optionAction3 = new ( _ =>
340+ {
341+ optionAction3WasCalled = true ;
342+ } , terminating : false ) ;
343+
344+ var command = new RootCommand
345+ {
346+ Action = new AsynchronousTestAction ( _ => commandActionWasCalled = true ) ,
347+ Options =
348+ {
349+ new Option < bool > ( "--1" ) { Action = optionAction1 } ,
350+ new Option < bool > ( "--2" ) { Action = optionAction2 } ,
351+ new Option < bool > ( "--3" ) { Action = optionAction3 } ,
352+ }
353+ } ;
354+
355+ ParseResult parseResult = command . Parse ( "--1 true --3 false --2 true" ) ;
356+
357+ using var _ = new AssertionScope ( ) ;
358+
359+ if ( invokeAsync )
360+ {
361+ ( await parseResult . InvokeAsync ( ) ) . Should ( ) . Be ( 0 ) ;
362+ }
363+ else
364+ {
365+ parseResult . Invoke ( ) . Should ( ) . Be ( 0 ) ;
366+ }
367+
368+ optionAction1WasCalled . Should ( ) . BeTrue ( ) ;
369+ optionAction2WasCalled . Should ( ) . BeTrue ( ) ;
370+ optionAction3WasCalled . Should ( ) . BeTrue ( ) ;
371+ commandActionWasCalled . Should ( ) . BeTrue ( ) ;
372+ }
373+
297374 [ Fact ]
298375 public void Directive_action_takes_precedence_over_option_action ( )
299376 {
@@ -327,9 +404,12 @@ public void Directive_action_takes_precedence_over_option_action()
327404 [ Theory ]
328405 [ InlineData ( true ) ]
329406 [ InlineData ( false ) ]
330- public async Task Nontermninating_option_actions_handle_exceptions_and_return_an_error_return_code ( bool invokeAsync )
407+ public async Task Nonterminating_option_actions_handle_exceptions_and_return_an_error_return_code ( bool invokeAsync )
331408 {
332- var nonexclusiveAction = new SynchronousTestAction ( _ => throw new Exception ( "oops!" ) , terminating : false ) ;
409+ var nonexclusiveAction = new SynchronousTestAction ( _ =>
410+ {
411+ throw new Exception ( "oops!" ) ;
412+ } , terminating : false ) ;
333413
334414 var command = new RootCommand
335415 {
0 commit comments