我正在学习rust,并试图创建一个find like实用程序(是的,还有一个),我使用clap并试图支持命令行和配置文件来获取程序参数(这与clap yml文件无关)。
我正在尝试解析命令,如果没有命令传递给应用程序,我将尝试从配置文件中加载它们。现在,我不知道如何以一种惯用的方式来做这件事。
fn main() {
let matches = App::new("findx")
.version(crate_version!())
.author(crate_authors!())
.about("find + directory operations utility")
.arg(
Arg::with_name("paths")
...
)
.arg(
Arg::with_name("patterns")
...
)
.arg(
Arg::with_name("operation")
...
)
.get_matches();
let paths;
let patterns;
let operation;
if matches.is_present("patterns") && matches.is_present("operation") {
patterns = matches.values_of("patterns").unwrap().collect();
paths = matches.values_of("paths").unwrap_or(clap::Values<&str>{"./"}).collect(); // this doesn't work
operation = match matches.value_of("operation").unwrap() { // I dont like this
"Append" => Operation::Append,
"Prepend" => Operation::Prepend,
"Rename" => Operation::Rename,
_ => {
print!("Operation unsupported");
process::exit(1);
}
};
}else if Path::new("findx.yml").is_file(){
//TODO: try load from config file
}else{
eprintln!("Command line parameters or findx.yml file must be provided");
process::exit(1);
}
if let Err(e) = findx::run(Config {
paths: paths,
patterns: patterns,
operation: operation,
}) {
eprintln!("Application error: {}", e);
process::exit(1);
}
}有一种惯用的方法可以将Option和Result类型的值提取到相同的作用域中,我的意思是,我读过的所有示例都使用match或if let Some(x)在模式匹配的作用域中使用x值,但我需要将该值赋给一个变量。
谁能帮我解决这个问题,或者给我指个正确的方向?
诚挚的问候
发布于 2019-10-15 06:17:44
就我个人而言,我认为使用match语句并将其折叠或放置在另一个函数中并没有什么错。但是如果你想删除它,有很多选择。
可以使用.default_value_if()方法,该方法对于clap::Arg是impl,并且根据匹配的arm具有不同的默认值。
//sets value of arg "other" to "default" if value of "--opt" is "special"
let m = App::new("prog")
.arg(Arg::with_name("opt")
.takes_value(true)
.long("opt"))
.arg(Arg::with_name("other")
.long("other")
.default_value_if("opt", Some("special"), "default"))
.get_matches_from(vec![
"prog", "--opt", "special"
]);
assert_eq!(m.value_of("other"), Some("default"));此外,您还可以向operation添加验证器,或者将有效操作值转换为标志。
下面是一个将匹配arm值转换为单个标志的示例(为清晰起见,请使用较小的示例)。
extern crate clap;
use clap::{Arg,App};
fn command_line_interface<'a>() -> clap::ArgMatches<'a> {
//Sets the command line interface of the program.
App::new("something")
.version("0.1")
.arg(Arg::with_name("rename")
.help("renames something")
.short("r")
.long("rename"))
.arg(Arg::with_name("prepend")
.help("prepends something")
.short("p")
.long("prepend"))
.arg(Arg::with_name("append")
.help("appends something")
.short("a")
.long("append"))
.get_matches()
}
#[derive(Debug)]
enum Operation {
Rename,
Append,
Prepend,
}
fn main() {
let matches = command_line_interface();
let operation = if matches.is_present("rename") {
Operation::Rename
} else if matches.is_present("prepend"){
Operation::Prepend
} else {
//DEFAULT
Operation::Append
};
println!("Value of operation is {:?}",operation);
}我希望这能帮到你!
编辑:
您还可以将Subcommands用于您的特定操作。这完全取决于你想要的界面是什么样的。
let app_m = App::new("git")
.subcommand(SubCommand::with_name("clone"))
.subcommand(SubCommand::with_name("push"))
.subcommand(SubCommand::with_name("commit"))
.get_matches();
match app_m.subcommand() {
("clone", Some(sub_m)) => {}, // clone was used
("push", Some(sub_m)) => {}, // push was used
("commit", Some(sub_m)) => {}, // commit was used
_ => {}, // Either no subcommand or one not tested for...
}https://stackoverflow.com/questions/58371141
复制相似问题