![](/img/trans.png)
[英]How can I pass in arguments to a command line Java program and escape all special characters?
[英]How can I pass all command line arguments through Clap to another program?
我有一个使用Clap来处理命令参数解析的程序foo
。 foo
调用另一个程序bar
。 最近,我决定foo
用户可以根据需要将参数传递给bar
。 我将bar
命令添加到了Clap中:
let matches = App::new("Foo")
.arg(Arg::with_name("file").value_name("FILE").required(true))
.arg(
Arg::with_name("bar")
.value_name("[BAR_OPTIONS]")
.short("b")
.long("bar")
.multiple(true)
.help("Invoke bar with these options"),
)
.get_matches();
当我尝试将命令"-baz=3"
传递给bar
如下所示:
./foo -b -baz=3 file.txt
要么
./foo -b "-baz=3" file.txt
clap
返回此错误:
error: Found argument '-b' which wasn't expected, or isn't valid in this context
如何通过Clap传送命令?
如果参数bar
的值本身可能以连字符开头,那么您需要设置allow_hyphen_values
选项:
let _matches = App::new("Foo")
.arg(Arg::with_name("file").value_name("FILE").required(true))
.arg(
Arg::with_name("bar")
.value_name("[BAR_OPTIONS]")
.allow_hyphen_values(true)
.short("b")
.long("bar")
.multiple(true)
.help("Invoke bar with these options"),
)
.get_matches();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.