サブコマンドを含むコマンドがあります。私のアプリケーションでは、ユーザーがサブコマンドを指定することを必須にしたいと考えています。どうすればいいですか?
1397 次
1 に答える
2
更新: これは picocli マニュアルに記載されています: https://picocli.info/#_required_subcommands
picocli 4.3 より前では、これを達成する方法は、ParameterException
サブコマンドなしで最上位コマンドが呼び出された場合にエラーを表示するかスローすることでした。
例えば:
@Command(name = "top", subcommands = {Sub1.class, Sub2.class},
synopsisSubcommandLabel = "COMMAND")
class TopCommand implements Runnable {
@Spec CommandSpec spec;
public void run() {
throw new ParameterException(spec.commandLine(), "Missing required subcommand");
}
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");
}
}
picocli 4.3 から、最上位のコマンドをorを実装しない ようにすることで、これをより簡単に実現できます。Runnable
Callable
Runnable
コマンドにサブコマンドがあってもorが実装されていない場合Callable
、picocli はサブコマンドを必須にします。
例えば:
@Command(name = "top", subcommands = {Sub1.class, Sub2.class},
synopsisSubcommandLabel = "COMMAND")
class TopCommand {
public static void main(String[] args) {
CommandLine.run(new TopCommand(), args);
}
}
于 2018-10-29T10:52:05.870 に答える