From 5e20a29b4fdc8a2d442d1093681b396dcb4b816b Mon Sep 17 00:00:00 2001 From: Robin Krahl Date: Tue, 7 Jan 2020 11:18:04 +0000 Subject: Add structopt dependency in version 0.3.7 This patch series replaces argparse with structopt in the argument handling code. As a first step, we need structopt as a dependency. Import subrepo structopt/:structopt at efbdda4753592e27bc430fb01f7b9650b2f3174d Import subrepo bitflags/:bitflags at 30668016aca6bd3b02c766e8347e0b4080d4c296 Import subrepo clap/:clap at 784524f7eb193e35f81082cc69454c8c21b948f7 Import subrepo heck/:heck at 093d56fbf001e1506e56dbfa38631d99b1066df1 Import subrepo proc-macro-error/:proc-macro-error at 6c4cfe79a622c5de8ae68557993542be46eacae2 Import subrepo proc-macro2/:proc-macro2 at d5d48eddca4566e5438e8a2cbed4a74e049544de Import subrepo quote/:quote at 727436c6c137b20f0f34dde5d8fda2679b9747ad Import subrepo rustversion/:rustversion at 0c5663313516263059ce9059ef81fc7a1cf655ca Import subrepo syn-mid/:syn-mid at 5d3d85414a9e6674e1857ec22a87b96e04a6851a Import subrepo syn/:syn at e87c27e87f6f4ef8919d0372bdb056d53ef0d8f3 Import subrepo textwrap/:textwrap at abcd618beae3f74841032aa5b53c1086b0a57ca2 Import subrepo unicode-segmentation/:unicode-segmentation at 637c9874c4fe0c205ff27787faf150a40295c6c3 Import subrepo unicode-width/:unicode-width at 3033826f8bf05e82724140a981d5941e48fce393 Import subrepo unicode-xid/:unicode-xid at 4baae9fffb156ba229665b972a9cd5991787ceb7 --- clap/tests/arg_aliases.rs | 200 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 200 insertions(+) create mode 100644 clap/tests/arg_aliases.rs (limited to 'clap/tests/arg_aliases.rs') diff --git a/clap/tests/arg_aliases.rs b/clap/tests/arg_aliases.rs new file mode 100644 index 0000000..77bcd17 --- /dev/null +++ b/clap/tests/arg_aliases.rs @@ -0,0 +1,200 @@ +extern crate clap; +extern crate regex; + +include!("../clap-test.rs"); + +use clap::{App, Arg, SubCommand}; + +static SC_VISIBLE_ALIAS_HELP: &'static str = "ct-test 1.2 +Some help + +USAGE: + ct test [FLAGS] [OPTIONS] + +FLAGS: + -f, --flag [aliases: v_flg, flag2, flg3] + -h, --help Prints help information + -V, --version Prints version information + +OPTIONS: + -o, --opt [aliases: visible]"; + +static SC_INVISIBLE_ALIAS_HELP: &'static str = "ct-test 1.2 +Some help + +USAGE: + ct test [FLAGS] [OPTIONS] + +FLAGS: + -f, --flag + -h, --help Prints help information + -V, --version Prints version information + +OPTIONS: + -o, --opt "; + +#[test] +fn single_alias_of_option() { + let a = App::new("single_alias") + .arg(Arg::with_name("alias") + .long("alias") + .takes_value(true) + .help("single alias") + .alias("new-opt")) + .get_matches_from_safe(vec![ + "", "--new-opt", "cool" + ]); + assert!(a.is_ok()); + let a = a.unwrap(); + assert!(a.is_present("alias")); + assert_eq!(a.value_of("alias").unwrap(), "cool"); +} + +#[test] +fn multiple_aliases_of_option() { + let a = App::new("multiple_aliases") + .arg(Arg::with_name("aliases") + .long("aliases") + .takes_value(true) + .help("multiple aliases") + .aliases(&[ + "alias1", + "alias2", + "alias3" + ])); + let long = a.clone().get_matches_from_safe(vec![ + "", "--aliases", "value" + ]); + assert!(long.is_ok()); + let long = long.unwrap(); + + let als1 = a.clone().get_matches_from_safe(vec![ + "", "--alias1", "value" + ]); + assert!(als1.is_ok()); + let als1 = als1.unwrap(); + + let als2 = a.clone().get_matches_from_safe(vec![ + "", "--alias2", "value" + ]); + assert!(als2.is_ok()); + let als2 = als2.unwrap(); + + let als3 = a.clone().get_matches_from_safe(vec![ + "", "--alias3", "value" + ]); + assert!(als3.is_ok()); + let als3 = als3.unwrap(); + + assert!(long.is_present("aliases")); + assert!(als1.is_present("aliases")); + assert!(als2.is_present("aliases")); + assert!(als3.is_present("aliases")); + assert_eq!(long.value_of("aliases").unwrap(), "value"); + assert_eq!(als1.value_of("aliases").unwrap(), "value"); + assert_eq!(als2.value_of("aliases").unwrap(), "value"); + assert_eq!(als3.value_of("aliases").unwrap(), "value"); +} + +#[test] +fn single_alias_of_flag() { + let a = App::new("test") + .arg(Arg::with_name("flag") + .long("flag") + .alias("alias")) + .get_matches_from_safe(vec!["", "--alias"]); + assert!(a.is_ok()); + let a = a.unwrap(); + assert!(a.is_present("flag")); +} + +#[test] +fn multiple_aliases_of_flag() { + let a = App::new("test") + .arg(Arg::with_name("flag") + .long("flag") + .aliases(&["invisible", + "set", "of", + "cool", "aliases"])); + + let flag = a.clone().get_matches_from_safe(vec!["", "--flag"]); + assert!(flag.is_ok()); + let flag = flag.unwrap(); + + let inv = a.clone().get_matches_from_safe(vec!["", "--invisible"]); + assert!(inv.is_ok()); + let inv = inv.unwrap(); + + let cool = a.clone().get_matches_from_safe(vec!["", "--cool"]); + assert!(cool.is_ok()); + let cool = cool.unwrap(); + + let als = a.clone().get_matches_from_safe(vec!["", "--aliases"]); + assert!(als.is_ok()); + let als = als.unwrap(); + + assert!(flag.is_present("flag")); + assert!(inv.is_present("flag")); + assert!(cool.is_present("flag")); + assert!(als.is_present("flag")); +} + +#[test] +fn alias_on_a_subcommand_option() { + let m = App::new("test") + .subcommand(SubCommand::with_name("some") + .arg(Arg::with_name("test") + .short("t") + .long("test") + .takes_value(true) + .alias("opt") + .help("testing testing"))) + .arg(Arg::with_name("other") + .long("other") + .aliases(&["o1", "o2", "o3"])) + .get_matches_from(vec![ + "test", "some", "--opt", "awesome" + ]); + + assert!(m.subcommand_matches("some").is_some()); + let sub_m = m.subcommand_matches("some").unwrap(); + assert!(sub_m.is_present("test")); + assert_eq!(sub_m.value_of("test").unwrap(), "awesome"); +} + +#[test] +fn invisible_arg_aliases_help_output() { + let app = App::new("ct") + .author("Salim Afiune") + .subcommand(SubCommand::with_name("test") + .about("Some help") + .version("1.2") + .arg(Arg::with_name("opt") + .long("opt") + .short("o") + .takes_value(true) + .aliases(&["invisible", "als1", "more"])) + .arg(Arg::from_usage("-f, --flag") + .aliases(&["invisible", "flg1", "anyway"]))); + assert!(test::compare_output(app, "ct test --help", SC_INVISIBLE_ALIAS_HELP, false)); +} + +#[test] +fn visible_arg_aliases_help_output() { + let app = App::new("ct") + .author("Salim Afiune") + .subcommand(SubCommand::with_name("test") + .about("Some help") + .version("1.2") + .arg(Arg::with_name("opt") + .long("opt") + .short("o") + .takes_value(true) + .alias("invisible") + .visible_alias("visible")) + .arg(Arg::with_name("flg") + .long("flag") + .short("f") + .visible_aliases(&["v_flg", "flag2", "flg3"]))); + assert!(test::compare_output(app, "ct test --help", SC_VISIBLE_ALIAS_HELP, false)); +} -- cgit v1.2.1