Skip to content

Instantly share code, notes, and snippets.

@cite-reader
Created June 5, 2016 23:46
Show Gist options
  • Save cite-reader/1286a870b67fb94cd29e44f007cc6908 to your computer and use it in GitHub Desktop.
Save cite-reader/1286a870b67fb94cd29e44f007cc6908 to your computer and use it in GitHub Desktop.
Testing a Clap arg with default value
extern crate clap;
use clap::{Arg, App};
fn main() {
let matches = App::new("A test")
.arg(Arg::with_name("has-default")
.short("d")
.takes_value(true))
.get_matches();
let value: u32 = matches.value_of("has-default").and_then(|s| s.parse().ok()).unwrap_or(42);
println!("The flag's value was {}", value);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment