Skip to content

Commit 9d9132e

Browse files
committed
fix implicit parser side effect when usnig CommandLineBuilder
1 parent 89211a6 commit 9d9132e

File tree

5 files changed

+16
-27
lines changed

5 files changed

+16
-27
lines changed

src/System.CommandLine.Tests/CommandExtensionsTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public void Command_Invoke_can_be_called_more_than_once_for_the_same_command()
3232
}
3333

3434
[Fact]
35-
public void When_CommandLineBuilder_is_used_then_Command_Invoke_uses_its_configuration()
35+
public void When_CommandLineBuilder_is_used_then_Command_Invoke_does_not_use_its_configuration()
3636
{
3737
var command = new RootCommand();
3838

@@ -50,7 +50,7 @@ public void When_CommandLineBuilder_is_used_then_Command_Invoke_uses_its_configu
5050
console.Out
5151
.ToString()
5252
.Should()
53-
.Contain("hello!");
53+
.NotContain("hello!");
5454
}
5555
}
5656
}

src/System.CommandLine/CommandExtensions.cs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public static int Invoke(
2626
string[] args,
2727
IConsole? console = null)
2828
{
29-
return GetInvocationPipeline(command, args).Invoke(console);
29+
return GetDefaultInvocationPipeline(command, args).Invoke(console);
3030
}
3131

3232
/// <summary>
@@ -55,7 +55,7 @@ public static async Task<int> InvokeAsync(
5555
string[] args,
5656
IConsole? console = null)
5757
{
58-
return await GetInvocationPipeline(command, args).InvokeAsync(console);
58+
return await GetDefaultInvocationPipeline(command, args).InvokeAsync(console);
5959
}
6060

6161
/// <summary>
@@ -72,14 +72,16 @@ public static Task<int> InvokeAsync(
7272
IConsole? console = null) =>
7373
command.InvokeAsync(CommandLineStringSplitter.Instance.Split(commandLine).ToArray(), console);
7474

75-
private static InvocationPipeline GetInvocationPipeline(Command command, string[] args)
75+
private static InvocationPipeline GetDefaultInvocationPipeline(Command command, string[] args)
7676
{
77-
var parser = command.ImplicitParser ??
78-
new CommandLineBuilder(command)
79-
.UseDefaults()
80-
.Build();
77+
if (command.ImplicitParser is null)
78+
{
79+
command.ImplicitParser = new CommandLineBuilder(command)
80+
.UseDefaults()
81+
.Build();
82+
}
8183

82-
var parseResult = parser.Parse(args);
84+
var parseResult = command.ImplicitParser.Parse(args);
8385

8486
return new InvocationPipeline(parseResult);
8587
}

src/System.CommandLine/Parsing/ParseResultExtensions.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,8 @@
22
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
33

44
using System.Collections;
5-
using System.Collections.Generic;
65
using System.CommandLine.Binding;
76
using System.CommandLine.Invocation;
8-
using System.CommandLine.Completions;
97
using System.Linq;
108
using System.Text;
119
using System.Threading.Tasks;

src/System.CommandLine/Parsing/Parser.cs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,6 @@ public class Parser
1515
public Parser(CommandLineConfiguration configuration)
1616
{
1717
Configuration = configuration ?? throw new ArgumentNullException(nameof(configuration));
18-
19-
if (configuration.RootCommand is Command { ImplicitParser: null} cmd)
20-
{
21-
cmd.ImplicitParser = this;
22-
}
2318
}
2419

2520
/// <param name="command">The root command for the parser.</param>

src/System.CommandLine/SymbolExtensions.cs

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -37,19 +37,13 @@ internal static Parser GetOrCreateDefaultParser(this Symbol symbol)
3737
{
3838
var root = GetOrCreateRootCommand(symbol);
3939

40-
if (root.ImplicitParser is { } parser)
40+
if (root.ImplicitParser is not { } parser)
4141
{
42-
return parser;
43-
}
44-
else
45-
{
46-
return BuildParser(root);
42+
parser = new Parser(new CommandLineConfiguration(root));
43+
root.ImplicitParser = parser;
4744
}
4845

49-
static Parser BuildParser(Command cmd)
50-
{
51-
return new Parser(new CommandLineConfiguration(cmd));
52-
}
46+
return parser;
5347
}
5448

5549
internal static Command GetOrCreateRootCommand(Symbol symbol)

0 commit comments

Comments
 (0)