Picocli:如何使子命令成为必需

时间:2018-10-29 10:52:06

标签: command-line-parsing subcommand picocli

我有一个带有子命令的命令。在我的应用程序中,我希望用户必须指定一个子命令。我该怎么办?

(另请参见https://github.com/remkop/picocli/issues/529

1 个答案:

答案 0 :(得分:1)

一种实现此目的的方法是,如果在没有子命令的情况下调用顶层命令,则会显示错误。

例如:

@Command(name = "top", subcommands = {Sub1.class, Sub2.class})
class TopCommand implements Runnable {

    public void run() {
        System.err.println("Please invoke a subcommand");
        new CommandLine(this).usage();
    }

    public static void main(String[] args) {
        CommandLine.run(new TopCommand(), args);
    }
}

@Command(name = "sub1)
class Sub1 implements Runnable {
    public void run() {
        System.out.println("All good, executing Sub1");
    }
}

@Command(name = "sub2)
class Sub2 implements Runnable {
    public void run() {
        System.out.println("All good, executing Sub2");
    }
}