Skip to content

Commit 008524c

Browse files
committed
Address PR comments
1 parent c65020e commit 008524c

File tree

13 files changed

+64
-50
lines changed

13 files changed

+64
-50
lines changed

src/System.CommandLine.Tests/DirectiveTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,14 +72,14 @@ public async Task Multiple_instances_of_the_same_directive_can_be_invoked(bool i
7272
var testDirective = new TestDirective("test")
7373
{
7474
Action = invokeAsync
75-
? new AsynchronousNonterminatingTestAction(incrementCallCount, terminating: false)
75+
? new AsynchronousTestAction(incrementCallCount, terminating: false)
7676
: new SynchronousTestAction(incrementCallCount, terminating: false)
7777
};
7878

7979
var config = new CliConfiguration(new CliRootCommand
8080
{
8181
Action = invokeAsync
82-
? new AsynchronousNonterminatingTestAction(verifyActionWasCalled, terminating: false)
82+
? new AsynchronousTestAction(verifyActionWasCalled, terminating: false)
8383
: new SynchronousTestAction(verifyActionWasCalled, terminating: false)
8484
})
8585
{

src/System.CommandLine.Tests/HelpOptionTests.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -179,12 +179,11 @@ public async Task Help_option_with_custom_aliases_default_aliases_replaced(strin
179179
CliConfiguration config = new(command)
180180
{
181181
Output = new StringWriter(),
182-
EnableParseErrorReporting = false
183182
};
184183

185184
await config.InvokeAsync(helpAlias);
186185

187-
config.Output.ToString().Should().Be("");
186+
config.Output.ToString().Should().NotContain(helpAlias);
188187
}
189188
}
190189
}

src/System.CommandLine.Tests/Invocation/InvocationTests.cs

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
using System.CommandLine.Invocation;
66
using System.CommandLine.Parsing;
77
using System.IO;
8-
using System.Runtime.InteropServices.ComTypes;
98
using System.Threading;
109
using System.Threading.Tasks;
1110
using FluentAssertions;
@@ -195,10 +194,10 @@ public async Task Anonymous_RootCommand_int_returning_Action_can_set_custom_resu
195194
}
196195

197196
[Fact]
198-
public void Option_action_takes_precedence_over_command_action()
197+
public void Terminating_option_action_short_circuits_command_action()
199198
{
200-
bool wasCalled = false;
201-
SynchronousTestAction optionAction = new(_ => wasCalled = true);
199+
bool optionActionWasCalled = false;
200+
SynchronousTestAction optionAction = new(_ => optionActionWasCalled = true, terminating: true);
202201
bool commandActionWasCalled = false;
203202

204203
CliOption<bool> option = new("--test")
@@ -214,17 +213,41 @@ public void Option_action_takes_precedence_over_command_action()
214213
commandActionWasCalled = true;
215214
});
216215

217-
ParseResult parseResult = command.Parse("cmd --test true");
216+
ParseResult parseResult = command.Parse("cmd --test");
218217

219218
parseResult.Action.Should().NotBeNull();
220-
wasCalled.Should().BeFalse();
219+
optionActionWasCalled.Should().BeFalse();
221220
commandActionWasCalled.Should().BeFalse();
222221

223222
parseResult.Invoke().Should().Be(0);
224-
wasCalled.Should().BeTrue();
223+
optionActionWasCalled.Should().BeTrue();
225224
commandActionWasCalled.Should().BeFalse();
226225
}
227226

