2020-02-04 08:10:53 +00:00
|
|
|
mod utils;
|
2016-09-30 21:54:14 +00:00
|
|
|
|
2018-11-14 17:05:06 +00:00
|
|
|
use clap::{App, Arg};
|
2016-09-30 21:54:14 +00:00
|
|
|
|
2019-10-02 13:27:19 +00:00
|
|
|
static SC_VISIBLE_ALIAS_HELP: &str = "ct-test 1.2
|
2016-09-30 21:54:14 +00:00
|
|
|
Some help
|
|
|
|
|
|
|
|
USAGE:
|
2017-01-03 04:05:23 +00:00
|
|
|
ct test [FLAGS] [OPTIONS]
|
2016-10-03 15:01:04 +00:00
|
|
|
|
|
|
|
FLAGS:
|
2017-01-03 04:05:23 +00:00
|
|
|
-f, --flag [aliases: v_flg, flag2, flg3]
|
|
|
|
-h, --help Prints help information
|
|
|
|
-V, --version Prints version information
|
2016-09-30 21:54:14 +00:00
|
|
|
|
|
|
|
OPTIONS:
|
2016-10-04 13:54:45 +00:00
|
|
|
-o, --opt <opt> [aliases: visible]";
|
2016-09-30 21:54:14 +00:00
|
|
|
|
2019-10-02 13:27:19 +00:00
|
|
|
static SC_INVISIBLE_ALIAS_HELP: &str = "ct-test 1.2
|
2016-09-30 21:54:14 +00:00
|
|
|
Some help
|
|
|
|
|
|
|
|
USAGE:
|
2017-01-03 04:05:23 +00:00
|
|
|
ct test [FLAGS] [OPTIONS]
|
2016-10-03 15:01:04 +00:00
|
|
|
|
|
|
|
FLAGS:
|
2017-01-03 04:05:23 +00:00
|
|
|
-f, --flag
|
|
|
|
-h, --help Prints help information
|
|
|
|
-V, --version Prints version information
|
2016-09-30 21:54:14 +00:00
|
|
|
|
|
|
|
OPTIONS:
|
2016-10-04 13:54:45 +00:00
|
|
|
-o, --opt <opt> ";
|
2016-09-30 21:54:14 +00:00
|
|
|
|
|
|
|
#[test]
|
2016-10-03 15:01:04 +00:00
|
|
|
fn single_alias_of_option() {
|
2016-09-30 21:54:14 +00:00
|
|
|
let a = App::new("single_alias")
|
2018-01-25 04:05:05 +00:00
|
|
|
.arg(
|
2020-05-14 20:50:56 +00:00
|
|
|
Arg::new("alias")
|
2018-01-25 04:05:05 +00:00
|
|
|
.long("alias")
|
|
|
|
.takes_value(true)
|
2020-04-21 15:51:38 +00:00
|
|
|
.about("single alias")
|
2018-01-25 04:05:05 +00:00
|
|
|
.alias("new-opt"),
|
|
|
|
)
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec!["", "--new-opt", "cool"]);
|
2016-09-30 21:54:14 +00:00
|
|
|
assert!(a.is_ok());
|
|
|
|
let a = a.unwrap();
|
|
|
|
assert!(a.is_present("alias"));
|
|
|
|
assert_eq!(a.value_of("alias").unwrap(), "cool");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2016-10-03 15:01:04 +00:00
|
|
|
fn multiple_aliases_of_option() {
|
2018-01-25 04:05:05 +00:00
|
|
|
let a = App::new("multiple_aliases").arg(
|
2020-05-14 20:50:56 +00:00
|
|
|
Arg::new("aliases")
|
2016-09-30 21:54:14 +00:00
|
|
|
.long("aliases")
|
|
|
|
.takes_value(true)
|
2020-04-21 15:51:38 +00:00
|
|
|
.about("multiple aliases")
|
2019-10-02 13:27:19 +00:00
|
|
|
.aliases(&["alias1", "alias2", "alias3"]),
|
2018-01-25 04:05:05 +00:00
|
|
|
);
|
2018-08-02 03:13:51 +00:00
|
|
|
let long = a
|
|
|
|
.clone()
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec!["", "--aliases", "value"]);
|
2016-09-30 21:54:14 +00:00
|
|
|
assert!(long.is_ok());
|
|
|
|
let long = long.unwrap();
|
|
|
|
|
2018-08-02 03:13:51 +00:00
|
|
|
let als1 = a
|
|
|
|
.clone()
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec!["", "--alias1", "value"]);
|
2016-09-30 21:54:14 +00:00
|
|
|
assert!(als1.is_ok());
|
|
|
|
let als1 = als1.unwrap();
|
|
|
|
|
2018-08-02 03:13:51 +00:00
|
|
|
let als2 = a
|
|
|
|
.clone()
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec!["", "--alias2", "value"]);
|
2016-09-30 21:54:14 +00:00
|
|
|
assert!(als2.is_ok());
|
|
|
|
let als2 = als2.unwrap();
|
|
|
|
|
2018-08-02 03:13:51 +00:00
|
|
|
let als3 = a
|
|
|
|
.clone()
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec!["", "--alias3", "value"]);
|
2016-09-30 21:54:14 +00:00
|
|
|
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");
|
|
|
|
}
|
|
|
|
|
2016-10-03 15:01:04 +00:00
|
|
|
#[test]
|
|
|
|
fn single_alias_of_flag() {
|
|
|
|
let a = App::new("test")
|
2020-05-14 20:50:56 +00:00
|
|
|
.arg(Arg::new("flag").long("flag").alias("alias"))
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec!["", "--alias"]);
|
2016-10-03 15:01:04 +00:00
|
|
|
assert!(a.is_ok());
|
|
|
|
let a = a.unwrap();
|
|
|
|
assert!(a.is_present("flag"));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn multiple_aliases_of_flag() {
|
2020-05-14 20:50:56 +00:00
|
|
|
let a = App::new("test").arg(Arg::new("flag").long("flag").aliases(&[
|
2018-01-25 04:05:05 +00:00
|
|
|
"invisible",
|
|
|
|
"set",
|
|
|
|
"of",
|
|
|
|
"cool",
|
|
|
|
"aliases",
|
|
|
|
]));
|
2016-10-03 15:01:04 +00:00
|
|
|
|
2018-10-19 20:42:13 +00:00
|
|
|
let flag = a.clone().try_get_matches_from(vec!["", "--flag"]);
|
2016-10-03 15:01:04 +00:00
|
|
|
assert!(flag.is_ok());
|
|
|
|
let flag = flag.unwrap();
|
|
|
|
|
2018-10-19 20:42:13 +00:00
|
|
|
let inv = a.clone().try_get_matches_from(vec!["", "--invisible"]);
|
2016-10-03 15:01:04 +00:00
|
|
|
assert!(inv.is_ok());
|
|
|
|
let inv = inv.unwrap();
|
|
|
|
|
2018-10-19 20:42:13 +00:00
|
|
|
let cool = a.clone().try_get_matches_from(vec!["", "--cool"]);
|
2016-10-03 15:01:04 +00:00
|
|
|
assert!(cool.is_ok());
|
|
|
|
let cool = cool.unwrap();
|
|
|
|
|
2018-10-19 20:42:13 +00:00
|
|
|
let als = a.clone().try_get_matches_from(vec!["", "--aliases"]);
|
2016-10-03 15:01:04 +00:00
|
|
|
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"));
|
|
|
|
}
|
|
|
|
|
2016-09-30 21:54:14 +00:00
|
|
|
#[test]
|
|
|
|
fn alias_on_a_subcommand_option() {
|
|
|
|
let m = App::new("test")
|
2018-01-25 04:05:05 +00:00
|
|
|
.subcommand(
|
2018-10-19 20:42:13 +00:00
|
|
|
App::new("some").arg(
|
2020-05-14 20:50:56 +00:00
|
|
|
Arg::new("test")
|
2018-07-23 19:09:42 +00:00
|
|
|
.short('t')
|
2018-01-25 04:05:05 +00:00
|
|
|
.long("test")
|
|
|
|
.takes_value(true)
|
|
|
|
.alias("opt")
|
2020-04-21 15:51:38 +00:00
|
|
|
.about("testing testing"),
|
2018-01-25 04:05:05 +00:00
|
|
|
),
|
|
|
|
)
|
2020-05-14 20:50:56 +00:00
|
|
|
.arg(Arg::new("other").long("other").aliases(&["o1", "o2", "o3"]))
|
2018-01-25 04:05:05 +00:00
|
|
|
.get_matches_from(vec!["test", "some", "--opt", "awesome"]);
|
2016-09-30 21:54:14 +00:00
|
|
|
|
|
|
|
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() {
|
2018-01-25 04:05:05 +00:00
|
|
|
let app = App::new("ct").author("Salim Afiune").subcommand(
|
2018-10-19 20:42:13 +00:00
|
|
|
App::new("test")
|
2016-09-30 21:54:14 +00:00
|
|
|
.about("Some help")
|
2017-01-03 04:05:23 +00:00
|
|
|
.version("1.2")
|
2018-01-25 04:05:05 +00:00
|
|
|
.arg(
|
2020-05-14 20:50:56 +00:00
|
|
|
Arg::new("opt")
|
2018-01-25 04:05:05 +00:00
|
|
|
.long("opt")
|
2018-07-23 19:09:42 +00:00
|
|
|
.short('o')
|
2018-01-25 04:05:05 +00:00
|
|
|
.takes_value(true)
|
|
|
|
.aliases(&["invisible", "als1", "more"]),
|
|
|
|
)
|
2020-08-02 16:52:12 +00:00
|
|
|
.arg(Arg::from("-f, --flag").aliases(&["unseeable", "flg1", "anyway"])),
|
2018-01-25 04:05:05 +00:00
|
|
|
);
|
2020-02-04 08:10:53 +00:00
|
|
|
assert!(utils::compare_output(
|
2018-01-25 04:05:05 +00:00
|
|
|
app,
|
|
|
|
"ct test --help",
|
|
|
|
SC_INVISIBLE_ALIAS_HELP,
|
|
|
|
false
|
|
|
|
));
|
2016-09-30 21:54:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn visible_arg_aliases_help_output() {
|
2018-01-25 04:05:05 +00:00
|
|
|
let app = App::new("ct").author("Salim Afiune").subcommand(
|
2018-10-19 20:42:13 +00:00
|
|
|
App::new("test")
|
2016-09-30 21:54:14 +00:00
|
|
|
.about("Some help")
|
2017-01-03 04:05:23 +00:00
|
|
|
.version("1.2")
|
2018-01-25 04:05:05 +00:00
|
|
|
.arg(
|
2020-05-14 20:50:56 +00:00
|
|
|
Arg::new("opt")
|
2018-01-25 04:05:05 +00:00
|
|
|
.long("opt")
|
2018-07-23 19:09:42 +00:00
|
|
|
.short('o')
|
2018-01-25 04:05:05 +00:00
|
|
|
.takes_value(true)
|
|
|
|
.alias("invisible")
|
|
|
|
.visible_alias("visible"),
|
|
|
|
)
|
|
|
|
.arg(
|
2020-05-14 20:50:56 +00:00
|
|
|
Arg::new("flg")
|
2018-01-25 04:05:05 +00:00
|
|
|
.long("flag")
|
2018-07-23 19:09:42 +00:00
|
|
|
.short('f')
|
2018-01-25 04:05:05 +00:00
|
|
|
.visible_aliases(&["v_flg", "flag2", "flg3"]),
|
|
|
|
),
|
|
|
|
);
|
2020-02-04 08:10:53 +00:00
|
|
|
assert!(utils::compare_output(
|
2018-01-25 04:05:05 +00:00
|
|
|
app,
|
|
|
|
"ct test --help",
|
|
|
|
SC_VISIBLE_ALIAS_HELP,
|
|
|
|
false
|
|
|
|
));
|
2016-09-30 21:54:14 +00:00
|
|
|
}
|