Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/398.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何从字符串中转义字符选项_Java_Picocli - Fatal编程技术网

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

Java 如何从字符串中转义字符选项,java,picocli,Java,Picocli,我有一个简单的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

我有一个简单的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);
}
@命令(name=“process data”,description=“Bla Bla Bla…”)
公共类MyApp实现了可调用{
@选择权(
名称={--arg-a“},
必需=真
)
私有字符串argA;
@选择权(
名称={--arg-b},
必需=真
)
私有字符串argB;
@选择权(
名称={--arg-c},
必需=真
)
私有字符串argC;
@凌驾
公共整数调用(){
...
}
}
公共静态void main(字符串[]args){
新命令行(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;
    }
}

请发布设置命令选项并调用picocli解析器的完整、最少的代码。我更新了我的问题。看起来我和你们一样使用它,但对我来说,第一个空格上的解析器拆分选项。@jswieca你们使用的是哪个shell?这是在Windows/cmd.exe下吗?我发现一个问题。我在Windows上测试了它,执行如上所述,但最后我想在linux上运行它,我使用了带有
$@
java-D的简单schell脚本-jar my-app.jar$@
。当我删除$@并在一个脚本finle中传递所有选项时,它开始work@jswieca使用
$@
时要小心,您几乎总是需要引用它:
“$@”
。这将在传递到另一个命令时完全保留单个参数中的引号和空格。感谢您的帮助!