【问题标题】:Can picocli subcommands have options with the same name?picocli 子命令可以有同名的选项吗?
【发布时间】:2019-05-15 11:45:18
【问题描述】:

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

==测试2==
直方图
ID = 1002
表 = ex5

==测试3==
频率
ID = 1001
表 = ex1
直方图
ID = 1002
表 = ex5

【问题讨论】:

    标签: picocli


    【解决方案1】:

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

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

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

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

    【讨论】:

      猜你喜欢
      • 2019-10-24
      • 1970-01-01
      • 2020-07-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多