如何从字符串选项中转义字符



我有一个简单的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 = %snb = %snc = %sn", 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'))

最新更新