2

我有一个执行不同类型统计分析的程序。我想为每种类型的分析定义一个子命令。父命令将是程序的主要入口点。当我的子命令具有相同名称的选项时,我收到一条错误消息,提示“选项只能指定一次”。问题似乎是我如何调用子命令。在下面的示例中,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;
        }
    }

}

我期望看到的输出是:

==Test1==
频率
ID = 1001
表 = ex1

==Test2==
直方图
ID = 1002
表 = ex5

==Test3==
频率
ID = 1001
表 = ex1
直方图
ID = 1002
表 = ex5

4

1 回答 1

1

最后一个示例调用两个子命令frequencyhistogram,它们是同级的(它们具有相同的父命令)。

从 picocli 4.0.0-alpha-3 开始尚不支持此功能:当前 picocli 期望子命令是层次结构。

但是,对此的支持在待办事项列表中,请参阅 GitHub 票证#454#319

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

于 2019-05-16T01:11:34.727 回答