You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This is quite likely an effect of the startup code that transforms the given command line into an args string array that's being passed to the main method of your .NET program. Basically, said startup code expects the double-quotes escaped with a backslash character for them to be treated as ordinary characters.
Using the cmd.exe shell, doing this would be quite simple: cmd.exe>myapp --param1 {\"KEY\":{}}
With Powershell, it gets a little bit more complicated. Note the single quotes; without them Powershell itself would strip the double-quotes from the command line: PS>.\myapp --param1 '{\"KEY\":{}}'
If you are using another shell, you need to verify whether that shell treats backspaces and/or double-quotes in a special way. If so, you will have to apply that shell's syntax rules in a manner so that both the backspaces as well as the double-quotes are being passed to the called program.
I happen to be using Windows Command Prompt. In this instance I am not launching app with command line args passed in. Instead I have infinite loop to Console.ReadLine() and pass to myRootCommand.Invoke() so every time user enters/pastes the args.
(UPDATE: It seems at least with the current master branch of the library code you would be out of luck regarding double quotes. When passing a single string of a commandline to the Parse method, the library uses its own SplitCommandLine method. Unfortunately, its current implementation does not seem to allow for escaping of double quotes and simply strips them away :-( )
There are packages of newer versions of System.CommandLine on the daily builds nuget feed (see the project's readme.md). The latest as of writing this is 2.0.0-beta4.23407.1. But updating to this version wouldn't likely help you (not to mention the work it would require to adapt your existing code to the new API surface of the newer library versions.
Even with the current master branch of the library, when passing a single string of a commandline to the Parse method, the library uses its own SplitCommandLine method. Unfortunately, its current implementation does not seem to allow for escaping of double quotes and simply strips them away -- just as the CommandLineStringSplitter.Split method did in 2.0.0-beta4.22272.1 :-(
Not sure what the future holds regarding System.CommandLine's own SplitCommandLine method, as i am not involved with the project. But you might want to keep this discussion #1758 on your radar, even though it doesn't really help you solve your problem now. (Basically, if you need to handle and retain double-quotes, currently you would have to do the splitting of the user input string by yourself and then passing the resulting string array to the Parse or Invoke method.)
I just hit this myself - can't see how to pass JSON as an argument. SplitCommandLine seems to not have a way to allow for escaped quotes within arguments and it will remove all the quotes it finds.
Activity
KalleOlaviNiemitalo commentedon Oct 4, 2023
Which operating system and shell is this?
elgonzo commentedon Oct 4, 2023
This is quite likely an effect of the startup code that transforms the given command line into an
argsstring array that's being passed to the main method of your .NET program. Basically, said startup code expects the double-quotes escaped with a backslash character for them to be treated as ordinary characters.Using the cmd.exe shell, doing this would be quite simple:
cmd.exe>myapp --param1 {\"KEY\":{}}With Powershell, it gets a little bit more complicated. Note the single quotes; without them Powershell itself would strip the double-quotes from the command line:
PS>.\myapp --param1 '{\"KEY\":{}}'If you are using another shell, you need to verify whether that shell treats backspaces and/or double-quotes in a special way. If so, you will have to apply that shell's syntax rules in a manner so that both the backspaces as well as the double-quotes are being passed to the called program.
KalleOlaviNiemitalo commentedon Oct 4, 2023
@lucasmaj doesn't appear to have been using PowerShell; it would have parsed
{"KEY":{}}as a script block and Base64 encoded the content. (PowerShell/PowerShell#4973, PowerShell/PowerShell#10842)lucasmaj commentedon Oct 4, 2023
I happen to be using Windows Command Prompt. In this instance I am not launching app with command line args passed in. Instead I have infinite loop to Console.ReadLine() and pass to myRootCommand.Invoke() so every time user enters/pastes the args.
And thank you for prompt responses.
elgonzo commentedon Oct 4, 2023
Which version of the library are you using?
(UPDATE: It seems at least with the current master branch of the library code you would be out of luck regarding double quotes. When passing a single string of a commandline to the Parse method, the library uses its own SplitCommandLine method. Unfortunately, its current implementation does not seem to allow for escaping of double quotes and simply strips them away :-( )
lucasmaj commentedon Oct 4, 2023
2.0.0-beta4.22272.1 which looks like is latest
elgonzo commentedon Oct 4, 2023
There are packages of newer versions of System.CommandLine on the daily builds nuget feed (see the project's readme.md). The latest as of writing this is 2.0.0-beta4.23407.1. But updating to this version wouldn't likely help you (not to mention the work it would require to adapt your existing code to the new API surface of the newer library versions.
Even with the current master branch of the library, when passing a single string of a commandline to the Parse method, the library uses its own SplitCommandLine method. Unfortunately, its current implementation does not seem to allow for escaping of double quotes and simply strips them away -- just as the CommandLineStringSplitter.Split method did in 2.0.0-beta4.22272.1 :-(
Not sure what the future holds regarding System.CommandLine's own SplitCommandLine method, as i am not involved with the project. But you might want to keep this discussion #1758 on your radar, even though it doesn't really help you solve your problem now. (Basically, if you need to handle and retain double-quotes, currently you would have to do the splitting of the user input string by yourself and then passing the resulting string array to the Parse or Invoke method.)
Better error messages and fix double quote problem
ericstj commentedon Nov 20, 2025
I just hit this myself - can't see how to pass JSON as an argument. SplitCommandLine seems to not have a way to allow for escaped quotes within arguments and it will remove all the quotes it finds.