4

I am using JCommander for command line parameters parsing. I would like to add parameter dependency, but from the JCommander documentation, I am not able to find out if it is even supported. Has anyone tried it ? For example, java -jar Some.jar -a Foo -b Hola So, option -b shall only accompany with option -a.

This is supported in args4j. However, I can't use args4j because it does not support multiple values as JCommander.

Thanks

assylias
  • 321,522
  • 82
  • 660
  • 783
Arjun Patel
  • 345
  • 6
  • 22

2 Answers2

0

Yes, you can use args4j, it does support multiple values.

JCommander:

@Parameter(names={"--length", "-l"})
int length;

Args4j:

@Option(name = "-l", aliases = { "--length" })
int length;

About validation and dependency: You can do this manually, of course. It's not too much of programming. Just ignore option b if a is not given either, or throw an exception if a is non-null but b is null.

Benjamin Marwell
  • 1,173
  • 1
  • 13
  • 36
0

I had exactly the same problem but it looks like that args4j has added support for multiple values:

import org.kohsuke.args4j.spi.StringArrayOptionHandler;

@Option(name = "-p", handler = StringArrayOptionHandler.class, required = true)
private List<String> list;

which should allow

-p arg1 arg2 ...
eztam
  • 3,443
  • 7
  • 36
  • 54