-
Notifications
You must be signed in to change notification settings - Fork 335
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
Comments
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. In Java your string value (!) then is |
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 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:
If inside that command you just do This is as expected: |
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! |
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 |
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. :-) |
#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\"\""
The text was updated successfully, but these errors were encountered: