Replies: 6 comments
-
The string splitter is intended to emulate the behavior of the command line splitter that prepares the arguments passed to There's a related discussion here: #1740. Using the example program from this comment, here's what your examples produce (assuming I got the escaping right): This is with PowerShell. The first two aren't valid PowerShell and the program never even gets invoked. The last one works because the surrounding single quotes are valid escapes in PowerShell, but In cmd.exe the same command lines do this: None of these survive as valid JSON. So I would ask a different question: How do you expect your users to write raw JSON content? |
Beta Was this translation helpful? Give feedback.
-
But it doesn't with respect to escaping of double quotes. I don't know with certainty if the intent of the question is whether the CommandLineSplitter.Split method supports escaping of double quotes in some way, but i tend to belive so; the code in the question showing attempts to find some working escaping scheme for the double quotes implies to me the question is how to escape double quotes as part of argument values so they survive the CommandLineSplitter.Split method. But with respect to escaping double quotes CommandLineSplitter.Split does not emulate the escaping scheme that works in cmd.exe nor that which is working in PS. Background: It is possible both in cmd.exe and in Powershell to escape/quote the json string in such a manner that the quotes that form part of the value survive and are passed properly into the args array of the Main method. Given this very simple console application: static void Main(string[] args)
{
foreach (var a in args)
Console.WriteLine($">{a}<");
} entering in a cmd.exe shell:
will output:
Note how the double quotes are surviving. CommandLineSplitter does not emulate this cmd.exe escaping scheme. And entering in PS:
will again yield the output:
And CommandLineSplitter does not emulate this PS escaping scheme either. |
Beta Was this translation helpful? Give feedback.
-
Keep in mind that during command line invocation of your app, The main goal of the If you want to assume that your users will correctly escape JSON in order to pass it to your application, it's more correct to test using a string array, e.g.: var result = parser.Parse(new string[] { "--raw", @"{""Id"":1,""Name"":""Test""}"); |
Beta Was this translation helpful? Give feedback.
-
CommandLineStringSplitter is also used in the Invoke/Parse methods that accept a single string. And it would perhaps be good if the API documentation would make note of the fact that it does not support escaping of double quotes or other characters. Currently, the API documentation for these (extension) methods has only a simple statement: "The command line string input will be split into tokens as if it had been passed on the command line." which omits the fact that there are limits to when this statement is true or not. (Let me know if i should move this to its own topic/issue.) |
Beta Was this translation helpful? Give feedback.
-
I'm not sure the users would use which shell. For me, I usually use pwsh on windows, sometimes bash on linux. Wondering if we could provide a cross-shell implementation so that the users could use consistent scripts across different shells |
Beta Was this translation helpful? Give feedback.
-
@WeihanLi Exactly. And each user will need to handle their own shell-specific escaping to successfully pass a raw JSON string on the command line. Any working example will necessarily be shell-specific. Your example test is also shell-specific. So yes, it might make sense to create different implementations of
@elgonzo While
|
Beta Was this translation helpful? Give feedback.
-
I'm trying to read raw JSON text as an option value, but no luck finding an effective way
For example:
All the cases failed, is there a simple way to read raw json text
Beta Was this translation helpful? Give feedback.
All reactions