Skip to content

Commit 6de04f7

Browse files
author
Jon Sequeira
committed
InvocationPipeline improvements based on PR feedback
1 parent 56e3af7 commit 6de04f7

File tree

1 file changed

+17
-33
lines changed

1 file changed

+17
-33
lines changed

src/System.CommandLine/Invocation/InvocationPipeline.cs

Lines changed: 17 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -15,42 +15,39 @@ internal static async Task<int> InvokeAsync(ParseResult parseResult, Cancellatio
1515

1616
try
1717
{
18-
int exitCode = 0;
18+
int actionResult = 0;
19+
int preActionResult = 0;
1920

2021
if (parseResult.PreActions is not null)
2122
{
2223
for (int i = 0; i < parseResult.PreActions.Count; i++)
2324
{
2425
var action = parseResult.PreActions[i];
25-
int preActionResult;
26+
var result = 0;
2627

2728
switch (action)
2829
{
2930
case SynchronousCommandLineAction syncAction:
30-
preActionResult = syncAction.Invoke(parseResult);
31+
result = syncAction.Invoke(parseResult);
3132
break;
3233
case AsynchronousCommandLineAction asyncAction:
33-
preActionResult = await asyncAction.InvokeAsync(parseResult, cts.Token);
34-
break;
35-
default:
36-
preActionResult = 0;
34+
result = await asyncAction.InvokeAsync(parseResult, cts.Token);
3735
break;
36+
3837
}
3938

40-
if (exitCode == 0)
39+
if (result != 0)
4140
{
42-
exitCode = preActionResult;
41+
preActionResult = result;
4342
}
4443
}
4544
}
4645

4746
if (parseResult.Action is null)
4847
{
49-
return exitCode != 0 ? exitCode : ReturnCodeForMissingAction(parseResult);
48+
return preActionResult != 0 ? preActionResult : ReturnCodeForMissingAction(parseResult);
5049
}
5150

52-
int actionResult;
53-
5451
switch (parseResult.Action)
5552
{
5653
case SynchronousCommandLineAction syncAction:
@@ -85,7 +82,7 @@ internal static async Task<int> InvokeAsync(ParseResult parseResult, Cancellatio
8582
throw new ArgumentOutOfRangeException(nameof(parseResult.Action));
8683
}
8784

88-
return exitCode != 0 ? exitCode : actionResult;
85+
return preActionResult != 0 ? preActionResult : actionResult;
8986
}
9087
catch (Exception ex) when (parseResult.InvocationConfiguration.EnableDefaultExceptionHandler)
9188
{
@@ -101,32 +98,19 @@ internal static int Invoke(ParseResult parseResult)
10198
{
10299
try
103100
{
104-
int exitCode = 0;
101+
int preActionResult = 0;
105102

106103
if (parseResult.PreActions is not null)
107104
{
108-
#if DEBUG
109-
for (var i = 0; i < parseResult.PreActions.Count; i++)
110-
{
111-
var action = parseResult.PreActions[i];
112-
113-
if (action is not SynchronousCommandLineAction)
114-
{
115-
parseResult.InvocationConfiguration.EnableDefaultExceptionHandler = false;
116-
throw new Exception(
117-
$"This should not happen. An instance of {nameof(AsynchronousCommandLineAction)} ({action}) was called within {nameof(InvocationPipeline)}.{nameof(Invoke)}. This is supposed to be detected earlier resulting in a call to {nameof(InvocationPipeline)}{nameof(InvokeAsync)}");
118-
}
119-
}
120-
#endif
121-
122105
for (var i = 0; i < parseResult.PreActions.Count; i++)
123106
{
124107
if (parseResult.PreActions[i] is SynchronousCommandLineAction syncPreAction)
125108
{
126-
int preActionResult = syncPreAction.Invoke(parseResult);
127-
if (exitCode == 0)
109+
int result = syncPreAction.Invoke(parseResult);
110+
111+
if (result != 0)
128112
{
129-
exitCode = preActionResult;
113+
preActionResult = result;
130114
}
131115
}
132116
}
@@ -135,11 +119,11 @@ internal static int Invoke(ParseResult parseResult)
135119
switch (parseResult.Action)
136120
{
137121
case null:
138-
return exitCode != 0 ? exitCode : ReturnCodeForMissingAction(parseResult);
122+
return preActionResult != 0 ? preActionResult : ReturnCodeForMissingAction(parseResult);
139123

140124
case SynchronousCommandLineAction syncAction:
141125
int actionResult = syncAction.Invoke(parseResult);
142-
return exitCode != 0 ? exitCode : actionResult;
126+
return preActionResult != 0 ? preActionResult : actionResult;
143127

144128
default:
145129
throw new InvalidOperationException($"{nameof(AsynchronousCommandLineAction)} called within non-async invocation.");

0 commit comments

Comments
 (0)