我想为我的程序提供一个搜索字符串,例如:
cmd.execute("getDevices", "-h 1.2.3.4", "-p myPSW", "-u myUser", "-n red|blue&black,-nonprod");
我想创建谓词来搜索包含红色或蓝色和黑色但不包含非产品的主机名。我不清楚如何解析逻辑运算符以及 Picocli 中的字符串来创建谓词。是否有一种简单直接的方法将字符串解析为谓词?
我的 CLI 设置如下:
@Command(name = "HostMagicCLI", mixinStandardHelpOptions = true,
version = "1.0",
description = "Do Stuff With Hosts"
,
subcommands = {TufinDevices.class}
)
public class HostMagicCLI implements Runnable {
public static void main(String[] args) {
CommandLine cmd = new CommandLine(new InterfaceMagicCLI());
cmd.setExecutionStrategy(new RunAll());
cmd.getHelpSectionMap().put(SECTION_KEY_COMMAND_LIST, new MyCommandListRenderer());
cmd.usage(System.out);
cmd.execute("getDevices", "-h1.2.3.4", "-p myPSW", "-u myUser", "-n red|blue&black");
}
@Override
public void run() {
System.out.println("Running..");
}
}
@Command(name = "getDevices", aliases = {"l"}, description = "SpecifyTufin Credentials", subcommands = {InterfaceCommand.class})
class TufinDevices implements Runnable {
.
.//Options to collect user,psw, host etc.
.
@CommandLine.Option(names = {"-n", "--n"}, split = ",", arity = "0..*", description = "Hostname Contains")
String[] hostNameContains;
private void filter(TufinDeviceCollection<TufinDevice> devices) {
if (hostNameContains != null) {
Predicate< ? super TufinDevice> deviceFilter = device -> Arrays.stream(hostNameContains)
.allMatch(input -> device.getHostName().toLowerCase().contains(input.toLowerCase()));
devices = devices.stream()
.sequential()
.filter(deviceFilter)
.collect(Collectors.toCollection(TufinDeviceCollection<TufinDevice>::new));
}
@Override
public void run() {
try {
TufinDeviceCollection<TufinDevice> FETCH_DEVICES = Tufin.FETCH_DEVICES(user.trim(), password.trim(), hostName.trim());
this.filter(FETCH_DEVICES);
} catch (IOException | NoSuchAlgorithmException | KeyManagementException | IPConverter.InvalidIPException ex) {
Logger.getLogger(TufinDevices.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
最佳答案
我怀疑您可能想要使用一个库来解析最终用户指定为过滤器表达式(-n
参数)的字符串。查看像 Spring Expression Language 这样的库可能是一个主意。 , OGNL , JXPath ,可能还有其他。或者,如果很容易在 Groovy 中编写这样的过滤器或BeanShell ,这些语言可以从 Java 调用,因此您可以从 Java 命令调用该过滤器。
注意: 我注意到该示例将参数传递给 picocli 解析器,如下所示:
cmd.execute("getDevices", "-h 1.2.3.4", "-p myPSW", "-u myUser", "-n red|blue&black,-nonprod");
这可能会给出一个错误,解释“没有定义 -p myPSW
选项”。
在测试中,如果直接调用 execute
方法,请确保单独传递参数,如下所示:
cmd.execute("getDevices", "-h", "1.2.3.4", "-p", "myPSW", "-u", "myUser", "-n", "red|blue&black,-nonprod");
关于java - Picocli 解析搜索字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57298492/