dotnet / dotnet/command-line-api
CommandLineStringSplitter does not conform to "standard" quote escaping rules
- Dominant language
- C#
- Stars
- 3.7k
- Forks
- 428
- PR merge metrics
- No merged PRs in 30d
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)` or `Invoke(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 equivalent `string[] args` array that `Main` 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](https://docs.microsoft.com/en-us/previous-versions/17w5ykft(v=vs.85))"
* 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](https://docs.microsoft.com/en-us/archive/blogs/twistylittlepassagesallalike/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](https://devblogs.microsoft.com/oldnewthing/20100917-00/?p=12833)" (h/t @ericsampson)
* https://github.com/dotnet/runtime/blob/732ae12c9cdd8227ab2882f0949d798c3fcb446d/src/libraries/System.Private.CoreLib/src/System/Environment.Windows.cs#L223
## Related
- [ ] #1740
- [x] #1755
------
For the purposes of illustration in examples below, I'll be using the following program to confirm behaviors that differ among various shells:
```csharp
Console.WriteLine("---- DEFAULT SPLIT ---");
foreach (var arg in args)
{
Console.WriteLine(arg);
}
Console.WriteLine("---- Environment.CommandLine ----");
Console.WriteLine(Environment.CommandLine);
```
Contributor guide
Assessment
This issue has not been assessed yet.