Escaped quotes in symbol parameter values lost or cause errors
When I try to pass quotation marks to a parameter from the command line, I either get an error or the produced content will not contain quotation marks.
Full details can be found in my sample repo notes, but here are some examples.
dotnet new repro-quotes-spaces --someparameter="something needing `"escaped quotes`" in it."
Results in this error:
Invalid input switch:
quotes in it.
If I remove the space within the quoted sub-string, there is no error, but the content it produces doesn't have any quotation marks either.
dotnet new repro-quotes-spaces --someparameter="something needing `"escapedquotes`" (without spaces) in it."
Results in this text missing the expected quotes:
something needing escapedquotes (without spaces) in it.
Environment
- Windows 10 Pro 1803 and macOS v10.13.6 (High Sierra)
- .NET Core SDK: v2.1.500 (also tested on v2.1.302)
- PowerShell Core: v6.1.0
Sample repro
Here is a repo containing a sample and my notes so far (Notes direct link):
https://github.com/patridge/repro-DotnetNewTemplateParameterQuotes
I can file it in a separate issue since it's a different OS, but there is a different issue for \" on macOS with .NET Core v2.1.500. This case works, but produces output that is missing multiple other characters, very similar to #1704.
dotnet new repro-quotes-spaces --someparameter="something needing \"escaped quotes\" in it."
Unexpected substitution:
omething needing "escaped quotes" in it
Just confirmed the same errors and unexpected outputs on macOS running the same .NET Core and PowerShell Core versions. Editing the initial issue to include that.
The issue seems totally different in Bash, though, as mentioned in the prior comment.
Hi @patridge, This issue will be resolved once we switch to the new command line parser which is planned for .NET 6. You can track the work here #2191.
@GangWang01 could you please check if it is fixed? It should be as we already moved to new parser. Thank you
@vlada-shubina, @GangWang01 Not sure if I should be on something newer, but I just tested this on dotnet v6.0.302 and it is no longer throwing an error, but it also doesn't work correctly with the escaped characters. The resulting parameter value is never the full, escaped string, and the resulting file only part of the parameter value.
- For PowerShell-escaped backtick characters
-
dotnet new repro-quotes-spaces --someparameter="something needing"escaped quotes" in it." - Resulting variable value:
something needing escaped
-
- For backslash-escaped characters
-
dotnet new repro-quotes-spaces --someparameter="something needing \"escaped quotes\" in it." - Resulting variable value:
something needing \escaped
-
- For double-quote characters
-
dotnet new repro-quotes-spaces --someparameter="something needing ""escaped quotes"" in it." - Resulting variable value:
something needing escaped
-
- For quotes within apostrophes
-
dotnet new repro-quotes-spaces --someparameter='something needing "escaped quotes" in it.' - Resulting variable value:
something needing escaped
-
@patridge the fix is potentially available in .NET 7, unfortunately it won't be backported to .NET 6.
@patridge @vlada-shubina It is not reproduced with .NET 7.0.100-preview.6.22352.1.
With Command Prompt on Windows, \ or " can escape quotation mark well. For PowerShell it should be \ or " plus ` together to escape quotation mark, like \`" or `"`" will be escaped as quotation mark.
For other escape characters, they could not escape correctly, but result in being parsed to be multiple arguments passed to the application's entry Main method, then cause the error "Invalid option".
Here are the details using different escape characters with/without space inside.
Command prompt
| Input | Escape as Expected? | Received Parsed Argument |
|---|---|---|
--someparameter="something needing `"escaped quotes`" in it." |
No | --someparameter=something needing `escapedquotes` in it. |
--someparameter="something needing `"escapedquotes`" (without spaces) in it." |
No | --someparameter=something needing `escapedquotes` (without spaces) in it. |
--someparameter="something needing \"escaped quotes\" in it." |
Yes | --someparameter=something needing "escaped quotes" in it. |
--someparameter="something needing \"escapedquotes\" (without spaces) in it." |
Yes | --someparameter=something needing "escapedquotes" (without spaces) in it. |
--someparameter="something needing ""escaped quotes"" in it." |
Yes | --someparameter=something needing "escaped quotes" in it. |
--someparameter="something needing ""escapedquotes"" (without spaces) in it." |
Yes | --someparameter=something needing "escapedquotes" (without spaces) in it. |
--someparameter='something needing "escaped quotes" in it.' |
No | --someparameter='something needing escaped quotes in it.' |
PowerShell 7
| Input | Escape as Expected? | Received Parsed Argument |
|---|---|---|
--someparameter="something needing `"escaped quotes`" in it." |
No | --someparameter=something needing escapedquotes in it. |
--someparameter="something needing `"escapedquotes`" (without spaces) in it." |
No | --someparameter=something needing escapedquotes (without spaces) in it. |
--someparameter="something needing \"escaped quotes\" in it." |
No | --someparameter=something needing \escaped quotes\ in it. |
--someparameter="something needing \"escapedquotes\" (without spaces) in it." |
No | --someparameter=something needing \escapedquotes\ (without spaces) in it. |
--someparameter="something needing ""escaped quotes"" in it." |
No | --someparameter=something needing escaped quotes in it. |
--someparameter="something needing ""escapedquotes"" (without spaces) in it." |
No | --someparameter=something needing escapedquotes (without spaces) in it. |
--someparameter='something needing "escaped quotes" in it.' |
No | --someparameter=something needing escaped quotes in it. |
--someparameter="something needing \`"escaped quotes\`" in it." |
Yes | --someparameter=something needing "escaped quotes" in it. |
--someparameter="something needing \`"escapedquotes\`" (without spaces) in it." |
Yes | --someparameter=something needing "escapedquotes" (without spaces) in it. |
--someparameter="something needing `"`"escaped quotes`"`" in it." |
Yes | --someparameter=something needing "escaped quotes" in it. |
--someparameter="something needing `"`"escapedquotes`"`" (without spaces) in it." |
Yes | --someparameter=something needing "escapedquotes" (without spaces) in it. |
Closing, this has been fixed in .NET 7. Correct syntax follows:
Command prompt
--someparameter="something needing \"escaped quotes\" in it."
--someparameter="something needing ""escaped quotes"" in it."
PowerShell 7
--someparameter="something needing \`"escaped quotes\`" in it."
--someparameter="something needing `"`"escaped quotes`"`" in it."