Skip to content

Commit 3e0d47c

Browse files
authored
merge CommandLineStringSplitter into Parser (#2079)
* merge CommandLineStringSplitter into Parser, mostly to make it more discoverable * address code review feedback: rename Split to SplitCommandLine
1 parent c534e56 commit 3e0d47c

File tree

10 files changed

+135
-168
lines changed

10 files changed

+135
-168
lines changed

src/System.CommandLine.ApiCompatibility.Tests/ApiCompatibilityApprovalTests.System_CommandLine_api_is_not_changed.approved.txt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -336,8 +336,6 @@ System.CommandLine.Parsing
336336
public T GetValueOrDefault<T>()
337337
public System.Void OnlyTake(System.Int32 numberOfTokens)
338338
public System.String ToString()
339-
public class CommandLineStringSplitter
340-
public System.Collections.Generic.IEnumerable<System.String> Split(System.String commandLine)
341339
public class CommandResult : SymbolResult
342340
public System.Collections.Generic.IEnumerable<SymbolResult> Children { get; }
343341
public System.CommandLine.Command Command { get; }
@@ -361,6 +359,7 @@ System.CommandLine.Parsing
361359
public static class Parser
362360
public static System.CommandLine.ParseResult Parse(System.CommandLine.Command command, System.Collections.Generic.IReadOnlyList<System.String> args, System.CommandLine.CommandLineConfiguration configuration = null)
363361
public static System.CommandLine.ParseResult Parse(System.CommandLine.Command command, System.String commandLine, System.CommandLine.CommandLineConfiguration configuration = null)
362+
public static System.Collections.Generic.IEnumerable<System.String> SplitCommandLine(System.String commandLine)
364363
public static class ParseResultExtensions
365364
public static System.String Diagram(this System.CommandLine.ParseResult parseResult)
366365
public abstract class SymbolResult

src/System.CommandLine.Suggest.Tests/SuggestionDispatcherTests.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public async Task InvokeAsync_executes_registered_executable()
3939
{
4040
string receivedTargetExeName = null;
4141

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

4444
await InvokeAsync(
4545
args,
@@ -112,13 +112,13 @@ await InvokeAsync(
112112
private static string[] PrepareArgs(string args)
113113
{
114114
var formattableString = args.Replace("$", "");
115-
return CommandLineStringSplitter.Instance.Split(formattableString).ToArray();
115+
return Parser.SplitCommandLine(formattableString).ToArray();
116116
}
117117

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

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

137137
await dispatcher.InvokeAsync(args, testConsole);
138138

@@ -168,7 +168,7 @@ public async Task Register_command_adds_new_suggestion_entry()
168168
var provider = new TestSuggestionRegistration();
169169
var dispatcher = new SuggestionDispatcher(provider);
170170

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

173173
await dispatcher.InvokeAsync(args);
174174

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

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

187187
await dispatcher.InvokeAsync(args);
188188
await dispatcher.InvokeAsync(args);

src/System.CommandLine.Tests/ParserTests.RootCommandAndArg0.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,6 @@ public void When_parsing_an_unsplit_string_then_a_renamed_RootCommand_can_be_omi
101101
result3.RootCommandResult.Command.Should().BeSameAs(result1.RootCommandResult.Command);
102102
}
103103

104-
string[] Split(string value) => CommandLineStringSplitter.Instance.Split(value).ToArray();
104+
string[] Split(string value) => Parser.SplitCommandLine(value).ToArray();
105105
}
106106
}

src/System.CommandLine.Tests/ParserTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -473,7 +473,7 @@ public void Relative_order_of_arguments_and_options_within_a_command_does_not_ma
473473
[InlineData("not a valid command line --one 1")]
474474
public void Original_order_of_tokens_is_preserved_in_ParseResult_Tokens(string commandLine)
475475
{
476-
var rawSplit = CommandLineStringSplitter.Instance.Split(commandLine);
476+
var rawSplit = Parser.SplitCommandLine(commandLine);
477477

478478
var command = new Command("the-command")
479479
{

src/System.CommandLine.Tests/Parsing/CommandLineStringSplitterTests.cs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@ namespace System.CommandLine.Tests.Parsing
1111
{
1212
public class CommandLineStringSplitterTests
1313
{
14-
private readonly CommandLineStringSplitter _splitter = CommandLineStringSplitter.Instance;
15-
1614
[Theory]
1715
[InlineData("one two three four")]
1816
[InlineData("one two\tthree four ")]
@@ -21,7 +19,7 @@ public class CommandLineStringSplitterTests
2119
[InlineData(" one\r\ntwo\r\nthree\r\nfour\r\n")]
2220
public void It_splits_strings_based_on_whitespace(string commandLine)
2321
{
24-
_splitter.Split(commandLine)
22+
Parser.SplitCommandLine(commandLine)
2523
.Should()
2624
.BeEquivalentSequenceTo("one", "two", "three", "four");
2725
}
@@ -31,7 +29,7 @@ public void It_does_not_break_up_double_quote_delimited_values()
3129
{
3230
var commandLine = @"rm -r ""c:\temp files\""";
3331

34-
_splitter.Split(commandLine)
32+
Parser.SplitCommandLine(commandLine)
3533
.Should()
3634
.BeEquivalentSequenceTo("rm", "-r", @"c:\temp files\");
3735
}
@@ -51,7 +49,7 @@ public void It_does_not_split_double_quote_delimited_values_when_a_non_whitespac
5149

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

54-
_splitter.Split(commandLine)
52+
Parser.SplitCommandLine(commandLine)
5553
.Should()
5654
.BeEquivalentSequenceTo("the-command", optionAndArgument.Replace("\"", ""));
5755
}
@@ -64,7 +62,7 @@ public void It_handles_multiple_options_with_quoted_arguments()
6462

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

67-
var tokenized = _splitter.Split(commandLine);
65+
var tokenized = Parser.SplitCommandLine(commandLine);
6866

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

84-
_splitter.Split(commandLine)
82+
Parser.SplitCommandLine(commandLine)
8583
.Should()
8684
.BeEquivalentTo("POST", "--raw='{Id:1,Name:Alice}'");
8785
}
@@ -91,7 +89,7 @@ public void Internal_whitespaces_are_preserved_and_do_not_cause_string_to_be_spl
9189
{
9290
var commandLine = @"command --raw='{""Id"":1,""Movie Name"":""The Three Musketeers""}'";
9391

94-
_splitter.Split(commandLine)
92+
Parser.SplitCommandLine(commandLine)
9593
.Should()
9694
.BeEquivalentTo("command", "--raw='{Id:1,Movie Name:The Three Musketeers}'");
9795
}

src/System.CommandLine.Tests/SplitCommandLineTests.cs

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,20 +24,19 @@ public void It_splits_strings_based_on_whitespace()
2424
{
2525
var commandLine = "one two\tthree four ";
2626

27-
CommandLineStringSplitter.Instance
28-
.Split(commandLine)
29-
.Should()
30-
.BeEquivalentSequenceTo("one", "two", "three", "four");
27+
Parser
28+
.SplitCommandLine(commandLine)
29+
.Should()
30+
.BeEquivalentSequenceTo("one", "two", "three", "four");
3131
}
3232

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

38-
CommandLineStringSplitter
39-
.Instance
40-
.Split(commandLine)
38+
Parser
39+
.SplitCommandLine(commandLine)
4140
.Should()
4241
.BeEquivalentSequenceTo("rm", "-r", @"c:\temp files\");
4342
}
@@ -57,9 +56,8 @@ public void It_does_not_split_double_quote_delimited_values_when_a_non_whitespac
5756

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

60-
CommandLineStringSplitter
61-
.Instance
62-
.Split(commandLine)
59+
Parser
60+
.SplitCommandLine(commandLine)
6361
.Should()
6462
.BeEquivalentSequenceTo("the-command", optionAndArgument.Replace("\"", ""));
6563
}
@@ -72,7 +70,7 @@ public void It_handles_multiple_options_with_quoted_arguments()
7270

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

75-
var tokenized = CommandLineStringSplitter.Instance.Split(commandLine);
73+
var tokenized = Parser.SplitCommandLine(commandLine);
7674

7775
_output.WriteLine(commandLine);
7876

src/System.CommandLine/CommandExtensions.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public static int Invoke(
4343
this Command command,
4444
string commandLine,
4545
IConsole? console = null) =>
46-
command.Invoke(CommandLineStringSplitter.Instance.Split(commandLine).ToArray(), console);
46+
command.Invoke(Parser.SplitCommandLine(commandLine).ToArray(), console);
4747

4848
/// <summary>
4949
/// Parses and invokes a command.
@@ -78,6 +78,6 @@ public static Task<int> InvokeAsync(
7878
string commandLine,
7979
IConsole? console = null,
8080
CancellationToken cancellationToken = default) =>
81-
command.InvokeAsync(CommandLineStringSplitter.Instance.Split(commandLine).ToArray(), console, cancellationToken);
81+
command.InvokeAsync(Parser.SplitCommandLine(commandLine).ToArray(), console, cancellationToken);
8282
}
8383
}

src/System.CommandLine/Parsing/CommandLineStringSplitter.cs

Lines changed: 0 additions & 133 deletions
This file was deleted.

0 commit comments

Comments
 (0)