我的问题与此处提出的问题相似且更简单。
我有三个选项,-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(" "));
}
}
有没有更简单的方法?
谢谢!