Escaping in Visual Studio debugging command arguments - c++

I'm trying to pass a command-line argument < (single angle bracket, without quotes) to a C++ program during a debugging session in Visual Studio. As described in Project settings for a C++ debug configuration, < has special meaning as a redirection operator, but it does not specify how to escape it. I've tried using "<" and ^<, but none of them do what I want. Invoking the command from command line with the "<" argument works as expected.
So it looks like debugging command arguments are preprocessed first. Is there any way to escape a character from this preprocessing?

Seems like passing "<" works if "Start debugging" is used. I was using "Start Without Debugging", so the problem can be reproduced by passing "<" to an empty console application and invoking "Start Without Debugging".
"Start Without Debugging" seems to actually invoke cmd.exe, so command arguments are subject to command line interpretation rules. The question then turns out to be really how to escape < in a command line. < outside double quotes can be escaped by using ^<, but I still have no idea how to escape < inside double quotes.
I must say it's somewhat surprising that interpretation of debugging command arguments is subject to different rules depending on how the program is started.

Related

Detecting semicolon as command line argument in linux

I am trying to run a C++ application where I am passing some command line arguments to it as follows:
./startServer -ip 10.78.242.4 tcpip{ldap=no;port=2435}
The application is getting crashed because it is not able to get the correct port. Searching over the web, I found that ";" is treated an end of command character (Semicolon on command line in linux) so everything after that is getting ignored. I also understand the putting it inside the quotes will work fine. However, I do not want to force this restriction of putting the arguments in the quotes on the users. So, I want to know is there a way I can process the ";" character with the argv array?
The semicolon separates two commands so your command line is equivalent to
./startServer -ip 10.78.242.4 tcpip{ldap=no
port=2435}
Your application will never know anything about either the semi colon or the second command, these will be completely handled by the shell. You need to escape the colon with a back slash or enclose it in quotes. Other characters which may cause similar issues include: $,\-#`'":*?()&|
Complex strings are much easier to pass either from a file or through stdin.
You need to quote not only the ; but in the general case also the { and }:
./startServer -ip 10.78.242.4 'tcpip{ldap=no;port=2435}'
If your users are required to type in that complicated last argument, then they can also be made to quote it.

Pass \ in command line arguments of C++

I am working on a C++ program where one of the command line arguments needs to be a passed a regex. For example: abc.exe --option ab\[0\]
When I access the option value from inside the program, it becomes ab\\[0\\] which becomes a different regex.
Inside the program when I try to replace \\[ with \[ using boost::replace_all, the result is [ which also is not the intended output for me.
So, any suggestions on how to pass and retain \[ this while passing it through command line arguments
You can quote the parameter:
abc.exe --option "ab\[0\]"
Or use the shell escape sequence:
abc.exe --option ab\\[0\\]
Did you try these?
It was a problem with how visual studio displays the symbol. When I looked on the ASCII code of the symbol, it was alright. Thanks #ScottK for helping me to debug this through your comments

Executing cmd commands from python program

from subprocess import *
s=Popen(['C:\Python27\Scripts\pyssim',"'C:\Users\P\Desktop\1.png'",'C:\Users\P\Desktop\2.png'],stderr=PIPE,stdout=PIPE,shell=True)
out,err=s.communicate()
print out
The python program above executes successfully but it shows no output.
Nothing is printed on the shell.
While running command on cmd it gives output "1".
Your command is failing because the parameters being passed to it are not what you think they are; keep in mind that backslashes are normally treated as the start of escape sequences in Python string literals. Specifically, the \1 and \2 are being treated as octal character escapes, rather than digits. If you looked at the contents of err, you would probably find something like a file not found error. Some possible solutions:
Double all of the backslashes, to escape them.
Put an 'r' in front of each string literal, to make them 'raw strings' that don't specially interpret backslashes.
Not actually applicable in this case, but you can often just use forward slashes instead - most of Windows will happily accept them instead of backslashes, the one exception being the command line (which is what you're actually invoking here).

C++ on Windows: executable path with whitespace in system() call

I am trying to execute a file with parameters using the "system()" function in C++ on Windows, and it works as long as there are no whitespaces in the filename. For parameters, putting double quotes around the string works, but when I try the same with the executable itself, I get the following error:
"the filename,directory name, or volume label syntax is incorrect"
Does anyone know how to handle this correctly?
Use a string like this:
cmd /S /C "your entire command line string"
See: How do I deal with quote characters when using cmd.exe
It should work, look for the problem elsewhere.
Perhaps something in your flow is removing the whitespace or the double quotes from the string.

Devenv.exe with /I and whitespacess

I've tried to compile an application with Directx. But this causes an PRJ0030 error for $().
How can I escape critical characters like (,) or blanks. Refering to the cmd I've used ^ but it does not help.
AdditionalLibraryDirectories=""$(DIRECTX_ROOT)\Lib\x86""
&quot should not be there. $(DIRECTX_ROOT) requires the macro to be set in a project property sheet. You are better off spelling it out:
AdditionalLibraryDirectories="c:\blah\dx9\Lib\x86"
cmd.exe unfortunately isn't a proper shell like bash, and parsing the command line is up to each individual program. I can't speak for devenv.exe but a common convention is to surround troublesome strings with double quotes (").