-
Notifications
You must be signed in to change notification settings - Fork 406
Description
The CommandLineStringSplitter
provides a way to split a string
into a string[]
and is intended to conform to the way that strings are split on the command line.
It has two primary use cases:
- Testing. Testing your parser using
Parse(string[] args)
orInvoke(string[] args)
can be error prone because it requires people to make assumptions about how a command line will actually be split when your app is started, and the actual rules are not always intuitive. - Completions.
CommandLineStringSplitter
is critical for completions, since the only way to capture the text on the command line from within the launched .NET application is to pass it in as a complete string from the shim shell script and then split it into the equivalentstring[] args
array thatMain
receives.
Various shells will exhibit different behaviors here due to different quote escaping rules, so the user's experience of this splitting logic might not match up with a single implementation of CommandLineStringSplitter
. Because of this, and to eventually provide higher fidelity for the completions experience on different shells in the presence of escapes on the command line, it might be useful to provide multiple implementations over time and we should consider anticipating that need in the API design now.
Resources
- "Parsing C++ Command-Line Arguments"
- Lots of discussion here: https://stackoverflow.com/questions/298830/split-string-containing-command-line-parameters-into-string-in-c-sharp
- "Everyone quotes command line arguments the wrong way" (h/t @ericsampson)
- "What's up with the strange treatment of quotation marks and backslashes by CommandLineToArgvW" (h/t @ericsampson)
- https://github.com/dotnet/runtime/blob/732ae12c9cdd8227ab2882f0949d798c3fcb446d/src/libraries/System.Private.CoreLib/src/System/Environment.Windows.cs#L223
Related
- Use 'CommandLineStringSplitter.Instance.Split' parse bug. #1740
- Question: how could I read a raw json content #1755
For the purposes of illustration in examples below, I'll be using the following program to confirm behaviors that differ among various shells:
Console.WriteLine("---- DEFAULT SPLIT ---");
foreach (var arg in args)
{
Console.WriteLine(arg);
}
Console.WriteLine("---- Environment.CommandLine ----");
Console.WriteLine(Environment.CommandLine);