![](/img/trans.png)
[英]How do I change the names of the values in a clap argument that takes multiple values?
[英]Taking multiple values in an argument in Clap
我正在使用Clap并且我正在尝试使用它,以便子命令可以为参数采用多个值。 我追求的界面是:
just use repo [files]
一个例子:
just use radar/dot-files gitaliases ryan-aliases
此处的repo
参数将是字符串 "radar/dot-files",而files
参数将是["gitaliases", "ryan-aliases"]
的向量。
这是我尝试使用的代码:
let matches = App::new("just")
.version("v1.0-beta")
.subcommand(
SubCommand::with_name("use")
.arg(Arg::with_name("repo").required(true))
.arg(
Arg::with_name("files")
.required(true)
.multiple(true)
.number_of_values(1),
),
)
.get_matches();
if let Some(matches) = matches.subcommand_matches("use") {
println!("{:?}", matches.value_of("files").unwrap())
}
这仅输出我指定的第一个文件,而不是所有文件。
对于任意数量的参数,如何使其输出所有不同的文件?
来自 irc.mozilla.org#rust 的 durka42 告诉我使用values_of
而不是value_of
:
let matches = App::new("just")
.version("v1.0-beta")
.subcommand(
SubCommand::with_name("use")
.arg(Arg::with_name("repo").required(true))
.arg(Arg::with_name("files").required(true).min_values(1)),
)
.get_matches();
if let Some(matches) = matches.subcommand_matches("use") {
let files: Vec<_> = matches.values_of("files").unwrap().collect();
println!("{}", files[0]);
println!("{}", files[1]);
}
如果我愿意,我可能可以使用iter()
调用来浏览文件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.