picocli :ArgGroup 中的依赖参数
picocli : dependent arguments in ArgGroup
我的问题与 .
的问题相似且更简单
我有三个选项,-A
、-A1
、-A2
(从概念上讲,是单个组的一部分)。所需的关系是这些:
- 这些都不是必需的
-A
应与 -A1
或 -A2
中的至少一项一起给出
-A1
和 -A2
都可以用一个 -A
换句话说:
- 有效规格:
-A -A1
、-A -A2
和 -A -A1 -A2
- 无效的规格:
-A
、-A1
、-A2
和 -A1 -A2
这是我使用两个 @ArgGroup
s:
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(" "));
}
}
有没有更简单的方法?
谢谢!
有些关系不能只用 @ArgGroup
注释来表达,在这种情况下,需要在应用程序中自定义验证逻辑。
但是,您的应用程序并非如此。在我看来,您已经找到了一种非常紧凑的方式来表达您的要求。
有效的用户输入序列 -A -A1
、-A -A2
和 -A -A1 -A2
都被接受。
无效的用户输入序列全部被拒绝,并显示相当明确的错误消息:
Input Error message
----- -------------
-A Missing required argument(s): ([-A1] [-A2])
-A1 Missing required argument(s): -A
-A2 Missing required argument(s): -A
-A1 -A2 Missing required argument(s): -A
应用程序在代码中没有任何明确的验证逻辑的情况下实现了所有这些,所有这些都是通过注释以声明方式完成的。使命已完成,我会说。我看不到进一步改进的方法。
我的问题与
我有三个选项,-A
、-A1
、-A2
(从概念上讲,是单个组的一部分)。所需的关系是这些:
- 这些都不是必需的
-A
应与-A1
或-A2
中的至少一项一起给出
-A1
和-A2
都可以用一个-A
换句话说:
- 有效规格:
-A -A1
、-A -A2
和-A -A1 -A2
- 无效的规格:
-A
、-A1
、-A2
和-A1 -A2
这是我使用两个 @ArgGroup
s:
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(" "));
}
}
有没有更简单的方法?
谢谢!
有些关系不能只用 @ArgGroup
注释来表达,在这种情况下,需要在应用程序中自定义验证逻辑。
但是,您的应用程序并非如此。在我看来,您已经找到了一种非常紧凑的方式来表达您的要求。
有效的用户输入序列 -A -A1
、-A -A2
和 -A -A1 -A2
都被接受。
无效的用户输入序列全部被拒绝,并显示相当明确的错误消息:
Input Error message
----- -------------
-A Missing required argument(s): ([-A1] [-A2])
-A1 Missing required argument(s): -A
-A2 Missing required argument(s): -A
-A1 -A2 Missing required argument(s): -A
应用程序在代码中没有任何明确的验证逻辑的情况下实现了所有这些,所有这些都是通过注释以声明方式完成的。使命已完成,我会说。我看不到进一步改进的方法。