diff --git a/src/System.CommandLine.Tests/CompletionContextTests.cs b/src/System.CommandLine.Tests/CompletionContextTests.cs index de5e33c5ab..71f28db1e7 100644 --- a/src/System.CommandLine.Tests/CompletionContextTests.cs +++ b/src/System.CommandLine.Tests/CompletionContextTests.cs @@ -2,8 +2,11 @@ // Licensed under the MIT license. See LICENSE file in the project root for full license information. using System.CommandLine.Completions; +using ApprovalTests.Namers; using FluentAssertions; using Xunit; +using System.CommandLine; +using System.Threading; namespace System.CommandLine.Tests { @@ -33,6 +36,128 @@ public void CommandLineText_preserves_command_line_prior_to_splitting_when_compl .Be(commandLine); } + [Fact] + public void CommandLineText_is_parsed_when_option_other_than_last_is_in_name_equals_sign_value_format() + { + + CliRootCommand command = new CliRootCommand + { + new CliCommand("inner") + { + new CliOption("--optionOne"), + new CliOption("--optionTwo") + } + }; + + var commandLine = "inner --optionOne=argument1 --optionTwo argument2"; + + var parseResult = command.Parse(commandLine); + parseResult.GetCompletions(); + + Assert.True(parseResult.Tokens[1].Value == "--optionOne"); + Assert.True(parseResult.Tokens[2].Value == "argument1"); + Assert.True(parseResult.Tokens[3].Value == "--optionTwo"); + Assert.True(parseResult.Tokens[4].Value == "argument2"); + + } + + [Fact] + public void CommandLineText_is_parsed_when_last_option_is_in_name_equals_sign_value_format() + { + + CliRootCommand command = new CliRootCommand + { + new CliCommand("inner") + { + new CliOption("--optionOne"), + new CliOption("--optionTwo") + } + }; + + var commandLine = "inner --optionOne argument1 --optionTwo=argument2"; + + var parseResult = command.Parse(commandLine); + parseResult.GetCompletions(); + + Assert.True(parseResult.Tokens[1].Value == "--optionOne"); + Assert.True(parseResult.Tokens[2].Value == "argument1"); + Assert.True(parseResult.Tokens[3].Value == "--optionTwo"); + Assert.True(parseResult.Tokens[4].Value == "argument2"); + + } + + [Fact] + public void CommandLineText_is_parsed_when_equal_sign_used_in_multiple_option_params() + { + + CliRootCommand command = new CliRootCommand + { + new CliCommand("inner") + { + new CliOption("--optionOne"), + new CliOption("--optionTwo") + } + }; + + var commandLine = "inner --optionOne=argument1 --optionTwo=argument2"; + + var parseResult = command.Parse(commandLine); + parseResult.GetCompletions(); + + Assert.True(parseResult.Tokens[1].Value == "--optionOne"); + Assert.True(parseResult.Tokens[2].Value == "argument1"); + Assert.True(parseResult.Tokens[3].Value == "--optionTwo"); + Assert.True(parseResult.Tokens[4].Value == "argument2"); + + } + + [Fact] + public void CommandLineText_is_parsed_when_equal_sign_used_in_option_value() + { + + CliRootCommand command = new CliRootCommand + { + new CliCommand("inner") + { + new CliOption("--optionOne"), + new CliOption("--optionTwo") + } + }; + + var commandLine = "inner --optionOne -=Yay=-"; + + var parseResult = command.Parse(commandLine); + parseResult.GetCompletions(); + + Assert.True(parseResult.Tokens[0].Value == "inner"); + Assert.True(parseResult.Tokens[1].Value == "--optionOne"); + Assert.True(parseResult.Tokens[2].Value == "-=Yay=-"); + + } + + [Fact] + public void CommandLineText_is_parsed_when_equal_sign_used_in_option_value_and_as_option_value_spacer() + { + + CliRootCommand command = new CliRootCommand + { + new CliCommand("inner") + { + new CliOption("--optionOne") + } + }; + + var commandLine = "inner --optionOne=-=Yay=-"; + + var parseResult = command.Parse(commandLine); + parseResult.GetCompletions(); + + Assert.True(parseResult.Tokens[0].Value == "inner"); + Assert.True(parseResult.Tokens[1].Value == "--optionOne"); + Assert.True(parseResult.Tokens[2].Value == "-=Yay=-"); + + } + [Fact] public void CommandLineText_is_preserved_when_adjusting_position() { diff --git a/src/System.CommandLine/Completions/CompletionContext.cs b/src/System.CommandLine/Completions/CompletionContext.cs index 0ce1544cae..53b566a284 100644 --- a/src/System.CommandLine/Completions/CompletionContext.cs +++ b/src/System.CommandLine/Completions/CompletionContext.cs @@ -87,8 +87,11 @@ protected static string GetWordToComplete( var textAfterCursor = rawInput.Substring(position.Value); - return textBeforeCursor.Split(' ').LastOrDefault() + + var lastOrFirstWord = textBeforeCursor.Split(' ').LastOrDefault() + textAfterCursor.Split(' ').FirstOrDefault(); + + return (lastOrFirstWord.StartsWith("--")) ? lastOrFirstWord.Split(new[] { '=' }, 2).Last() : lastOrFirstWord; + } return "";