Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Eclipse & quotes in command line arguments?

Tags:

c++

eclipse

I have some C++ code that expects it's command line to look like this:

/path/to/exe -p:parameterName="[/path/to/a/file,/path/to/another/file]"

including the quotation marks. They can be single or double quotes, but they must be there. In Eclipse if I set up the command line arguments (Debug configurations/Arguments) and enter the command line option above (minus the /path/to/exe) Eclipse eats the quotes. Since I'm running this on Linux the square brackets give the shell grief, and it never even makes it into my code.

If I set up the command line args thus:

"this is quoted"

argv[1] looks like this:

this is quoted

i.e. without the quotes. If I set up the command line thus:

\"this is quoted\"

I get:

argv[1]: "this
argv[2]: is
argv[3]: quoted"

If I try to put the square brackets in it goes back to giving the shell grief, even if I try to escape them:

\"\[this is quoted\]\"

How do I tell Eclipse to take my command line arguments exactly as I've entered them?

Thanks

like image 652
Al Dunstan Avatar asked Nov 27 '12 22:11

Al Dunstan


People also ask

What is called Eclipse?

An eclipse occurs when one heavenly body such as a moon or planet moves into the shadow of another heavenly body. Let's learn about the two types of eclipses on Earth. What Is a Lunar Eclipse? The Moon moves in an orbit around Earth. At the same time, Earth orbits the Sun.

What is Eclipse best for?

Eclipse is a widely used IDE primarily for Java development. Eclipse is used for C and C++ development as well as PHP among other programming languages.

Why do we use Eclipse?

Eclipse is a free, Java-based development platform known for its plugins that allow developers to develop and test code written in other programming languages.


2 Answers

I fixed this, so now there are clear rules for providing arguments. You can read about this in detail here:

https://bugs.eclipse.org/bugs/show_bug.cgi?id=474648

The main principles are that Run and Debug have to behave the same, arguments are primarily what is received by the program and not bash, and the rules for how to provide arguments must be simple and intuitive. It is impossible to avoid rules altogether as when using a single string to convey where each argument ends, some special marking is inevitable.

So, the arguments work in the following way:

  • Any character following a backslash is treated literally and loses special meaning.

  • Any character after a quote and before the next matching quote (or EOF) is treated literally and loses special meaning (both single and double).

  • White space (unless escaped or within quotes) is used as the argument delimiter.

Previous behaviour was pretty much undefined as something like `date` would be subbed by bash and yet behaviour was not always identical to that of bash. This may ruin some users hacky configs but now rewriting is trivial, whilst before the right string had to be found through trial and error with various combinations of escaping quoting, "do I need one, two or four backslashes?".

like image 92
rudolfovic Avatar answered Oct 26 '22 23:10

rudolfovic


I think I've found a solution - at least this has worked several times in a row now. In Eclipse's Arguments tab, in the "Program arguments" field enter the command line parameter as follows:

-p:parameterName="'[foo,bar]'"

This turns into:

-p:parameterName='[foo,bar]'

in gdb's "set args" command. I wind up with single quotes around the square-bracket expression but that's fine for my application. I don't know why this works (I wish I did) or why the other ways of escaping text failed.

The order of the double & single quotes matters. If you try putting the single quotes on the outside it gets turned into

set args -p:parameterName=\"[foo,bar]\"

which fails. Not sure what I'd do if double quotes were required.

like image 44
Al Dunstan Avatar answered Oct 27 '22 00:10

Al Dunstan