Java:Picocli:如何提供参数以命令而不命名?

发布于 2025-01-27 18:32:20 字数 666 浏览 2 评论 0原文

是否可以在 @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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

余生再见 2025-02-03 18:32:20

picocli提供@parameters 位置参数除<代码外, > @option 注释,该注释是 new nore noreferrer“> nater参数

如果您使用@parameters对该号码的注释,而不是@option(names =“ n”),则最终用户可以将命令称为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.Parameters(description = "Number of notes to open")
    private Integer number;

    public void run() {
        System.out.printf("Number of notes that will be opened: %s%n", number);
    }
}

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 as open 1.

That is a fairly minimal change, the resulting code could look like this:

@CommandLine.Command(name = "open",
    mixinStandardHelpOptions = true,
    version = "1.0",
    description = "Open note")
@Getter
@ToString
public class OpenCommand implements Runnable {
    @CommandLine.ParentCommand TopLevelCommand parent;

    @CommandLine.Parameters(description = "Number of notes to open")
    private Integer number;

    public void run() {
        System.out.printf("Number of notes that will be opened: %s%n", number);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文