![](/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.