2020-02-04 08:10:53 +00:00
|
|
|
mod utils;
|
|
|
|
|
2017-01-03 04:05:23 +00:00
|
|
|
use std::io::Write;
|
|
|
|
use std::str;
|
|
|
|
|
2020-04-22 10:57:21 +00:00
|
|
|
use clap::{App, Arg};
|
2015-08-27 21:03:45 +00:00
|
|
|
|
2019-10-02 13:27:19 +00:00
|
|
|
static SCF2OP: &str = "flag present 2 times
|
2016-05-09 03:20:50 +00:00
|
|
|
option NOT present
|
|
|
|
positional NOT present
|
|
|
|
flag2 NOT present
|
|
|
|
option2 maybe present with value of: Nothing
|
|
|
|
positional2 maybe present with value of: Nothing
|
|
|
|
option3 NOT present
|
|
|
|
positional3 NOT present
|
|
|
|
option NOT present
|
|
|
|
positional NOT present
|
|
|
|
subcmd present
|
|
|
|
flag present 2 times
|
|
|
|
scoption present with value: some
|
|
|
|
An scoption: some
|
|
|
|
scpositional present with value: value
|
|
|
|
";
|
|
|
|
|
2019-10-02 13:27:19 +00:00
|
|
|
static SCFOP: &str = "flag present 1 times
|
2016-05-09 03:20:50 +00:00
|
|
|
option NOT present
|
|
|
|
positional NOT present
|
|
|
|
flag2 NOT present
|
|
|
|
option2 maybe present with value of: Nothing
|
|
|
|
positional2 maybe present with value of: Nothing
|
|
|
|
option3 NOT present
|
|
|
|
positional3 NOT present
|
|
|
|
option NOT present
|
|
|
|
positional NOT present
|
|
|
|
subcmd present
|
|
|
|
flag present 1 times
|
|
|
|
scoption present with value: some
|
|
|
|
An scoption: some
|
|
|
|
scpositional present with value: value
|
|
|
|
";
|
|
|
|
|
2019-10-02 13:27:19 +00:00
|
|
|
static O2P: &str = "flag NOT present
|
2016-05-09 03:20:50 +00:00
|
|
|
option present 2 times with value: some
|
|
|
|
An option: some
|
|
|
|
An option: other
|
|
|
|
positional present with value: value
|
|
|
|
flag2 NOT present
|
|
|
|
option2 maybe present with value of: Nothing
|
|
|
|
positional2 maybe present with value of: Nothing
|
|
|
|
option3 NOT present
|
|
|
|
positional3 NOT present
|
|
|
|
option present 2 times with value: some
|
|
|
|
An option: some
|
|
|
|
An option: other
|
|
|
|
positional present with value: value
|
|
|
|
subcmd NOT present
|
|
|
|
";
|
|
|
|
|
2019-10-02 13:27:19 +00:00
|
|
|
static F2OP: &str = "flag present 2 times
|
2016-05-09 03:20:50 +00:00
|
|
|
option present 1 times with value: some
|
|
|
|
An option: some
|
|
|
|
positional present with value: value
|
|
|
|
flag2 NOT present
|
|
|
|
option2 maybe present with value of: Nothing
|
|
|
|
positional2 maybe present with value of: Nothing
|
|
|
|
option3 NOT present
|
|
|
|
positional3 NOT present
|
|
|
|
option present 1 times with value: some
|
|
|
|
An option: some
|
|
|
|
positional present with value: value
|
|
|
|
subcmd NOT present
|
|
|
|
";
|
|
|
|
|
2019-10-02 13:27:19 +00:00
|
|
|
static FOP: &str = "flag present 1 times
|
2016-05-09 03:20:50 +00:00
|
|
|
option present 1 times with value: some
|
|
|
|
An option: some
|
|
|
|
positional present with value: value
|
|
|
|
flag2 NOT present
|
|
|
|
option2 maybe present with value of: Nothing
|
|
|
|
positional2 maybe present with value of: Nothing
|
|
|
|
option3 NOT present
|
|
|
|
positional3 NOT present
|
|
|
|
option present 1 times with value: some
|
|
|
|
An option: some
|
|
|
|
positional present with value: value
|
|
|
|
subcmd NOT present
|
|
|
|
";
|
|
|
|
|
2017-01-03 04:05:23 +00:00
|
|
|
pub fn check_complex_output(args: &str, out: &str) {
|
|
|
|
let mut w = vec![];
|
2020-02-04 08:10:53 +00:00
|
|
|
let matches = utils::complex_app().get_matches_from(args.split(' ').collect::<Vec<_>>());
|
2017-01-03 04:05:23 +00:00
|
|
|
if matches.is_present("flag") {
|
|
|
|
writeln!(w, "flag present {} times", matches.occurrences_of("flag")).unwrap();
|
|
|
|
} else {
|
|
|
|
writeln!(w, "flag NOT present").unwrap();
|
|
|
|
}
|
|
|
|
|
|
|
|
if matches.is_present("option") {
|
|
|
|
if let Some(v) = matches.value_of("option") {
|
2018-01-25 04:05:05 +00:00
|
|
|
writeln!(
|
|
|
|
w,
|
|
|
|
"option present {} times with value: {}",
|
|
|
|
matches.occurrences_of("option"),
|
|
|
|
v
|
2018-11-14 17:05:06 +00:00
|
|
|
)
|
|
|
|
.unwrap();
|
2017-01-03 04:05:23 +00:00
|
|
|
}
|
|
|
|
if let Some(ov) = matches.values_of("option") {
|
|
|
|
for o in ov {
|
|
|
|
writeln!(w, "An option: {}", o).unwrap();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
writeln!(w, "option NOT present").unwrap();
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(p) = matches.value_of("positional") {
|
|
|
|
writeln!(w, "positional present with value: {}", p).unwrap();
|
|
|
|
} else {
|
|
|
|
writeln!(w, "positional NOT present").unwrap();
|
|
|
|
}
|
|
|
|
|
|
|
|
if matches.is_present("flag2") {
|
|
|
|
writeln!(w, "flag2 present").unwrap();
|
2018-01-25 04:05:05 +00:00
|
|
|
writeln!(
|
|
|
|
w,
|
|
|
|
"option2 present with value of: {}",
|
|
|
|
matches.value_of("long-option-2").unwrap()
|
2018-11-14 17:05:06 +00:00
|
|
|
)
|
|
|
|
.unwrap();
|
2018-01-25 04:05:05 +00:00
|
|
|
writeln!(
|
|
|
|
w,
|
|
|
|
"positional2 present with value of: {}",
|
|
|
|
matches.value_of("positional2").unwrap()
|
2018-11-14 17:05:06 +00:00
|
|
|
)
|
|
|
|
.unwrap();
|
2017-01-03 04:05:23 +00:00
|
|
|
} else {
|
|
|
|
writeln!(w, "flag2 NOT present").unwrap();
|
2018-01-25 04:05:05 +00:00
|
|
|
writeln!(
|
|
|
|
w,
|
|
|
|
"option2 maybe present with value of: {}",
|
|
|
|
matches.value_of("long-option-2").unwrap_or("Nothing")
|
2018-11-14 17:05:06 +00:00
|
|
|
)
|
|
|
|
.unwrap();
|
2018-01-25 04:05:05 +00:00
|
|
|
writeln!(
|
|
|
|
w,
|
|
|
|
"positional2 maybe present with value of: {}",
|
|
|
|
matches.value_of("positional2").unwrap_or("Nothing")
|
2018-11-14 17:05:06 +00:00
|
|
|
)
|
|
|
|
.unwrap();
|
2017-01-03 04:05:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
let _ = match matches.value_of("Option3").unwrap_or("") {
|
|
|
|
"fast" => writeln!(w, "option3 present quickly"),
|
|
|
|
"slow" => writeln!(w, "option3 present slowly"),
|
2018-01-25 04:05:05 +00:00
|
|
|
_ => writeln!(w, "option3 NOT present"),
|
2017-01-03 04:05:23 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
let _ = match matches.value_of("positional3").unwrap_or("") {
|
|
|
|
"vi" => writeln!(w, "positional3 present in vi mode"),
|
|
|
|
"emacs" => writeln!(w, "positional3 present in emacs mode"),
|
2018-01-25 04:05:05 +00:00
|
|
|
_ => writeln!(w, "positional3 NOT present"),
|
2017-01-03 04:05:23 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
if matches.is_present("option") {
|
|
|
|
if let Some(v) = matches.value_of("option") {
|
2018-01-25 04:05:05 +00:00
|
|
|
writeln!(
|
|
|
|
w,
|
|
|
|
"option present {} times with value: {}",
|
|
|
|
matches.occurrences_of("option"),
|
|
|
|
v
|
2018-11-14 17:05:06 +00:00
|
|
|
)
|
|
|
|
.unwrap();
|
2017-01-03 04:05:23 +00:00
|
|
|
}
|
|
|
|
if let Some(ov) = matches.values_of("option") {
|
|
|
|
for o in ov {
|
|
|
|
writeln!(w, "An option: {}", o).unwrap();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
writeln!(w, "option NOT present").unwrap();
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(p) = matches.value_of("positional") {
|
|
|
|
writeln!(w, "positional present with value: {}", p).unwrap();
|
|
|
|
} else {
|
|
|
|
writeln!(w, "positional NOT present").unwrap();
|
|
|
|
}
|
|
|
|
if matches.is_present("subcmd") {
|
|
|
|
writeln!(w, "subcmd present").unwrap();
|
|
|
|
if let Some(matches) = matches.subcommand_matches("subcmd") {
|
|
|
|
if matches.is_present("flag") {
|
|
|
|
writeln!(w, "flag present {} times", matches.occurrences_of("flag")).unwrap();
|
|
|
|
} else {
|
|
|
|
writeln!(w, "flag NOT present").unwrap();
|
|
|
|
}
|
|
|
|
|
|
|
|
if matches.is_present("option") {
|
|
|
|
if let Some(v) = matches.value_of("option") {
|
|
|
|
writeln!(w, "scoption present with value: {}", v).unwrap();
|
|
|
|
}
|
|
|
|
if let Some(ov) = matches.values_of("option") {
|
|
|
|
for o in ov {
|
|
|
|
writeln!(w, "An scoption: {}", o).unwrap();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
writeln!(w, "scoption NOT present").unwrap();
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(p) = matches.value_of("scpositional") {
|
|
|
|
writeln!(w, "scpositional present with value: {}", p).unwrap();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
writeln!(w, "subcmd NOT present").unwrap();
|
|
|
|
}
|
|
|
|
|
|
|
|
let res = str::from_utf8(&w).unwrap();
|
|
|
|
assert_eq!(res, out);
|
|
|
|
}
|
|
|
|
|
2015-08-27 21:03:45 +00:00
|
|
|
#[test]
|
|
|
|
fn create_app() {
|
2018-01-25 04:05:05 +00:00
|
|
|
let _ = App::new("test")
|
|
|
|
.version("1.0")
|
|
|
|
.author("kevin")
|
|
|
|
.about("does awesome things")
|
|
|
|
.get_matches_from(vec![""]);
|
2015-08-27 21:03:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn add_multiple_arg() {
|
|
|
|
let _ = App::new("test")
|
2018-03-21 23:41:58 +00:00
|
|
|
.args(&[
|
2018-07-23 19:09:42 +00:00
|
|
|
Arg::with_name("test").short('s'),
|
|
|
|
Arg::with_name("test2").short('l'),
|
2018-01-25 04:05:05 +00:00
|
|
|
])
|
|
|
|
.get_matches_from(vec![""]);
|
2015-08-27 21:03:45 +00:00
|
|
|
}
|
2016-05-09 03:20:50 +00:00
|
|
|
#[test]
|
|
|
|
fn flag_x2_opt() {
|
2018-01-25 04:05:05 +00:00
|
|
|
check_complex_output(
|
|
|
|
"clap-test value -f -f -o some",
|
|
|
|
"flag present 2 times
|
2016-05-09 03:20:50 +00:00
|
|
|
option present 1 times with value: some
|
|
|
|
An option: some
|
|
|
|
positional present with value: value
|
|
|
|
flag2 NOT present
|
|
|
|
option2 maybe present with value of: Nothing
|
|
|
|
positional2 maybe present with value of: Nothing
|
|
|
|
option3 NOT present
|
|
|
|
positional3 NOT present
|
|
|
|
option present 1 times with value: some
|
|
|
|
An option: some
|
|
|
|
positional present with value: value
|
|
|
|
subcmd NOT present
|
2018-01-25 04:05:05 +00:00
|
|
|
",
|
|
|
|
);
|
2016-05-09 03:20:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2020-01-31 09:13:44 +00:00
|
|
|
fn long_opt_x2_pos() {
|
|
|
|
check_complex_output("clap-test value --option some --option other", O2P);
|
|
|
|
}
|
2016-05-09 03:20:50 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn long_opt_eq_x2_pos() {
|
2017-01-03 04:05:23 +00:00
|
|
|
check_complex_output("clap-test value --option=some --option=other", O2P);
|
2016-05-09 03:20:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2020-01-31 09:13:44 +00:00
|
|
|
fn short_opt_x2_pos() {
|
|
|
|
check_complex_output("clap-test value -o some -o other", O2P);
|
|
|
|
}
|
2016-05-09 03:20:50 +00:00
|
|
|
|
|
|
|
#[test]
|
2020-01-31 09:13:44 +00:00
|
|
|
fn short_opt_eq_x2_pos() {
|
|
|
|
check_complex_output("clap-test value -o=some -o=other", O2P);
|
|
|
|
}
|
2016-05-09 03:20:50 +00:00
|
|
|
|
|
|
|
#[test]
|
2020-01-31 09:13:44 +00:00
|
|
|
fn short_flag_x2_comb_short_opt_pos() {
|
|
|
|
check_complex_output("clap-test value -ff -o some", F2OP);
|
|
|
|
}
|
2016-05-09 03:20:50 +00:00
|
|
|
|
|
|
|
#[test]
|
2020-01-31 09:13:44 +00:00
|
|
|
fn short_flag_short_opt_pos() {
|
|
|
|
check_complex_output("clap-test value -f -o some", FOP);
|
|
|
|
}
|
2016-05-09 03:20:50 +00:00
|
|
|
|
|
|
|
#[test]
|
2020-01-31 09:13:44 +00:00
|
|
|
fn long_flag_long_opt_pos() {
|
|
|
|
check_complex_output("clap-test value --flag --option some", FOP);
|
|
|
|
}
|
2016-05-09 03:20:50 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn long_flag_long_opt_eq_pos() {
|
2017-01-03 04:05:23 +00:00
|
|
|
check_complex_output("clap-test value --flag --option=some", FOP);
|
2016-05-09 03:20:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn sc_long_flag_long_opt() {
|
2017-01-03 04:05:23 +00:00
|
|
|
check_complex_output("clap-test subcmd value --flag --option some", SCFOP);
|
2016-05-09 03:20:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn sc_long_flag_short_opt_pos() {
|
2017-01-03 04:05:23 +00:00
|
|
|
check_complex_output("clap-test subcmd value --flag -o some", SCFOP);
|
2016-05-09 03:20:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn sc_long_flag_long_opt_eq_pos() {
|
2017-01-03 04:05:23 +00:00
|
|
|
check_complex_output("clap-test subcmd value --flag --option=some", SCFOP);
|
2016-05-09 03:20:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn sc_short_flag_long_opt_pos() {
|
2017-01-03 04:05:23 +00:00
|
|
|
check_complex_output("clap-test subcmd value -f --option some", SCFOP);
|
2016-05-09 03:20:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn sc_short_flag_short_opt_pos() {
|
2017-01-03 04:05:23 +00:00
|
|
|
check_complex_output("clap-test subcmd value -f -o some", SCFOP);
|
2016-05-09 03:20:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn sc_short_flag_short_opt_eq_pos() {
|
2017-01-03 04:05:23 +00:00
|
|
|
check_complex_output("clap-test subcmd value -f -o=some", SCFOP);
|
2016-05-09 03:20:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn sc_short_flag_long_opt_eq_pos() {
|
2017-01-03 04:05:23 +00:00
|
|
|
check_complex_output("clap-test subcmd value -f --option=some", SCFOP);
|
2016-05-09 03:20:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn sc_short_flag_x2_comb_long_opt_pos() {
|
2017-01-03 04:05:23 +00:00
|
|
|
check_complex_output("clap-test subcmd value -ff --option some", SCF2OP);
|
2016-05-09 03:20:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn sc_short_flag_x2_comb_short_opt_pos() {
|
2017-01-03 04:05:23 +00:00
|
|
|
check_complex_output("clap-test subcmd value -ff -o some", SCF2OP);
|
2016-05-09 03:20:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn sc_short_flag_x2_comb_long_opt_eq_pos() {
|
2017-01-03 04:05:23 +00:00
|
|
|
check_complex_output("clap-test subcmd value -ff --option=some", SCF2OP);
|
2016-05-09 03:20:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn sc_short_flag_x2_comb_short_opt_eq_pos() {
|
2017-01-03 04:05:23 +00:00
|
|
|
check_complex_output("clap-test subcmd value -ff -o=some", SCF2OP);
|
2016-05-09 03:20:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn sc_long_flag_x2_long_opt_pos() {
|
2017-01-03 04:05:23 +00:00
|
|
|
check_complex_output("clap-test subcmd value --flag --flag --option some", SCF2OP);
|
2016-05-09 03:20:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn sc_long_flag_x2_short_opt_pos() {
|
2017-01-03 04:05:23 +00:00
|
|
|
check_complex_output("clap-test subcmd value --flag --flag -o some", SCF2OP);
|
2016-05-09 03:20:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn sc_long_flag_x2_short_opt_eq_pos() {
|
2017-01-03 04:05:23 +00:00
|
|
|
check_complex_output("clap-test subcmd value --flag --flag -o=some", SCF2OP);
|
2016-05-09 03:20:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn sc_long_flag_x2_long_opt_eq_pos() {
|
2017-01-03 04:05:23 +00:00
|
|
|
check_complex_output("clap-test subcmd value --flag --flag --option=some", SCF2OP);
|
2016-05-09 03:20:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn sc_short_flag_x2_long_opt_pos() {
|
2017-01-03 04:05:23 +00:00
|
|
|
check_complex_output("clap-test subcmd value -f -f --option some", SCF2OP);
|
2016-05-09 03:20:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn sc_short_flag_x2_short_opt_pos() {
|
2017-01-03 04:05:23 +00:00
|
|
|
check_complex_output("clap-test subcmd value -f -f -o some", SCF2OP);
|
2016-05-09 03:20:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn sc_short_flag_x2_short_opt_eq_pos() {
|
2017-01-03 04:05:23 +00:00
|
|
|
check_complex_output("clap-test subcmd value -f -f -o=some", SCF2OP);
|
2016-05-09 03:20:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn sc_short_flag_x2_long_opt_eq_pos() {
|
2017-01-03 04:05:23 +00:00
|
|
|
check_complex_output("clap-test subcmd value -f -f --option=some", SCF2OP);
|
2016-05-09 03:20:50 +00:00
|
|
|
}
|