java - 如何从字符串选项中转义字符
问题描述
我有一个简单的 java 应用程序,带有一个命令和几个选项。对于我想传递带有单引号和空格的字符串的选项之一。可能吗?
我使用 picocli 4.5.0。以下是我想要实现的
java -jar my-app.jar my-command --arg-a=aaa --arg-b=bbb --arg-c="x=y and z=w(to_date(xxx, 'YYYYMMDD'))"
我设置trimQuotes
为false
,但出现错误
Unmatched arguments from index X: 'and' 'z=w(to_date(xxx, 'YYYYMMDD'))'
是否可以转义整个字符串/选项?
@Command(name = "process-data", description = "Bla bla bla...")
public class MyApp implements Callable<Integer> {
@Option(
names = {"--arg-a"},
required = true
)
private String argA;
@Option(
names = {"--arg-b"},
required = true
)
private String argB;
@Option(
names = {"--arg-c"},
required = true
)
private String argC;
@Override
public Integer call() {
...
}
}
public static void main(String[] args) {
new CommandLine(MyApp.create()).execute(args);
}
解决方案
不需要trimQuotes
。以下作品:
@Command(description = "nothing here", name = "my-command", version = "0.1")
public class MyCommand implements Callable<Void> {
@Option(names = {"--arg-a"}, description = "A")
private String a;
@Option(names = {"--arg-b"}, description = "B")
private String b;
@Option(names = {"--arg-c"}, description = "C")
private String c;
public static void main(final String[] args) {
System.exit(new CommandLine(new MyCommand()).execute(args));
}
@Override
public Void call() throws Exception {
System.err.printf("a = %s\nb = %s\nc = %s\n", a, b, c);
return null;
}
}
⟩⟩⟩ java MyCommand --arg-a=aaa --arg-b=bbb --arg-c="x=y and z=w(to_date(xxx, 'YYYYMMDD'))"
a = aaa
b = bbb
c = x=y and z=w(to_date(xxx, 'YYYYMMDD'))
推荐阅读
- centos - 如何避免 centos-release rpm 通过 spacewalk 安装 yum repo 文件
- reactjs - 如何在链接“to”属性中传递回调函数
- python - “是否有基于数据框另一列的某些值添加新列的 pandas 函数?”
- docker - docker: 'build' 不是 docker 命令
- sql - SQL查询中断发生时间和持续时间
- kubernetes - 集群重启后Kafka主题消息消失
- c# - Service Fabric 参与者中的静态对象
- swift - 暂停 SpriteKit 游戏中所有正在进行的 asyncAfter() 延迟
- android - 试图将微调器背景设置为白色
- c++ - 程序停止而不运行 while 循环