Java:Picocli:如何提供参数以命令而不命名?
是否可以在 @commandline.command中提供参数,而无需在picocli
中明确命名参数?
例如,可以将以下命令调用为:打开n 1
。但是,我希望能够将命令称为open 1
。
@CommandLine.Command(name = "open",
mixinStandardHelpOptions = true,
version = "1.0",
description = "Open note")
@Getter
@ToString
public class OpenCommand implements Runnable {
@CommandLine.ParentCommand TopLevelCommand parent;
@CommandLine.Option(names = {"number", "n"}, description = "Number of note to open")
private Integer number;
public void run() {
System.out.println(String.format("Number of note that will be opened: " + number));
}
}
Is it possible to provide argument to @CommandLine.Command without explicitly naming the argument in picocli
?
As example the following command can be invoked as: open n 1
. However, I would like to be able to invoke the command as open 1
.
@CommandLine.Command(name = "open",
mixinStandardHelpOptions = true,
version = "1.0",
description = "Open note")
@Getter
@ToString
public class OpenCommand implements Runnable {
@CommandLine.ParentCommand TopLevelCommand parent;
@CommandLine.Option(names = {"number", "n"}, description = "Number of note to open")
private Integer number;
public void run() {
System.out.println(String.format("Number of note that will be opened: " + number));
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
picocli提供@parameters 位置参数除<代码外, > @option 注释,该注释是 new nore noreferrer“> nater参数。
如果您使用
@parameters
对该号码的注释,而不是@option(names =“ n”)
,则最终用户可以将命令称为open 1
。这是一个相当最小的变化,结果代码看起来像这样:
Picocli offers the
@Parameters
annotation for positional parameters, in addition to the@Option
annotation, which is for named parameters.If you use the
@Parameters
annotation for the number, instead of@Option(names = "n")
, then end users can invoke the command asopen 1
.That is a fairly minimal change, the resulting code could look like this: