JCommander 有一个很好的特性,它被称为命令。他们给出的示例如下所示:
git commit --amend -m "Bug fix"
是否可以配置 JCommander(或任何其他命令行解析器)以接受多个命令?例如:
mycommand drawCircle -colour "red" -radius 5 drawCircle -colour "blue" -radius 8
这是我尝试过的。
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<>();
}
主.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();
最后一行是我卡住的地方。它只返回一个值,而不是命令列表。有没有办法拥有多个命令?