George Kouzmov George Kouzmov - 4 months ago 11x
Java Question

Args4j list arguments that contain space not handled

I am dealing with a strange args4j bug. I am handling list command line arguments

aaa.jar -list special_date=2015-05-05 "special_name=bob the builder"

This is handled with StringArrayOptionHandler

. And when I retreive the arguments of the list what i get is

[special_date=2015-05-05, special_name=bob, the, builder]

Java docs say that this should work

java -jar aaa.jar -s banan hruska jablko
java -jar aaa.jar -s banan "hruska jablko"
java -jar aaa.jar -s "banan hruska jablko"
java -jar aaa.jar -s banan hruska jablko -l 4 -r
java -jar aaa.jar -t 222 -s banan hruska jablko -r

There is no bug when handling a single argument with quotes.

aaa.jar -a "bob the builder"

Works perfectly fine

Thank you


The implementation of StringArrayOptionHandler is fairly straightforward, you could adjust it for your own purposes easily enough. Something like this should work:

public class WellBehavedStringArrayOptionHandler extends OptionHandler<String> {
  public WellBehavedStringArrayOptionHandler(
      CmdLineParser parser, OptionDef option, Setter<String> setter) {
    super(parser, option, setter);

  public String getDefaultMetaVariable() {
    return Messages.DEFAULT_META_STRING_ARRAY_OPTION_HANDLER.format();            

  public int parseArguments(Parameters params) throws CmdLineException {
    int counter;
    for (counter = 0; counter < params.size(); counter++) {
      String param = params.getParameter(counter);
      if(param.startsWith("-")) {
    return counter;