Skip to content

merge CommandLineStringSplitter into Parser #2079

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -336,8 +336,6 @@ System.CommandLine.Parsing
public T GetValueOrDefault<T>()
public System.Void OnlyTake(System.Int32 numberOfTokens)
public System.String ToString()
public class CommandLineStringSplitter
public System.Collections.Generic.IEnumerable<System.String> Split(System.String commandLine)
public class CommandResult : SymbolResult
public System.Collections.Generic.IEnumerable<SymbolResult> Children { get; }
public System.CommandLine.Command Command { get; }
Expand All @@ -361,6 +359,7 @@ System.CommandLine.Parsing
public static class Parser
public static System.CommandLine.ParseResult Parse(System.CommandLine.Command command, System.Collections.Generic.IReadOnlyList<System.String> args, System.CommandLine.CommandLineConfiguration configuration = null)
public static System.CommandLine.ParseResult Parse(System.CommandLine.Command command, System.String commandLine, System.CommandLine.CommandLineConfiguration configuration = null)
public static System.Collections.Generic.IEnumerable<System.String> SplitCommandLine(System.String commandLine)
public static class ParseResultExtensions
public static System.String Diagram(this System.CommandLine.ParseResult parseResult)
public abstract class SymbolResult
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public async Task InvokeAsync_executes_registered_executable()
{
string receivedTargetExeName = null;

string[] args = CommandLineStringSplitter.Instance.Split($@"get -p 12 -e ""{CurrentExeFullPath()}"" -- ""{_currentExeName} add""").ToArray();
string[] args = Parser.SplitCommandLine($@"get -p 12 -e ""{CurrentExeFullPath()}"" -- ""{_currentExeName} add""").ToArray();

await InvokeAsync(
args,
Expand Down Expand Up @@ -112,13 +112,13 @@ await InvokeAsync(
private static string[] PrepareArgs(string args)
{
var formattableString = args.Replace("$", "");
return CommandLineStringSplitter.Instance.Split(formattableString).ToArray();
return Parser.SplitCommandLine(formattableString).ToArray();
}

[Fact]
public async Task InvokeAsync_with_unknown_suggestion_provider_returns_empty_string()
{
string[] args = Enumerable.ToArray(( CommandLineStringSplitter.Instance.Split(@"get -p 10 -e ""testcli.exe"" -- command op")));
string[] args = Enumerable.ToArray(Parser.SplitCommandLine(@"get -p 10 -e ""testcli.exe"" -- command op"));
(await InvokeAsync(args, new TestSuggestionRegistration()))
.Should()
.BeEmpty();
Expand All @@ -132,7 +132,7 @@ public async Task When_command_suggestions_use_process_that_remains_open_it_retu
dispatcher.Timeout = TimeSpan.FromMilliseconds(1);
var testConsole = new TestConsole();

var args = CommandLineStringSplitter.Instance.Split($@"get -p 0 -e ""{_currentExeName}"" -- {_currentExeName} add").ToArray();
var args = Parser.SplitCommandLine($@"get -p 0 -e ""{_currentExeName}"" -- {_currentExeName} add").ToArray();

await dispatcher.InvokeAsync(args, testConsole);

Expand Down Expand Up @@ -168,7 +168,7 @@ public async Task Register_command_adds_new_suggestion_entry()
var provider = new TestSuggestionRegistration();
var dispatcher = new SuggestionDispatcher(provider);

var args = CommandLineStringSplitter.Instance.Split($"register --command-path \"{_netExeFullPath}\"").ToArray();
var args = Parser.SplitCommandLine($"register --command-path \"{_netExeFullPath}\"").ToArray();

await dispatcher.InvokeAsync(args);

Expand All @@ -182,7 +182,7 @@ public async Task Register_command_will_not_add_duplicate_entry()
var provider = new TestSuggestionRegistration();
var dispatcher = new SuggestionDispatcher(provider);

var args = CommandLineStringSplitter.Instance.Split($"register --command-path \"{_netExeFullPath}\"").ToArray();
var args = Parser.SplitCommandLine($"register --command-path \"{_netExeFullPath}\"").ToArray();

await dispatcher.InvokeAsync(args);
await dispatcher.InvokeAsync(args);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,6 @@ public void When_parsing_an_unsplit_string_then_a_renamed_RootCommand_can_be_omi
result3.RootCommandResult.Command.Should().BeSameAs(result1.RootCommandResult.Command);
}

string[] Split(string value) => CommandLineStringSplitter.Instance.Split(value).ToArray();
string[] Split(string value) => Parser.SplitCommandLine(value).ToArray();
}
}
2 changes: 1 addition & 1 deletion src/System.CommandLine.Tests/ParserTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -473,7 +473,7 @@ public void Relative_order_of_arguments_and_options_within_a_command_does_not_ma
[InlineData("not a valid command line --one 1")]
public void Original_order_of_tokens_is_preserved_in_ParseResult_Tokens(string commandLine)
{
var rawSplit = CommandLineStringSplitter.Instance.Split(commandLine);
var rawSplit = Parser.SplitCommandLine(commandLine);

var command = new Command("the-command")
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ namespace System.CommandLine.Tests.Parsing
{
public class CommandLineStringSplitterTests
{
private readonly CommandLineStringSplitter _splitter = CommandLineStringSplitter.Instance;

[Theory]
[InlineData("one two three four")]
[InlineData("one two\tthree four ")]
Expand All @@ -21,7 +19,7 @@ public class CommandLineStringSplitterTests
[InlineData(" one\r\ntwo\r\nthree\r\nfour\r\n")]
public void It_splits_strings_based_on_whitespace(string commandLine)
{
_splitter.Split(commandLine)
Parser.SplitCommandLine(commandLine)
.Should()
.BeEquivalentSequenceTo("one", "two", "three", "four");
}
Expand All @@ -31,7 +29,7 @@ public void It_does_not_break_up_double_quote_delimited_values()
{
var commandLine = @"rm -r ""c:\temp files\""";

_splitter.Split(commandLine)
Parser.SplitCommandLine(commandLine)
.Should()
.BeEquivalentSequenceTo("rm", "-r", @"c:\temp files\");
}
Expand All @@ -51,7 +49,7 @@ public void It_does_not_split_double_quote_delimited_values_when_a_non_whitespac

var commandLine = $"the-command {optionAndArgument}";

_splitter.Split(commandLine)
Parser.SplitCommandLine(commandLine)
.Should()
.BeEquivalentSequenceTo("the-command", optionAndArgument.Replace("\"", ""));
}
Expand All @@ -64,7 +62,7 @@ public void It_handles_multiple_options_with_quoted_arguments()

var commandLine = $"move --from \"{source}\" --to \"{destination}\" --verbose";

var tokenized = _splitter.Split(commandLine);
var tokenized = Parser.SplitCommandLine(commandLine);

tokenized.Should()
.BeEquivalentSequenceTo(
Expand All @@ -81,7 +79,7 @@ public void Internal_quotes_do_not_cause_string_to_be_split()
{
var commandLine = @"POST --raw='{""Id"":1,""Name"":""Alice""}'";

_splitter.Split(commandLine)
Parser.SplitCommandLine(commandLine)
.Should()
.BeEquivalentTo("POST", "--raw='{Id:1,Name:Alice}'");
}
Expand All @@ -91,7 +89,7 @@ public void Internal_whitespaces_are_preserved_and_do_not_cause_string_to_be_spl
{
var commandLine = @"command --raw='{""Id"":1,""Movie Name"":""The Three Musketeers""}'";

_splitter.Split(commandLine)
Parser.SplitCommandLine(commandLine)
.Should()
.BeEquivalentTo("command", "--raw='{Id:1,Movie Name:The Three Musketeers}'");
}
Expand Down
20 changes: 9 additions & 11 deletions src/System.CommandLine.Tests/SplitCommandLineTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,19 @@ public void It_splits_strings_based_on_whitespace()
{
var commandLine = "one two\tthree four ";

CommandLineStringSplitter.Instance
.Split(commandLine)
.Should()
.BeEquivalentSequenceTo("one", "two", "three", "four");
Parser
.SplitCommandLine(commandLine)
.Should()
.BeEquivalentSequenceTo("one", "two", "three", "four");
}

[Fact]
public void It_does_not_break_up_double_quote_delimited_values()
{
var commandLine = @"rm -r ""c:\temp files\""";

CommandLineStringSplitter
.Instance
.Split(commandLine)
Parser
.SplitCommandLine(commandLine)
.Should()
.BeEquivalentSequenceTo("rm", "-r", @"c:\temp files\");
}
Expand All @@ -57,9 +56,8 @@ public void It_does_not_split_double_quote_delimited_values_when_a_non_whitespac

var commandLine = $"the-command {optionAndArgument}";

CommandLineStringSplitter
.Instance
.Split(commandLine)
Parser
.SplitCommandLine(commandLine)
.Should()
.BeEquivalentSequenceTo("the-command", optionAndArgument.Replace("\"", ""));
}
Expand All @@ -72,7 +70,7 @@ public void It_handles_multiple_options_with_quoted_arguments()

var commandLine = $"move --from \"{source}\" --to \"{destination}\"";

var tokenized = CommandLineStringSplitter.Instance.Split(commandLine);
var tokenized = Parser.SplitCommandLine(commandLine);

_output.WriteLine(commandLine);

Expand Down
4 changes: 2 additions & 2 deletions src/System.CommandLine/CommandExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public static int Invoke(
this Command command,
string commandLine,
IConsole? console = null) =>
command.Invoke(CommandLineStringSplitter.Instance.Split(commandLine).ToArray(), console);
command.Invoke(Parser.SplitCommandLine(commandLine).ToArray(), console);

/// <summary>
/// Parses and invokes a command.
Expand Down Expand Up @@ -78,6 +78,6 @@ public static Task<int> InvokeAsync(
string commandLine,
IConsole? console = null,
CancellationToken cancellationToken = default) =>
command.InvokeAsync(CommandLineStringSplitter.Instance.Split(commandLine).ToArray(), console, cancellationToken);
command.InvokeAsync(Parser.SplitCommandLine(commandLine).ToArray(), console, cancellationToken);
}
}
133 changes: 0 additions & 133 deletions src/System.CommandLine/Parsing/CommandLineStringSplitter.cs

This file was deleted.

Loading