2

JCommander has a nice feature that it refers to as commands. The example they give looks like this:

git commit --amend -m "Bug fix"

Is it possible to configure JCommander (or any other command line parser), to accept multiple commands? For example:

mycommand drawCircle -colour "red" -radius 5 drawCircle -colour "blue" -radius 8

This is what I've tried.

CommandDrawCircle.java

package omniplot.commandline;

import com.beust.jcommander.Parameter;
import com.beust.jcommander.Parameters;

@Parameters(separators = "=", commandDescription = "Record changes to the repository")
public class CommandDrawCircle {

    @Parameter(names = {"--colour", "-c"})
    private String colour;

    @Parameter(names = {"--radius", "-r"})
    private int radius;
}

CommandMain.java

package omniplot.commandline;

import com.beust.jcommander.Parameter;
import java.util.ArrayList;
import java.util.List;

public class CommandMain {

    @Parameter(names = "draw", description = "Information about what to draw")
    private List<CommandDrawCircle> drawCommands = new ArrayList<>();

}

Main.java

public static void main(String[] args) throws ParseException {
    CommandMain cm = new CommandMain();
    JCommander jc = new JCommander(cm);

    CommandDrawCircle draw = new CommandDrawCircle();
    jc.addCommand("drawCircle", draw);
    String cmd = jc.getParsedCommand();

That last line is where I am stuck. It only returns a single value, not a list of commands. Is there any way of having multiple commands?

Fidel
  • 7,027
  • 11
  • 57
  • 81
  • I'm not familiar with JCommander, but the documentation has a method, `getCommands()` that returns a `Map`. Perhaps that is what you're looking for? – Jonny Henly Jul 25 '16 at 22:43
  • Thanks Jonny, that looks promising however it returns the value I added in the 2nd last line – Fidel Jul 26 '16 at 06:23

0 Answers0