gpt4 book ai didi

java - Picocli 需要基于主要选项的选项选择

转载 作者:行者123 更新时间:2023-12-05 02:54:45 24 4
gpt4 key购买 nike

我想用以下格式用 picocli 解析选项:

application -mode CLIENT -c aaaa -d bbbb
application -mode SERVER -e xxxx -f yyyy

mode 是一个 enum,其值为 { CLIENT, SERVER }

  • 如果mode == CLIENT-c-d选项是必须的,而-e,不得使用 -f
  • 如果mode == SERVER-e-f选项是必须的,而-c,不得使用 -d

换句话说,我想根据一个关键选项来选择所需的选项。这在 picocli 中可能吗?

最佳答案

是的,这是可能的。一种方法是简单的编程验证:

import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Model.CommandSpec;
import picocli.CommandLine.Option;
import picocli.CommandLine.ParameterException;
import picocli.CommandLine.Spec;

import java.util.Objects;
import java.util.function.Predicate;

@Command(name = "application", mixinStandardHelpOptions = true)
public class MyApp implements Runnable {

enum Mode {CLIENT, SERVER}

@Option(names = "-mode", required = true)
Mode mode;

@Option(names = "-c") String c;
@Option(names = "-d") String d;
@Option(names = "-e") String e;
@Option(names = "-f") String f;

@Spec CommandSpec spec;

public static void main(String[] args) {
System.exit(new CommandLine(new MyApp()).execute(args));
}

@Override
public void run() {
validateInput();
// business logic here...
}

private void validateInput() {
String INVALID = "Error: option(s) %s cannot be used in %s mode";
String REQUIRED = "Error: option(s) %s are required in %s mode";
if (mode == Mode.CLIENT) {
check(INVALID, "CLIENT", Objects::isNull, e, "-e", f, "-f");
check(REQUIRED, "CLIENT", Objects::nonNull, c, "-c", d, "-d");
} else if (mode == Mode.SERVER) {
check(INVALID, "SERVER", Objects::isNull, c, "-c", d, "-d");
check(REQUIRED, "SERVER", Objects::nonNull, e, "-e", f, "-f");
}
}

private void check(String msg, String param, Predicate<String> validator, String... valuesAndLabels) {
String desc = "";
String sep = "";
for (int i = 0; i < valuesAndLabels.length; i += 2) {
if (validator.test(valuesAndLabels[i])) {
desc = sep + valuesAndLabels[i + 1];
sep = ", ";
}
}
if (desc.length() > 0) {
throw new ParameterException(spec.commandLine(), String.format(msg, desc, param));
}
}
}

或者,如果您愿意稍微更改您的要求,我们可以使用 picocli 的 argument groups对于更具声明性的方法:

import picocli.CommandLine;
import picocli.CommandLine.ArgGroup;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;

@Command(name = "application", mixinStandardHelpOptions = true)
public class MyApp2 implements Runnable {

static class ClientArgs {
@Option(names = "-clientMode", required = true) boolean clientMode;
@Option(names = "-c", required = true) String c;
@Option(names = "-d", required = true) String d;
}

static class ServerArgs {
@Option(names = "-serverMode", required = true) boolean serverMode;
@Option(names = "-e", required = true) String e;
@Option(names = "-f", required = true) String f;
}

static class Args {
@ArgGroup(exclusive = false, multiplicity = "1", heading = "CLIENT mode args%n")
ClientArgs clientArgs;

@ArgGroup(exclusive = false, multiplicity = "1", heading = "SERVER mode args%n")
ServerArgs serverArgs;
}

@ArgGroup(exclusive = true, multiplicity = "1")
Args args;

public static void main(String[] args) {
System.exit(new CommandLine(new MyApp2()).execute(args));
}

@Override
public void run() {
// business logic here...
}
}

当仅使用 -serverMode 调用时,第二个示例将显示此错误消息,后跟使用帮助消息:

Error: Missing required argument(s): -e=<e>, -f=<f>
Usage: application ((-clientMode -c=<c> -d=<d>) | (-serverMode -e=<e> -f=<f>))
...

请注意,这种声明式方法无法通过单个 -mode 选项实现:每个参数组都需要自己的选项(我选择了 -clientMode-serverMode 在这个例子中)。这允许 picocli 解析器找出哪些选项必须一起出现,哪些选项是相互排斥的。

关于java - Picocli 需要基于主要选项的选项选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61665865/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com