apache commons cli: take the next Option as an argument if not argument is included

458 views Asked by At

I would like to pass command line arguments. However, in this case if "-a" is followed by "-b", I would like the parser to accept "-b" as the argument, and not include -b as an option. Is there an easy way to do that? This is what I have.

import org.apache.commons.cli.*;
Option remove = new Option("a", true, "a option");
        remove.setRequired(false);
        remove.setOptionalArg(false);
        options.addOption(remove);

Option trim = new Option("b", false, "b option");
        trim.setRequired(false);
        trim.setOptionalArg(true);
        options.addOption(trim);

CommandLineParser parser = new DefaultParser();
CommandLine cmd;

try {
   cmd = parser.parse(options, args);
} catch (ParseException ex) {
   setErrorMessage();
   return;
}
1

There are 1 answers

0
kaylee On

I ended up fixing this in a rather hacky way. I would just check what followed "a" and not add the "b" option if it was "b".