Java: picocli: 如何在不命名的情况下为命令提供参数?
Java: picocli: How to provide argument to command without naming it?
是否可以在不明确命名 picocli
中的参数的情况下向 @CommandLine.Command 提供参数?
例如,可以调用以下命令:open 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));
}
}
Picocli 为 positional parameters, in addition to the @Option
annotation, which is for named parameters 提供 @Parameters
注释。
如果您对数字使用 @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
中的参数的情况下向 @CommandLine.Command 提供参数?
例如,可以调用以下命令:open 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));
}
}
Picocli 为 positional parameters, in addition to the @Option
annotation, which is for named parameters 提供 @Parameters
注释。
如果您对数字使用 @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);
}
}