[英]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.