来自 clap 的文档default_value
:
注意:如果用户在运行时不使用这个参数,ArgMatches::is_present
仍然会返回 true。如果您希望确定参数是否在运行时使用,请考虑如果在运行时未使用参数ArgMatches::occurrences_of
,将返回哪个0
。
https://docs.rs/clap/2.32.0/clap/struct.Arg.html#method.default_value
这可用于获取您描述的行为:
extern crate clap;
use clap::{App, Arg};
use std::fs::File;
use std::io::prelude::*;
fn main() {
let matches = App::new("MyApp")
.version("0.1.0")
.about("Example for StackOverflow")
.arg(
Arg::with_name("config")
.short("c")
.long("config")
.value_name("FILE")
.help("Sets a custom config file"),
)
.arg(
Arg::with_name("example")
.short("e")
.long("example")
.help("Sets an example parameter")
.default_value("default_value")
.takes_value(true),
)
.get_matches();
let mut value = String::new();
if let Some(c) = matches.value_of("config") {
let file = File::open(c);
match file {
Ok(mut f) => {
// Note: I have a file `config.txt` that has contents `file_value`
f.read_to_string(&mut value).expect("Error reading value");
}
Err(_) => println!("Error reading file"),
}
// Note: this lets us override the config file value with the
// cli argument, if provided
if matches.occurrences_of("example") > 0 {
value = matches.value_of("example").unwrap().to_string();
}
} else {
value = matches.value_of("example").unwrap().to_string();
}
println!("Value for config: {}", value);
}
// Code above licensed CC0
// https://creativecommons.org/share-your-work/public-domain/cc0/
导致行为:
./target/debug/example
Value for config: default_value
./target/debug/example --example cli_value
Value for config: cli_value
./target/debug/example --config config.txt
Value for config: file_value
./target/debug/example --example cli_value --config config.txt
Value for config: cli_value