如何指定默认子命令?
How do I specify default subcommand?
有没有办法在用户未提供子命令时打印帮助消息?
以下不起作用,因为它运行的可调用对象没有实现。
@Command(
name = "tool",
mixinStandardHelpOptions = true,
subcommands = [ListPlugins::class, RunJob::class, CommandLine.HelpCommand::class])
class Main : Callable<Int> {
override fun call(): Int {
// CommandLine.HelpCommand().run()
return 0
}
}
重新表述问题:当用户未指定子命令时如何显示帮助?
从 picoli 4.3 版开始,您可以简单地不在顶级命令上实现Callable
;这使得用户必须指定一个子命令。 (这假设您使用 CommandLine.execute
来解析命令行和 运行 业务逻辑。)
如果未指定子命令,则会显示“缺少必需的子命令”错误消息,然后显示使用帮助消息。
有关详细信息,请参阅:https://picocli.info/#_required_subcommands。
符合您的要求吗?
是的,例如,您可以在没有命令行参数的情况下进行假调用。 (而不是 args-> --help)
然后根据说明使用参数。
比如Java中的代码:
CommandLine helpCL = new CommandLine(new Main());
helpCL.execute(new String[]{"--help"});
return new CommandLine(new Main()).execute(args);
有没有办法在用户未提供子命令时打印帮助消息?
以下不起作用,因为它运行的可调用对象没有实现。
@Command(
name = "tool",
mixinStandardHelpOptions = true,
subcommands = [ListPlugins::class, RunJob::class, CommandLine.HelpCommand::class])
class Main : Callable<Int> {
override fun call(): Int {
// CommandLine.HelpCommand().run()
return 0
}
}
重新表述问题:当用户未指定子命令时如何显示帮助?
从 picoli 4.3 版开始,您可以简单地不在顶级命令上实现Callable
;这使得用户必须指定一个子命令。 (这假设您使用 CommandLine.execute
来解析命令行和 运行 业务逻辑。)
如果未指定子命令,则会显示“缺少必需的子命令”错误消息,然后显示使用帮助消息。
有关详细信息,请参阅:https://picocli.info/#_required_subcommands。
符合您的要求吗?
是的,例如,您可以在没有命令行参数的情况下进行假调用。 (而不是 args-> --help) 然后根据说明使用参数。 比如Java中的代码:
CommandLine helpCL = new CommandLine(new Main());
helpCL.execute(new String[]{"--help"});
return new CommandLine(new Main()).execute(args);