Skip to content

Commit

Permalink
test(clap): add wider capability test coverage - wip
Browse files Browse the repository at this point in the history
  • Loading branch information
kbknapp committed Apr 2, 2015
1 parent a0a2a40 commit 1da0905
Showing 1 changed file with 94 additions and 45 deletions.
139 changes: 94 additions & 45 deletions claptests/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,74 +3,123 @@ extern crate clap;
use clap::{App, Arg, SubCommand};

fn main() {
let version = format!("{}.{}.{}{}",
// Test version from Cargo.toml
let version = format!("{}.{}.{}{}",
env!("CARGO_PKG_VERSION_MAJOR"),
env!("CARGO_PKG_VERSION_MINOR"),
env!("CARGO_PKG_VERSION_PATCH"),
option_env!("CARGO_PKG_VERSION_PRE").unwrap_or(""));
option_env!("CARGO_PKG_VERSION_PRE").unwrap_or(""));
let matches = App::new("claptests")
.version(&version[..])
.about("tests clap library")
.author("Kevin K. <[email protected]>")
.arg(Arg::new("flag")
.short("f")
.long("flag")
.help("tests flags")
.multiple(true))
.arg(Arg::new("option")
.short("o")
.long("option")
.help("tests options")
.takes_value(true)
.multiple(true))
.arg(Arg::new("positional")
.index(1)
.help("tests positionals"))
.subcommand(SubCommand::new("subcmd")
.about("tests subcommands")
.version("0.1")
.author("Kevin K. <[email protected]>")
.arg(Arg::new("scflag")
.short("f")
.long("flag")
.help("tests flags")
.multiple(true))
.arg(Arg::new("scoption")
.short("o")
.long("option")
.help("tests options")
.takes_value(true)
.multiple(true))
.arg(Arg::new("scpositional")
.index(1)
.help("tests positionals")))
.get_matches();
.version(&version[..])
.about("tests clap library")
.author("Kevin K. <[email protected]>")
.arg(Arg::new("flag")
.short("f")
.long("flag")
.help("tests flags")
.multiple(true))
.arg(Arg::new("option")
.short("o")
.long("option")
.required(true)
.help("tests options")
.takes_value(true)
.multiple(true))
.arg(Arg::new("positional")
.index(1)
.help("tests positionals"))
.args(vec![
Arg::new("flag2").short("F").mutually_excludes("flag").help("tests flags with exclusions").requires("option2"),
Arg::new("option2").long("long-option-2").mutually_excludes("option").help("tests long options with exclusions and requirements").requires_all(vec!["positional", "positional2"]),
Arg::new("positional2").index(2).help("tests positionals with exclusions and multiple"),
Arg::new("option3").takes_value(true).short("O").possible_values(vec!["fast", "slow"]).help("test options with specific value sets").requires("positional3"),
Arg::new("positional3").index(3).multiple(true).possible_values(vec!["vi", "emacs"]).help("tests positionals with specific value sets")
])
.subcommand(SubCommand::new("subcmd")
.about("tests subcommands")
.version("0.1")
.author("Kevin K. <[email protected]>")
.arg(Arg::new("scflag")
.short("f")
.long("flag")
.help("tests flags")
.multiple(true))
.arg(Arg::new("scoption")
.short("o")
.long("option")
.help("tests options")
.takes_value(true)
.multiple(true))
.arg(Arg::new("scpositional")
.index(1)
.help("tests positionals")))
.get_matches();

if matches.is_present("flag") {
println!("flag present {} times", matches.occurrences_of("flag"));
println!("flag present {} times", matches.occurrences_of("flag"));
} else {
println!("flag NOT present");
println!("flag NOT present");
}

if matches.is_present("option") {
if let Some(v) = matches.value_of("option") {
println!("option present {} times with value: {}",matches.occurrences_of("option"), v);
println!("option present {} times with value: {}",matches.occurrences_of("option"), v);
}
if let Some(ref ov) = matches.values_of("option") {
for o in ov {
println!("An option: {}", o);
}
for o in ov {
println!("An option: {}", o);
}
}
} else {
println!("option NOT present");
}

if let Some(p) = matches.value_of("positional") {
println!("positional present with value: {}", p);
println!("positional present with value: {}", p);
} else {
println!("positional NOT present");
}

if matches.is_present("flag2") {
println!("flag2 present");
println!("option2 present with value of: {}", matches.value_of("option2").unwrap());
println!("positional2 present with value of: {}", matches.value_of("positional2").unwrap());
} else {
println!("flag2 NOT present");
println!("option2 maybe present with value of: {}", matches.value_of("option2").unwrap_or("Nothing"));
println!("positional2 maybe present with value of: {}", matches.value_of("positional2").unwrap_or("Nothing"));
}

match matches.value_of("option3").unwrap_or("") {
"fast" => println!("option3 present quickly"),
"slow" => println!("option3 present slowly"),
_ => println!("option3 NOT present")
}

match matches.value_of("positional3").unwrap_or("") {
"vi" => println!("positional3 present in vi mode"),
"emacs" => println!("positional3 present in emacs mode"),
_ => println!("positional3 NOT present")
}

if matches.is_present("option") {
if let Some(v) = matches.value_of("option") {
println!("option present {} times with value: {}",matches.occurrences_of("option"), v);
}
if let Some(ref ov) = matches.values_of("option") {
for o in ov {
println!("An option: {}", o);
}
}
} else {
println!("option NOT present");
}

if let Some(p) = matches.value_of("positional") {
println!("positional present with value: {}", p);
} else {
println!("positional NOT present");
}
if matches.is_present("subcmd") {
println!("subcmd present");
if let Some(matches) = matches.subcommand_matches("subcmd") {
Expand Down

0 comments on commit 1da0905

Please sign in to comment.