【发布时间】:2020-09-09 19:26:03
【问题描述】:
我的问题与here 提出的问题类似,也更简单。
我有三个选项,-A、-A1、-A2(从概念上讲,属于单个组)。所需的关系如下:
- 这些都不是必需的
-
-A应与-A1或-A2中的至少一个一起提供 -
-A1和-A2都可以用一个-A给出
换句话说:
- 有效规格:
-A -A1、-A -A2和-A -A1 -A2 - 无效规格:
-A、-A1、-A2和-A1 -A2
这就是我使用两个@ArgGroups:
import picocli.CommandLine;
import picocli.CommandLine.*;
import picocli.CommandLine.Model.CommandSpec;
public class App implements Runnable {
static class MyGroupX {
@Option(names="-A1", required=false) boolean A1;
@Option(names="-A2", required=false) boolean A2;
}
static class MyGroup {
@Option(names="-A", required=true) boolean A;
@ArgGroup(exclusive=false, multiplicity="1") MyGroupX myGroupX;
}
@ArgGroup(exclusive=false) MyGroup myGroup;
@Spec CommandSpec spec;
@Override
public void run() {
System.out.printf("OK: %s%n", spec.commandLine().getParseResult().originalArgs());
}
public static void main(String[] args) {
//test: these should be valid
new CommandLine(new App()).execute();
new CommandLine(new App()).execute("-A -A1".split(" "));
new CommandLine(new App()).execute("-A -A2".split(" "));
new CommandLine(new App()).execute("-A -A1 -A2".split(" "));
//test: these should FAIL
new CommandLine(new App()).execute("-A");
new CommandLine(new App()).execute("-A1");
new CommandLine(new App()).execute("-A2");
new CommandLine(new App()).execute("-A1 -A2".split(" "));
}
}
有没有更简单的方法?
谢谢!
【问题讨论】:
标签: picocli