Picocli子命令可以具有相同名称的选项

  • 本文关键字:选项 命令 Picocli picocli
  • 更新时间 :
  • 英文 :


我有一个程序,可以执行不同类型的统计分析。我想为每种类型的分析定义一个子命令。父命令将是程序的主要入口点。我收到一条错误消息,即当我的子命令具有具有相同名称的选项时,"只能指定一次选项"。问题似乎是我如何称呼子命令。在下面的示例中,Input1和Input2正常工作。当我尝试同时使用两个子命令时(Input3(,我会收到一个错误消息。

下面的代码显示了问题。如果输入包含两个子命令(即Input3(,则我在index 0((处仅指定一次错误消息" option'-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;
        }
    }
}

我期望看到的输出是:

== test1 ==
频率
id = 1001
table = ex1

== test2 ==
直方图
id = 1002
table = ex5

== test3 ==
频率
id = 1001
表= ex1
直方图
id = 1002
table = ex5

最后一个示例调用两个子命令,即 frequencyhistogram,它们是兄弟姐妹(它们具有相同的父命令(。

与Picocli 4.0.0-Alpha-3:当前Picocli期望子命令是层次结构。

但是,对此的支持在待办事项列表中,请参见GitHub门票#454和#319。

如果您想帮助加快速度,欢迎拉动请求。: - (

最新更新