Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stop removing double quotes when parsing arguments #458

Closed
Mirosta opened this issue Oct 10, 2018 · 5 comments
Closed

Stop removing double quotes when parsing arguments #458

Mirosta opened this issue Oct 10, 2018 · 5 comments

Comments

@Mirosta
Copy link

Mirosta commented Oct 10, 2018

#153 Is not fixed, for example:

@parameter(names = {"-v", "--value"}, description = "JSON value to set the attribute to", required = true)
String jsonValue;

From bash:
java -jar test.jar --value "\"some json string\""

Will result in jsonValue being set to "some json string", which will then fail to parse as it's no longer a valid json string

I'm not sure what the logic is behind that trim() function removing the outer quotes, as that is the shell's job not the program's.

In order to workaround this you have to do (in bash):
java -jar test.jar --value "\"\"some json string\"\""

@boerngen-schmidt
Copy link

Well this is not really a bug, but the way bash works. So the outer quotes are needed, so that bash does not delimit the string using its internal $LFS, which is usally newlines, tabs and spaces.
The escaping () is needed, so that bash does take the quote as a literal and not a special character.

In Java your string value (!) then is "some json string" so when you export this to Json, which seem to need escaping of special chars like the quote, that is something you have to take care of, not jComamnder.

@Mirosta
Copy link
Author

Mirosta commented Nov 22, 2018

I think you've missed the issue, it seems that the way jCommander is written, it is assuming that the quotes will be passed in by the shell.

If I do
echo "hello world"
Then the output is
hello world
But jCommander thinks it would be
"hello world"

Go have a look at the trim function, if you're not sure what I mean; it checks if the start and end of the string are a quote character, and removes then if they are.

So in my example above I would expect that:

java -jar test.jar --value "\"some json string\""

If inside that command you just do
System.out.println(jsonValue);
The output should be:
"some json string"
However it is actually:
some json string

This is as expected:
java -jar test.jar --value "some json string"
some json string

@boerngen-schmidt
Copy link

Yes you are right, I did not get the problem completely. I did some tests and the only Test that seem to fail if you quote the removal of " in trim() is QuotedMainTest which is wrong concerning your argumentation, which I agree to!

@Jangor67
Copy link

Jangor67 commented Nov 13, 2019

Can this issue be closed and/or is the behavior like the testCase below expected?

I upgraded from JCommander 1.72 to 1.78 and encountered a change which looks like it is related to this issue. I have made the following testCase which succeeds with version 1.78 but fails with 1.72:

@Parameters(separators = "=", commandDescription = "Just for testing quote handling.")
public class TestParameterQuoteHandling {

    @Parameter(names = { "--aParameter" }, description = "A String.")
    public String aParameter = null;

    @Parameter(names = { "--aParameterList"}, description = "A String list.")
    public List<String> aParameterList = null;

    @Test
    public void testParameterHavingQuotes() {
        JCommander jc = new JCommander(this);
        jc.parse("--aParameter=\"X\"");
        Assert.assertNotNull(aParameter);
        // as of JCommander 1.74/75
        Assert.assertEquals("Expect \"X\" for JCommander 1.74/75 and more recent", "\"X\"", aParameter);
    }

    @Test
    public void testParameterListHavingQuotes() {
        JCommander jc = new JCommander(this);
        jc.parse("--aParameterList=\"X,Y\"");
        Assert.assertNotNull(aParameterList);
        Assert.assertEquals(2, aParameterList.size());
        Assert.assertEquals("\"X", aParameterList.get(0));
        Assert.assertEquals("Y\"", aParameterList.get(1));
    }
}

Is this the desired behavior? If so is it an idea to document it on

@mkarg
Copy link
Collaborator

mkarg commented Dec 21, 2023

Can this issue be closed and/or is the behavior like the testCase below expected?
Is this the desired behavior? If so is it an idea to document it on

Thank you, Jan! I have added your unit test to the master branch. Indeed, it proofs that no quotes are removed. Hence I will close this issue as it seems to be fixed already. If anybody thinks the problem still exists, feel free to reopen this issue and post a unit test proving your claim. :-)

@mkarg mkarg closed this as completed Dec 21, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants