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

Problem with arity=2: and argument containing '{}[]:' characters #431

Open
lindenb opened this issue Feb 7, 2018 · 2 comments · May be fixed by #514
Open

Problem with arity=2: and argument containing '{}[]:' characters #431

lindenb opened this issue Feb 7, 2018 · 2 comments · May be fixed by #514

Comments

@lindenb
Copy link
Contributor

lindenb commented Feb 7, 2018

Hi,
Sorry if it's a known bug or if I'm missing someting.

Here is my test file below: Arity is 2.
It bugs when the VALUE contains a json string.

import com.beust.jcommander.*;
import java.util.*;

class Test
{
private static final String KEY="key";
private static final String VALUE="{\"a\":1,\"b\":[],\"c\":null}";

@Parameter(names = "-e",arity=2, description = "test")
private List<String> vals = new ArrayList<>();

private void check(boolean b,String msg) {
  if(!b) throw new RuntimeException(msg);
  }

private void run(final String args[]) {
  new JCommander(this).parse(args);
  check(vals.size()==2,"args size==2 got "+vals.size()+" "+vals);
  check(vals.get(0).equals(KEY),"test k="+KEY);
  check(vals.get(1).equals(VALUE),"test v="+VALUE);
  System.err.println("OK");
}

public static void main(String[] args) {
  args =new String[]{"-e",KEY,VALUE};
  new Test().run(args);
  }
}

when compiled and executed with 1.72 i've got the following error:


Exception in thread "main" java.lang.RuntimeException: args size==2 got 4 [key, {"a":1, "b":[], "c":null}]
	at Test.check(Test.java:13)
	at Test.run(Test.java:18)
	at Test.main(Test.java:26)

if I replace VALUE with :

private static final String VALUE="A B C D E";

the output is

OK

@lindenb
Copy link
Contributor Author

lindenb commented Feb 7, 2018

FYI:
changing pair1 in the test arityString() produces an error:

diff --git a/src/test/java/com/beust/jcommander/JCommanderTest.java b/src/test/java/com/beust/jcommander/JCommanderTest.java
index a9360ea..877238b 100644
--- a/src/test/java/com/beust/jcommander/JCommanderTest.java
+++ b/src/test/java/com/beust/jcommander/JCommanderTest.java
@@ -189,13 +189,14 @@ public class JCommanderTest {
 
     public void arityString() {
         ArgsArityString args = new ArgsArityString();
-        String[] argv = {"-pairs", "pair0", "pair1", "rest"};
+        final String PAIR1="{\"a\":1,\"b\":[],\"c\":null}";
+        String[] argv = {"-pairs", "pair0", PAIR1, "rest"};
         JCommander.newBuilder().addObject(args).build().parse(argv);
 
 
         Assert.assertEquals(args.pairs.size(), 2);
         Assert.assertEquals(args.pairs.get(0), "pair0");
-        Assert.assertEquals(args.pairs.get(1), "pair1");
+        Assert.assertEquals(args.pairs.get(1), PAIR1);
         Assert.assertEquals(args.rest.size(), 1);
         Assert.assertEquals(args.rest.get(0), "rest");
     }

error:

java.lang.AssertionError: expected [2] but found [4]
	at org.testng.Assert.fail(Assert.java:94)
	at org.testng.Assert.failNotEquals(Assert.java:513)
	at org.testng.Assert.assertEqualsImpl(Assert.java:135)
	at org.testng.Assert.assertEquals(Assert.java:116)
	at org.testng.Assert.assertEquals(Assert.java:389)
	at org.testng.Assert.assertEquals(Assert.java:399)
	at com.beust.jcommander.JCommanderTest.arityString(JCommanderTest.java:197)

@Asriter Asriter linked a pull request Apr 25, 2021 that will close this issue
@Asriter
Copy link

Asriter commented Apr 25, 2021

I would like to work on this issue

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

Successfully merging a pull request may close this issue.

2 participants