In order to debug my application I added a launch configuration (version 0.2.0) of type cppdbg. In it, I specify my command line argument that is to be passed to my program I'm about to debug.
The problem I have is that the argument that is received within the program is not the expected command line argument that I specified in the launch.json file.
In launch.json I specify the command line argument (a string within a string) as:
"args": ["{\"key\":42}"]
That is, I wan't to pass a simple JSON object with its key as a string, which I therefore escape within quotation marks. But what I receive in my program is: "{key:42}", that is, a key without its quotation marks, which is not what I want. I also tried using single quotation marks: "args": ["{'key':42}"] but the end result was the same.
Of course, passing the same argument directly from the command line (not using vscode/debug) as:
./myapp "{\"key\":42}"
works as expected, i.e. within myapp I receive my key as a string within the string: "{"key":42}".
I have tried to find information how I otherwise should specify my string within a string as command line arguments in launch.json but have not found anything useful, I therefore file this as a bug.
Does "args": ["{\\\"key\\\":42}"] work? If not, you should ask the vscode team since they own processing the launch.json file.
Sorry, for double-posting. I made a mistake in my previous response and I have edited it. There should be 3 backslashes in the string.
What @bobbrow says is correct. You need to escape the escape character and then escape the quote so that the escaped quote is passed to the commandline.
Yes it works with three backslashes. Thanks!
I guess there are some intermediate layers that absorb the extra backslashes. That is of course ok, but that is not obvious and should IMHO be clarified in documentation.
@bandzaw I created a PR for the doc change. Can you see if that is clear enough for you?
Most excellent @pieandcakes!
This is stil not working when using {} brackets in a unix shell.
@vbaurea72 can you give me an example of what you are putting in your launch.json and what you are expecting to get to your application?
I've figured out the problem. If you use an expression like this (with no spaces) (pyhton example):
args:["{\"something\":10}"]
Because latest vscode is automatically escaping double quotes (no needs for extra backslashes anymore) the final commandline will be:
python ... {\"something\":10}
but because "{" has a special meaning in linux shell the actual string that will be passed as argument will be "something":10 which is wrong.
But there's a workaround: just put a space somewhere in the argument (e.g.: before 10 in this example):
args:["{\"something\": 10}"]
Then, because of the space, the argument will be wrapped in quotes and the command line will be:
python ... "{\"something\": 10}"
Now, this is fine, because the "{" bracket will no more have an extra meaning when wrapped in double quotes.
So the patch would be to always wrap args in double quotes in linux/unix environment as it will be always be correct as long as quotes are already automatically backescaped
BTW: IMHO the best choice for this kind of thing would be to avoid any automatic backspacing, any hidden processing, and leave the user the choice to do whatever he wants so that he always knows what's going on without having to argue what automatic processing is done.
So I'd rather prefer if something like this was working:
args:["\"{\\\"something\\\":10}\""]
despite being more complex to write it would be easier to understand (and actually this was my first choice). Instead, I had to figure out that \ and " where automatically back escaped and find a way to deal with that losing a lot of time. And finally, I luckily found out that workaround.
I'm trying to pass a url as an argument to python.
args: ["https://www.google.com/search?hl=en&q=python"]
But the ampersand character "&" has a special meaning, and so I need the argument to be surrounded by double quotes.
args: ["\"https://www.google.com/search?hl=en&q=python\""] Doesn't work, and is interpreted as python foo.py \"https://www.google.com/search?hl=en&q=python\" for some reason.
Adding a space does make vscode put quotes around the url:
args: ["https://www.google.com/search?hl=en&q=python "]
is interpreted as
python foo.py "https://www.google.com/search?hl=en&q=python "
But I don't want a space in my argument! For JSON, this would be fine, as the space isn't important, but in a url it is important, so this workaround won't work for me.
Most helpful comment
BTW: IMHO the best choice for this kind of thing would be to avoid any automatic backspacing, any hidden processing, and leave the user the choice to do whatever he wants so that he always knows what's going on without having to argue what automatic processing is done.
So I'd rather prefer if something like this was working:
despite being more complex to write it would be easier to understand (and actually this was my first choice). Instead, I had to figure out that
\and"where automatically back escaped and find a way to deal with that losing a lot of time. And finally, I luckily found out that workaround.