2022-06-07 21:21:12 +00:00
|
|
|
use super::utils;
|
2016-09-30 21:54:14 +00:00
|
|
|
|
2022-06-10 01:03:28 +00:00
|
|
|
use clap::{arg, Arg, ArgAction, Command};
|
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:
|
2021-10-11 22:01:33 +00:00
|
|
|
ct test [OPTIONS]
|
2016-09-30 21:54:14 +00:00
|
|
|
|
|
|
|
OPTIONS:
|
2021-10-11 22:01:33 +00:00
|
|
|
-f, --flag [aliases: v_flg, flag2, flg3]
|
|
|
|
-h, --help Print help information
|
2021-09-24 15:58:39 +00:00
|
|
|
-o, --opt <opt> [aliases: visible]
|
2021-10-11 22:01:33 +00:00
|
|
|
-V, --version Print version information
|
2021-09-24 15:58:39 +00:00
|
|
|
";
|
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:
|
2021-10-11 22:01:33 +00:00
|
|
|
ct test [OPTIONS]
|
2016-09-30 21:54:14 +00:00
|
|
|
|
|
|
|
OPTIONS:
|
2021-10-11 22:01:33 +00:00
|
|
|
-f, --flag
|
|
|
|
-h, --help Print help information
|
2021-09-24 15:58:39 +00:00
|
|
|
-o, --opt <opt>
|
2021-10-11 22:01:33 +00:00
|
|
|
-V, --version Print version information
|
2021-09-24 15:58:39 +00:00
|
|
|
";
|
2016-09-30 21:54:14 +00:00
|
|
|
|
|
|
|
#[test]
|
2016-10-03 15:01:04 +00:00
|
|
|
fn single_alias_of_option() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let a = Command::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)
|
2021-11-18 16:17:15 +00:00
|
|
|
.help("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"]);
|
2021-12-27 19:57:38 +00:00
|
|
|
assert!(a.is_ok(), "{}", a.unwrap_err());
|
2016-09-30 21:54:14 +00:00
|
|
|
let a = a.unwrap();
|
2022-06-10 01:03:28 +00:00
|
|
|
assert!(a.contains_id("alias"));
|
2022-05-24 15:16:50 +00:00
|
|
|
assert_eq!(
|
|
|
|
a.get_one::<String>("alias").map(|v| v.as_str()).unwrap(),
|
|
|
|
"cool"
|
|
|
|
);
|
2016-09-30 21:54:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2016-10-03 15:01:04 +00:00
|
|
|
fn multiple_aliases_of_option() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let a = Command::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)
|
2021-11-18 16:17:15 +00:00
|
|
|
.help("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"]);
|
2021-12-27 19:57:38 +00:00
|
|
|
assert!(long.is_ok(), "{}", long.unwrap_err());
|
2016-09-30 21:54:14 +00:00
|
|
|
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"]);
|
2021-12-27 19:57:38 +00:00
|
|
|
assert!(als1.is_ok(), "{}", als1.unwrap_err());
|
2016-09-30 21:54:14 +00:00
|
|
|
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"]);
|
2021-12-27 19:57:38 +00:00
|
|
|
assert!(als2.is_ok(), "{}", als2.unwrap_err());
|
2016-09-30 21:54:14 +00:00
|
|
|
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"]);
|
2021-12-27 19:57:38 +00:00
|
|
|
assert!(als3.is_ok(), "{}", als3.unwrap_err());
|
2016-09-30 21:54:14 +00:00
|
|
|
let als3 = als3.unwrap();
|
|
|
|
|
2022-06-10 01:03:28 +00:00
|
|
|
assert!(long.contains_id("aliases"));
|
|
|
|
assert!(als1.contains_id("aliases"));
|
|
|
|
assert!(als2.contains_id("aliases"));
|
|
|
|
assert!(als3.contains_id("aliases"));
|
2022-05-24 15:16:50 +00:00
|
|
|
assert_eq!(
|
|
|
|
long.get_one::<String>("aliases")
|
|
|
|
.map(|v| v.as_str())
|
|
|
|
.unwrap(),
|
|
|
|
"value"
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
als1.get_one::<String>("aliases")
|
|
|
|
.map(|v| v.as_str())
|
|
|
|
.unwrap(),
|
|
|
|
"value"
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
als2.get_one::<String>("aliases")
|
|
|
|
.map(|v| v.as_str())
|
|
|
|
.unwrap(),
|
|
|
|
"value"
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
als3.get_one::<String>("aliases")
|
|
|
|
.map(|v| v.as_str())
|
|
|
|
.unwrap(),
|
|
|
|
"value"
|
|
|
|
);
|
2016-09-30 21:54:14 +00:00
|
|
|
}
|
|
|
|
|
2016-10-03 15:01:04 +00:00
|
|
|
#[test]
|
|
|
|
fn single_alias_of_flag() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let a = Command::new("test")
|
2022-06-10 01:03:28 +00:00
|
|
|
.arg(
|
|
|
|
Arg::new("flag")
|
|
|
|
.long("flag")
|
|
|
|
.alias("alias")
|
|
|
|
.action(ArgAction::SetTrue),
|
|
|
|
)
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec!["", "--alias"]);
|
2021-12-27 19:57:38 +00:00
|
|
|
assert!(a.is_ok(), "{}", a.unwrap_err());
|
2016-10-03 15:01:04 +00:00
|
|
|
let a = a.unwrap();
|
2022-06-10 01:03:28 +00:00
|
|
|
assert!(*a.get_one::<bool>("flag").expect("defaulted by clap"));
|
2016-10-03 15:01:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn multiple_aliases_of_flag() {
|
2022-06-10 01:03:28 +00:00
|
|
|
let a = Command::new("test").arg(
|
|
|
|
Arg::new("flag")
|
|
|
|
.long("flag")
|
|
|
|
.aliases(&["invisible", "set", "of", "cool", "aliases"])
|
|
|
|
.action(ArgAction::SetTrue),
|
|
|
|
);
|
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"]);
|
2021-12-27 19:57:38 +00:00
|
|
|
assert!(flag.is_ok(), "{}", flag.unwrap_err());
|
2016-10-03 15:01:04 +00:00
|
|
|
let flag = flag.unwrap();
|
|
|
|
|
2018-10-19 20:42:13 +00:00
|
|
|
let inv = a.clone().try_get_matches_from(vec!["", "--invisible"]);
|
2021-12-27 19:57:38 +00:00
|
|
|
assert!(inv.is_ok(), "{}", inv.unwrap_err());
|
2016-10-03 15:01:04 +00:00
|
|
|
let inv = inv.unwrap();
|
|
|
|
|
2018-10-19 20:42:13 +00:00
|
|
|
let cool = a.clone().try_get_matches_from(vec!["", "--cool"]);
|
2021-12-27 19:57:38 +00:00
|
|
|
assert!(cool.is_ok(), "{}", cool.unwrap_err());
|
2016-10-03 15:01:04 +00:00
|
|
|
let cool = cool.unwrap();
|
|
|
|
|
2018-10-19 20:42:13 +00:00
|
|
|
let als = a.clone().try_get_matches_from(vec!["", "--aliases"]);
|
2021-12-27 19:57:38 +00:00
|
|
|
assert!(als.is_ok(), "{}", als.unwrap_err());
|
2016-10-03 15:01:04 +00:00
|
|
|
let als = als.unwrap();
|
|
|
|
|
2022-06-10 01:03:28 +00:00
|
|
|
assert!(*flag.get_one::<bool>("flag").expect("defaulted by clap"));
|
|
|
|
assert!(*inv.get_one::<bool>("flag").expect("defaulted by clap"));
|
|
|
|
assert!(*cool.get_one::<bool>("flag").expect("defaulted by clap"));
|
|
|
|
assert!(*als.get_one::<bool>("flag").expect("defaulted by clap"));
|
2016-10-03 15:01:04 +00:00
|
|
|
}
|
|
|
|
|
2016-09-30 21:54:14 +00:00
|
|
|
#[test]
|
|
|
|
fn alias_on_a_subcommand_option() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let m = Command::new("test")
|
2018-01-25 04:05:05 +00:00
|
|
|
.subcommand(
|
2022-02-12 03:48:29 +00:00
|
|
|
Command::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")
|
2021-11-18 16:17:15 +00:00
|
|
|
.help("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"]))
|
2021-12-27 18:56:12 +00:00
|
|
|
.try_get_matches_from(vec!["test", "some", "--opt", "awesome"])
|
|
|
|
.unwrap();
|
2016-09-30 21:54:14 +00:00
|
|
|
|
|
|
|
assert!(m.subcommand_matches("some").is_some());
|
|
|
|
let sub_m = m.subcommand_matches("some").unwrap();
|
2022-06-10 01:03:28 +00:00
|
|
|
assert!(sub_m.contains_id("test"));
|
2022-05-24 15:16:50 +00:00
|
|
|
assert_eq!(
|
|
|
|
sub_m.get_one::<String>("test").map(|v| v.as_str()).unwrap(),
|
|
|
|
"awesome"
|
|
|
|
);
|
2016-09-30 21:54:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn invisible_arg_aliases_help_output() {
|
2022-02-14 21:47:20 +00:00
|
|
|
let cmd = Command::new("ct").author("Salim Afiune").subcommand(
|
2022-02-12 03:48:29 +00:00
|
|
|
Command::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"]),
|
|
|
|
)
|
2021-11-19 20:33:11 +00:00
|
|
|
.arg(arg!(-f - -flag).aliases(&["unseeable", "flg1", "anyway"])),
|
2018-01-25 04:05:05 +00:00
|
|
|
);
|
2022-04-29 20:32:25 +00:00
|
|
|
utils::assert_output(cmd, "ct test --help", SC_INVISIBLE_ALIAS_HELP, false);
|
2016-09-30 21:54:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn visible_arg_aliases_help_output() {
|
2022-02-14 21:47:20 +00:00
|
|
|
let cmd = Command::new("ct").author("Salim Afiune").subcommand(
|
2022-02-12 03:48:29 +00:00
|
|
|
Command::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"]),
|
|
|
|
),
|
|
|
|
);
|
2022-04-29 20:32:25 +00:00
|
|
|
utils::assert_output(cmd, "ct test --help", SC_VISIBLE_ALIAS_HELP, false);
|
2016-09-30 21:54:14 +00:00
|
|
|
}
|