我有一个程序可以执行不同类型的统计分析。我想为每种分析类型定义一个子命令。父命令将成为程序的主要入口点。当我的子命令具有相同名称的选项时,我收到一条错误消息,提示“选项只能指定一次”。问题似乎是我如何调用子命令。在下面的示例中,input1和input2正常工作。当我尝试同时使用两个子命令(input3)时,出现错误消息。
下面的代码演示了该问题。如果输入包含两个子命令(即input3),则会收到错误消息“索引0()上的选项'-id'仅应指定一次”。
如何像在input3中一样同时调用两个子命令?
import picocli.CommandLine;
import java.util.concurrent.Callable;
@CommandLine.Command(name = "myprogram", subcommands = {TestCase.FrequencyCommand.class, TestCase.HistogramCommand.class})
public class TestCase implements Callable<Void> {
public TestCase(){
}
public Void call() {
System.out.println("Main program called");
return null;
}
public static void main(String[] args){
String[] input1 = {"frequency", "-id", "1001", "-table", "ex1"};
String[] input2 = {"histogram", "-id", "1002", "-table", "ex5" };
String[] input3 = {"frequency", "-id", "1001", "-table", "ex1", "histogram", "-id", "1002", "-table", "ex5" };
CommandLine commandLine = new CommandLine(new TestCase());
System.out.println("==Test1==");
commandLine.execute(input1);
System.out.println();
System.out.println("==Test2==");
commandLine.execute(input2);
System.out.println();
System.out.println("==Test3==");
commandLine.execute(input3);
System.out.println();
}
@CommandLine.Command(name = "frequency", description = "Frequency analysis.")
static class FrequencyCommand implements Callable<Void> {
@CommandLine.Option(names = {"-id"}, arity = "1..*", description = "Unique case identifier")
public String id;
@CommandLine.Option(names = "-table", arity = "1..*", description = "Database table")
public String table;
public FrequencyCommand(){
}
public Void call() {
System.out.println("Frequency");
System.out.println("ID = " + id);
System.out.println("Table = " + table);
return null;
}
}
@CommandLine.Command(name = "histogram", description = "Histogram plot.")
static class HistogramCommand implements Callable<Void> {
@CommandLine.Option(names = {"-id"}, arity = "1..*", description = "Unique case identifier")
public String id;
@CommandLine.Option(names = "-table", arity = "1..*", description = "Database table")
public String table;
public HistogramCommand(){
}
public Void call() {
System.out.println("Histogram");
System.out.println("ID = " + id);
System.out.println("Table = " + table);
return null;
}
}
}
我希望看到的输出是:
==测试1 ==
频率
ID = 1001
表格= ex1
== Test2 ==
直方图
ID = 1002
表格= ex5
== Test3 ==
频率
ID = 1001
表格= ex1
直方图
ID = 1002
表格= ex5