解决方案 1:地图选项
最简单的解决方案是制作-VAR
一个地图选项。这可能看起来像这样:
@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
在此示例中),您可能需要在应用程序中转换为所需的类型(boolean
, int
, other...)。
但是,鉴于您帖子中的这句话,这可能是不可接受的:
我也不允许将多个参数作为参数折叠到一个 -VAR 选项中。
解决方案 2:参数组
另一种方法的想法是使用参数组:我们可以制作ARGUMENT1
、OTHERARG
和BOOLEANARG
单独的选项,并将它们放在一个组中,以便它们必须在-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
使用这种方法,您将MyGroup
在最终用户每次指定时获得一个对象-VAR
。这个MyGroup
对象有一个InnerGroup
which 有很多字段,除了其中一个之外,所有字段都是null
. 只有用户指定的字段才会是非null
. 这就是这种方法的缺点:在应用程序中,您需要检查所有字段以找到null
用户指定的非字段。好处是通过为@Option
-annotated 字段选择正确的类型,值将自动转换为目标类型。