Skip to content

Commit e678d32

Browse files
authored
subcommand completions should include root command recursive options completions (#2209)
* add failing tests * fix the bug: ParseResult.GetResult(rootCommand) should return root command parse result * adjust existing tests to also include root command recursive options in completions
1 parent 1b85be5 commit e678d32

File tree

4 files changed

+47
-6
lines changed

4 files changed

+47
-6
lines changed

src/System.CommandLine.Tests/CompletionTests.cs

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,28 @@ public void Command_GetCompletions_can_access_ParseResult()
246246
.Should()
247247
.BeEquivalentTo("test");
248248
}
249-
249+
250+
[Fact]
251+
public void Command_GetCompletions_include_recursive_options_of_root_command()
252+
{
253+
CliRootCommand rootCommand = new()
254+
{
255+
new CliCommand("sub")
256+
{
257+
new CliOption<int>("--option")
258+
}
259+
};
260+
261+
var result = rootCommand.Parse("sub --option 123 ");
262+
263+
_output.WriteLine(result.ToString());
264+
265+
result.GetCompletions()
266+
.Select(item => item.Label)
267+
.Should()
268+
.BeEquivalentTo("--help", "-?", "-h", "/?", "/h");
269+
}
270+
250271
[Fact]
251272
public void When_one_option_has_been_specified_then_it_and_its_siblings_will_still_be_suggested()
252273
{
@@ -414,7 +435,7 @@ public void When_a_subcommand_with_subcommands_has_been_specified_then_its_sibli
414435
result.GetCompletions(commandLine.Length + 1)
415436
.Select(item => item.Label)
416437
.Should()
417-
.BeEquivalentTo("rainier");
438+
.BeEquivalentTo("--help", "-?", "-h", "/?", "/h", "rainier");
418439
}
419440

420441
[Fact]

src/System.CommandLine.Tests/ParseResultTests.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,23 @@ public void GetResult_can_be_used_to_check_the_presence_of_an_implicit_option()
5353
result.GetResult(option).Should().NotBeNull();
5454
}
5555

56+
[Fact]
57+
public void GetResult_can_be_used_for_root_command_itself()
58+
{
59+
CliRootCommand rootCommand = new()
60+
{
61+
new CliCommand("the-command")
62+
{
63+
new CliOption<int>("-c")
64+
}
65+
};
66+
67+
var result = rootCommand.Parse("the-command -c 123");
68+
69+
result.RootCommandResult.Command.Should().BeSameAs(rootCommand);
70+
result.GetResult(rootCommand).Should().BeSameAs(result.RootCommandResult);
71+
}
72+
5673
[Fact]
5774
public void Command_will_not_accept_a_command_if_a_sibling_command_has_already_been_accepted()
5875
{

src/System.CommandLine.Tests/SuggestDirectiveTests.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,9 @@ public async Task It_writes_suggestions_for_option_arguments_when_under_root_com
7979
}
8080

8181
[Theory]
82-
[InlineData("[suggest:4] \"eat\"")]
83-
[InlineData("[suggest:6] \"eat --\"")]
84-
public async Task It_writes_suggestions_for_option_aliases_under_subcommand(string commandLine)
82+
[InlineData("[suggest:4] \"eat\"", new[] { "--fruit", "--help", "--vegetable", "-?", "-h", "/?", "/h" })]
83+
[InlineData("[suggest:6] \"eat --\"", new[] { "--fruit", "--help", "--vegetable" })]
84+
public async Task It_writes_suggestions_for_option_aliases_under_subcommand(string commandLine, string[] expectedCompletions)
8585
{
8686
CliRootCommand rootCommand = new() { _eatCommand };
8787
CliConfiguration config = new(rootCommand)
@@ -94,10 +94,12 @@ public async Task It_writes_suggestions_for_option_aliases_under_subcommand(stri
9494

9595
await result.InvokeAsync();
9696

97+
string expected = string.Join(NewLine, expectedCompletions) + NewLine;
98+
9799
config.Output
98100
.ToString()
99101
.Should()
100-
.Be($"--fruit{NewLine}--vegetable{NewLine}");
102+
.Be(expected);
101103
}
102104

103105
[Theory]

src/System.CommandLine/Parsing/ParseOperation.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ public ParseOperation(
3636
_configuration.RootCommand,
3737
CurrentToken,
3838
_symbolResultTree);
39+
_symbolResultTree.Add(_configuration.RootCommand, _rootCommandResult);
3940

4041
Advance();
4142
}

0 commit comments

Comments
 (0)