我有一个程序foo
使用Clap https://github.com/clap-rs/clap处理命令参数解析。foo
调用另一个程序,bar
。最近,我决定用户foo
应该能够将参数传递给bar
如果他们喜欢的话。我添加了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"
to bar
像这样:
./foo -b -baz=3 file.txt
or
./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 https://docs.rs/clap/2.32.0/clap/struct.Arg.html#method.allow_hyphen_values option:
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();
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)