clap/tests/opts.rs

125 lines
5.6 KiB
Rust
Raw Normal View History

extern crate clap;
use clap::{App, Arg};
#[test]
fn opts_using_short() {
let r = App::new("opts")
.args(&[
Arg::from_usage("-f [flag] 'some flag'"),
Arg::from_usage("-c [color] 'some other flag'")
])
.get_matches_from_safe(vec!["", "-f", "some", "-c", "other"]);
assert!(r.is_ok());
let m = r.unwrap();
assert!(m.is_present("f"));
assert_eq!(m.value_of("f").unwrap(), "some");
assert!(m.is_present("c"));
assert_eq!(m.value_of("c").unwrap(), "other");
}
#[test]
fn lots_o_vals() {
let r = App::new("opts")
.arg(
Arg::from_usage("-o [opt]... 'some opt'"),
)
.get_matches_from_safe(vec!["", "-o",
"some", "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
"some", "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
"some", "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
"some", "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
"some", "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
"some", "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
"some", "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
"some", "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
"some", "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
"some", "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
"some", "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
"some", "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
"some", "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
"some", "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
"some", "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
"some", "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
"some", "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
"some", "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
"some", "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
"some", "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
"some", "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
"some", "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
"some", "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
"some", "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
"some", "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
"some", "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
"some", "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
]);
assert!(r.is_ok());
let m = r.unwrap();
assert!(m.is_present("o"));
assert_eq!(m.values_of("o").unwrap().collect::<Vec<_>>().len(), 297); // i.e. more than u8
}
#[test]
fn opts_using_long_space() {
let r = App::new("opts")
.args(&[
Arg::from_usage("--flag [flag] 'some flag'"),
Arg::from_usage("--color [color] 'some other flag'")
])
.get_matches_from_safe(vec!["", "--flag", "some", "--color", "other"]);
assert!(r.is_ok());
let m = r.unwrap();
assert!(m.is_present("flag"));
assert_eq!(m.value_of("flag").unwrap(), "some");
assert!(m.is_present("color"));
assert_eq!(m.value_of("color").unwrap(), "other");
}
#[test]
fn opts_using_long_equals() {
let r = App::new("opts")
.args(&[
Arg::from_usage("--flag [flag] 'some flag'"),
Arg::from_usage("--color [color] 'some other flag'")
])
.get_matches_from_safe(vec!["", "--flag=some", "--color=other"]);
assert!(r.is_ok());
let m = r.unwrap();
assert!(m.is_present("flag"));
assert_eq!(m.value_of("flag").unwrap(), "some");
assert!(m.is_present("color"));
assert_eq!(m.value_of("color").unwrap(), "other");
}
#[test]
fn opts_using_mixed() {
let r = App::new("opts")
.args(&[
Arg::from_usage("-f, --flag [flag] 'some flag'"),
Arg::from_usage("-c, --color [color] 'some other flag'")
])
.get_matches_from_safe(vec!["", "-f", "some", "--color", "other"]);
assert!(r.is_ok());
let m = r.unwrap();
assert!(m.is_present("flag"));
assert_eq!(m.value_of("flag").unwrap(), "some");
assert!(m.is_present("color"));
assert_eq!(m.value_of("color").unwrap(), "other");
}
#[test]
fn opts_using_mixed2() {
let r = App::new("opts")
.args(&[
Arg::from_usage("-f, --flag [flag] 'some flag'"),
Arg::from_usage("-c, --color [color] 'some other flag'")
])
.get_matches_from_safe(vec!["", "--flag=some", "-c", "other"]);
assert!(r.is_ok());
let m = r.unwrap();
assert!(m.is_present("flag"));
assert_eq!(m.value_of("flag").unwrap(), "some");
assert!(m.is_present("color"));
assert_eq!(m.value_of("color").unwrap(), "other");
}