Make use of .default_value in clap

This commit is contained in:
Magnus Hoff 2017-11-01 11:51:58 +01:00
parent 548555c45e
commit 833c96b58d

View file

@ -52,18 +52,20 @@ fn args<'a>() -> clap::ArgMatches<'a> {
.help("Sets the database file to use") .help("Sets the database file to use")
.required(true)) .required(true))
.arg(Arg::with_name(PORT) .arg(Arg::with_name(PORT)
.help("Sets the listening port. Defaults to 8080") .help("Sets the listening port")
.short("p") .short("p")
.long(PORT) .long(PORT)
.default_value("8080")
.validator(|x| match x.parse::<u16>() { .validator(|x| match x.parse::<u16>() {
Ok(_) => Ok(()), Ok(_) => Ok(()),
Err(_) => Err("Must be an integer in the range [0, 65535]".into()) Err(_) => Err("Must be an integer in the range [0, 65535]".into())
}) })
.takes_value(true)) .takes_value(true))
.arg(Arg::with_name(ADDRESS) .arg(Arg::with_name(ADDRESS)
.help("Sets the TCP address to bind to. Defaults to 127.0.0.1") .help("Sets the IP address to bind to")
.short("a") .short("a")
.long(ADDRESS) .long(ADDRESS)
.default_value("127.0.0.1")
.validator(|x| match x.parse::<IpAddr>() { .validator(|x| match x.parse::<IpAddr>() {
Ok(_) => Ok(()), Ok(_) => Ok(()),
Err(_) => Err("Must be a valid IP address".into()) Err(_) => Err("Must be a valid IP address".into())
@ -80,13 +82,11 @@ fn args<'a>() -> clap::ArgMatches<'a> {
fn core_main() -> Result<(), Box<std::error::Error>> { fn core_main() -> Result<(), Box<std::error::Error>> {
let args = args(); let args = args();
let db_file = args.value_of(DATABASE).expect("Guaranteed by clap").to_owned(); const CLAP: &str = "Guaranteed by clap";
let bind_host = args.value_of(ADDRESS) const VALIDATOR: &str = "Guaranteed by clap validator";
.map(|p| p.parse().expect("Guaranteed by validator")) let db_file = args.value_of(DATABASE).expect(CLAP).to_owned();
.unwrap_or_else(|| "127.0.0.1".parse().unwrap()); let bind_host = args.value_of(ADDRESS).expect(CLAP).parse().expect(VALIDATOR);
let bind_port = args.value_of(PORT) let bind_port = args.value_of(PORT).expect(CLAP).parse().expect(VALIDATOR);
.map(|p| p.parse().expect("Guaranteed by validator"))
.unwrap_or(8080);
let trust_identity = args.is_present(TRUST_IDENTITY); let trust_identity = args.is_present(TRUST_IDENTITY);