我有一个使用 Clap 的程序 foo
处理命令参数解析。 foo
调用另一个程序 bar
。最近,我决定 foo
的用户如果愿意,应该能够将参数传递给 bar
。我向 Clap 添加了 bar
命令:
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();
关于command-line - 如何通过 Clap 将所有命令行参数传递给另一个程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54191836/