In some legacy code I'm porting a diy-command-line parser to Apache CommonsCli.
I can't break previously allowed+documented Options, and one of the Options is giving me trouble:
The Option has either one or two args, and can be specified as many times as desired.
Option: [-option arg1 [arg2]]+
I want the result as String[][] as following:
cli -option a b -option c should result in [ [a, b], [c,] ]
and
cli -option a -option b c should result in [ [a,], [b, c] ]
My code looks something like this:
final CommandLine cmd;
final Options options = new Options()
.addOption(Option.builder("option").hasArg().hasArgs().build());
cmd = new DefaultParser().parse(options, args);
thatOption = cmd.getOptionValues("option");
But the parser spits out [a, b, c]
(I looked at cmd.getOptionProperties, and that might be a way to bodge it.)
Is there a way or do I need to subclass DefaultParser?
I found a way that doesn't feel very elegant, but I'll take it unless someone comes around with a better one:
Loop over
Option opt : cmd.getOptions()and assemble the String[][] from theopt.getValues() String[]s