227+
[Fact]
228+
public void Nonterminating_option_action_does_not_short_circuit_command_action()
229+
{
230+
bool optionActionWasCalled = false;
231+
SynchronousTestAction optionAction = new(_ => optionActionWasCalled = true, terminating: false);
232+
bool commandActionWasCalled = false;
233+
234+
CliOption<bool> option = new("--test")
235+
{
236+
Action = optionAction
237+
};
238+
CliCommand command = new CliCommand("cmd")
239+
{
240+
option
241+
};
242+
command.SetAction(_ => { commandActionWasCalled = true; });
243+
244+
ParseResult parseResult = command.Parse("cmd --test");
245+
246+
parseResult.Invoke().Should().Be(0);
247+
optionActionWasCalled.Should().BeTrue();
248+
commandActionWasCalled.Should().BeTrue();
249+
}
250+
228251
[Fact]
229252
public void When_multiple_options_with_actions_are_present_then_only_the_last_one_is_invoked()
230253
{
@@ -286,7 +309,7 @@ public void Directive_action_takes_precedence_over_option_action()
286309
[Theory]
287310
[InlineData(true)]
288311
[InlineData(false)]
289-
public async Task Nonexclusive_actions_handle_exceptions_and_return_an_error_return_code(bool invokeAsync)
312+
public async Task Nontermninating_actions_handle_exceptions_and_return_an_error_return_code(bool invokeAsync)
290313
{
291314
var nonexclusiveAction = new NonexclusiveTestAction
292315
{

src/System.CommandLine.Tests/Invocation/TypoCorrectionTests.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
using System.CommandLine.Help;
12
using System.CommandLine.Invocation;
23
using System.IO;
4+
using System.Linq;
35
using System.Threading.Tasks;
46
using FluentAssertions;
57
using Xunit;
@@ -151,14 +153,18 @@ public async Task Arguments_are_not_suggested()
151153
argument,
152154
command
153155
};
156+
154157
CliConfiguration configuration = new(rootCommand)
155158
{
156-
EnableParseErrorReporting = false,
157159
Output = new StringWriter()
158160
};
159161

160162
var result = rootCommand.Parse("een", configuration);
161163

164+
var parseErrorAction = (ParseErrorAction)result.Action;
165+
parseErrorAction.ShowHelp = false;
166+
parseErrorAction.ShowTypoCorrections = true;
167+
162168
await result.InvokeAsync();
163169

164170
configuration.Output.ToString().Should().NotContain("the-argument");

src/System.CommandLine.Tests/ParseErrorReportingTests.cs

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
// Copyright (c) .NET Foundation and contributors. All rights reserved.
22
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
33

4-
// Copyright (c) .NET Foundation and contributors. All rights reserved.
5-
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
6-
74
using System.CommandLine.Help;
85
using System.CommandLine.Invocation;
96
using System.IO;
@@ -23,16 +20,11 @@ public void Parse_error_reporting_reports_error_when_help_is_used_and_required_s
2320
new HelpOption()
2421
};
2522

26-
CliConfiguration config = new (root)
27-
{
28-
EnableParseErrorReporting = true
29-
};
30-
31-
var parseResult = root.Parse("", config);
23+
var parseResult = root.Parse("");
3224

3325
parseResult.Errors.Should().NotBeEmpty();
3426

35-
var result = config.Invoke("");
27+
var result = parseResult.Invoke();
3628

3729
result.Should().Be(1);
3830
}

src/System.CommandLine.Tests/TestCliActions.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,11 @@ public override int Invoke(ParseResult parseResult)
2424
}
2525
}
2626

27-
public class AsynchronousNonterminatingTestAction : AsynchronousCliAction
27+
public class AsynchronousTestAction : AsynchronousCliAction
2828
{
2929
private readonly Action<ParseResult> _invoke;
3030

31-
public AsynchronousNonterminatingTestAction(Action<ParseResult> invoke, bool terminating = true)
31+
public AsynchronousTestAction(Action<ParseResult> invoke, bool terminating = true)
3232
{
3333
_invoke = invoke;
3434
Terminating = terminating;

src/System.CommandLine/CliConfiguration.cs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,6 @@ public CliConfiguration(CliCommand rootCommand)
6565
/// </summary>
6666
public bool EnableDefaultExceptionHandler { get; set; } = true;
6767

68-
/// <summary>
69-
/// Configures the command line to write error information to standard error when there are errors parsing command line input. Enabled by default.
70-
/// </summary>
71-
public bool EnableParseErrorReporting { get; set; } = true;
72-
7368
/// <summary>
7469
/// Enables signaling and handling of process termination (Ctrl+C, SIGINT, SIGTERM) via a <see cref="CancellationToken"/>
7570
/// that can be passed to a <see cref="CliAction"/> during invocation.

src/System.CommandLine/Invocation/AnonymousAsynchronousCliAction.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
namespace System.CommandLine.Invocation;
88

9-
internal class AnonymousAsynchronousCliAction : AsynchronousCliAction
9+
internal sealed class AnonymousAsynchronousCliAction : AsynchronousCliAction
1010
{
1111
private readonly Func<ParseResult, CancellationToken, Task<int>> _asyncAction;
1212

src/System.CommandLine/Invocation/AnonymousSynchronousCliAction.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
namespace System.CommandLine.Invocation;
55

6-
internal class AnonymousSynchronousCliAction : SynchronousCliAction
6+
internal sealed class AnonymousSynchronousCliAction : SynchronousCliAction
77
{
88
private readonly Func<ParseResult, int> _syncAction;
99

src/System.CommandLine/Invocation/CliAction.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,5 @@ private protected CliAction()
1515
/// <summary>
1616
/// Indicates that the action terminates a command line invocation, and later actions are skipped.
1717
/// </summary>
18-
public bool Terminating { get; protected set; } = true;
18+
public bool Terminating { get; protected init; } = true;
1919
}

0 commit comments

Comments
 (0)