Picocli:是否可以在名称中添加空格来定义选项



我在谷歌上搜索了一下,还搜索了StackOverflow,当然还有Picocli文档,但没有找到任何解决方案。

我所在的公司在批处理程序中使用了一种特殊的命令行参数格式:

-VAR ARGUMENT1=VALUE -VAR ARGUMENT2=VALUE2 -VAR BOOLEANARG=FALSE

(不要问我为什么使用这种格式,我已经对它提出了质疑,但没有得到正确的答案。(现在我想使用Picocli进行命令行解析。然而,我无法使用我们使用的参数格式,因为空格使Picocli认为这是两个独立的参数,因此它不会将它们识别为我定义的参数。

这显然行不通:

@CommandLine.Option( names = { "-VAR BOOLEANARG" } )
boolean booleanarg = true;

用-VARBOOLEANARG=FALSE调用程序不会有任何效果。

有什么方法可以自定义定义那些包含空格的特殊选项名称吗?或者我该怎么做?我也不允许将多个参数作为参数折叠成一个-VAR选项。

非常感谢您的帮助。谢谢并致以最良好的问候,Rosa

解决方案1:映射选项

最简单的解决方案是将-VAR设置为Map选项。这可能看起来像这样:

@Command(separator = " ")
class Simple implements Runnable {
enum MyOption {ARGUMENT1, OTHERARG, BOOLEANARG}
@Option(names = "-VAR",
description = "Variable options. Valid keys: ${COMPLETION-CANDIDATES}.")
Map<MyOption, String> options;
@Override
public void run() {
// business logic here
}
public static void main(String[] args) {
new CommandLine(new Simple()).execute(args);
}
}

此示例的用法帮助如下所示:

Usage: <main class> [-VAR <MyOption=String>]...
-VAR <MyOption=String>
Variable options. Valid keys: ARGUMENT1, OTHERARG, BOOLEANARG.

请注意,使用此解决方案,所有值都将具有相同的类型(本例中为String(,并且您可能需要在应用程序中转换为所需的类型(booleanint、其他…(。

然而,考虑到你帖子中的这句话,这可能是不可接受的:

我也不允许将多个参数作为参数折叠到一个-VAR选项中。

解决方案2:参数组

替代方案的一个想法是使用参数组:我们可以将ARGUMENT1OTHERARGBOOLEANARG作为单独的选项,并将它们放在一个组中,这样它们前面必须有-VAR选项。

由此产生的使用帮助如下所示:

Usage: group-demo [-VAR (ARGUMENT1=<arg1> | OTHERARG=<otherValue> |
BOOLEANARG=<bool>)]... [-hV]
-VAR                   Option prefix. Must be followed by one of
ARGUMENT1, OTHERARG or BOOLEANARG
ARGUMENT1=<arg1>       An arg. Must be preceded by -VAR.
OTHERARG=<otherValue>  Another arg. Must be preceded by -VAR.
BOOLEANARG=<bool>      A boolean arg. Must be preceded by -VAR.
-h, --help                 Show this help message and exit.
-V, --version              Print version information and exit.

实现可能看起来像这样:

@Command(name = "group-demo", mixinStandardHelpOptions = true,
sortOptions = false)
class UsingGroups implements Runnable {
static class MyGroup {
@Option(names = "-VAR", required = true,
description = "Option prefix. Must be followed by one of ARGUMENT1, OTHERARG or BOOLEANARG")
boolean ignored;
static class InnerGroup {
@Option(names = "ARGUMENT1", description = "An arg. Must be preceded by -VAR.")
String arg1;
@Option(names = "OTHERARG", description = "Another arg. Must be preceded by -VAR.")
String otherValue;
@Option(names = "BOOLEANARG", arity = "1",
description = "A boolean arg. Must be preceded by -VAR.")
Boolean bool;
}
// exclusive: only one of these options can follow a -VAR option
// multiplicity=1: InnerGroup must occur once
@ArgGroup(multiplicity = "1", exclusive = true)
InnerGroup inner;
}

// non-exclusive means co-occurring, so if -VAR is specified,
// then it must be followed by one of the InnerGroup options
@ArgGroup(multiplicity = "0..*", exclusive = false)
List<MyGroup> groupOccurrences;
@Override
public void run() {
// business logic here
System.out.printf("You specified %d -VAR options.%n", groupOccurrences.size());
for (MyGroup group : groupOccurrences) {
System.out.printf("ARGUMENT1=%s, ARGUMENT2=%s, BOOLEANARG=%s%n",
group.inner.arg1, group.inner.arg2, group.inner.arg3);
}
}
public static void main(String[] args) {
new CommandLine(new UsingGroups()).execute(args);
}
}

然后,用java UsingGroups -VAR ARGUMENT1=abc -VAR BOOLEANARG=true调用会给出:

You specified 2 -VAR options.
ARGUMENT1=abc, OTHERARG=null, BOOLEANARG=null
ARGUMENT1=null, OTHERARG=null, BOOLEANARG=true

使用这种方法,每次最终用户指定-VAR时,都会得到一个MyGroup对象。这个MyGroup对象有一个InnerGroup,它有许多字段,除了一个之外,所有字段都将是null。只有用户指定的字段将是非null。这就是这种方法的缺点:在应用程序中,您需要检查所有字段,以找到用户指定的非null字段。好处是,通过为@Option注释字段选择正确的类型,值将自动转换为目标类型。

最新更新