2021-11-30 15:31:19 +00:00
|
|
|
use crate::utils;
|
2020-05-03 06:13:24 +00:00
|
|
|
|
2021-11-19 20:33:11 +00:00
|
|
|
use clap::{arg, App, Arg};
|
2020-05-03 06:13:24 +00:00
|
|
|
|
|
|
|
static SC_VISIBLE_ALIAS_HELP: &str = "ct-test 1.2
|
|
|
|
Some help
|
|
|
|
|
|
|
|
USAGE:
|
2021-10-11 22:01:33 +00:00
|
|
|
ct test [OPTIONS]
|
2020-05-03 06:13:24 +00:00
|
|
|
|
|
|
|
OPTIONS:
|
2021-10-11 22:01:33 +00:00
|
|
|
-f, --flag [aliases: flag1] [short aliases: a, b, 🦆]
|
|
|
|
-h, --help Print help information
|
2021-09-24 15:58:39 +00:00
|
|
|
-o, --opt <opt> [short aliases: v]
|
2021-10-11 22:01:33 +00:00
|
|
|
-V, --version Print version information
|
2021-09-24 15:58:39 +00:00
|
|
|
";
|
2020-05-03 06:13:24 +00:00
|
|
|
|
|
|
|
static SC_INVISIBLE_ALIAS_HELP: &str = "ct-test 1.2
|
|
|
|
Some help
|
|
|
|
|
|
|
|
USAGE:
|
2021-10-11 22:01:33 +00:00
|
|
|
ct test [OPTIONS]
|
2020-05-03 06:13:24 +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
|
|
|
";
|
2020-05-03 06:13:24 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn single_short_alias_of_option() {
|
|
|
|
let a = App::new("single_alias")
|
|
|
|
.arg(
|
2020-05-14 20:50:56 +00:00
|
|
|
Arg::new("alias")
|
2020-05-03 06:13:24 +00:00
|
|
|
.long("alias")
|
|
|
|
.takes_value(true)
|
2021-11-18 16:17:15 +00:00
|
|
|
.help("single short alias")
|
2020-05-03 06:13:24 +00:00
|
|
|
.short_alias('a'),
|
|
|
|
)
|
|
|
|
.try_get_matches_from(vec!["", "-a", "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_short_aliases_of_option() {
|
|
|
|
let a = App::new("multiple_aliases").arg(
|
2020-05-14 20:50:56 +00:00
|
|
|
Arg::new("aliases")
|
2020-05-03 06:13:24 +00:00
|
|
|
.long("aliases")
|
|
|
|
.takes_value(true)
|
2021-11-18 16:17:15 +00:00
|
|
|
.help("multiple aliases")
|
2020-05-03 06:13:24 +00:00
|
|
|
.short_aliases(&['1', '2', '3']),
|
|
|
|
);
|
|
|
|
let long = a
|
|
|
|
.clone()
|
|
|
|
.try_get_matches_from(vec!["", "--aliases", "value"]);
|
|
|
|
assert!(long.is_ok());
|
|
|
|
let long = long.unwrap();
|
|
|
|
|
|
|
|
let als1 = a.clone().try_get_matches_from(vec!["", "-1", "value"]);
|
|
|
|
assert!(als1.is_ok());
|
|
|
|
let als1 = als1.unwrap();
|
|
|
|
|
|
|
|
let als2 = a.clone().try_get_matches_from(vec!["", "-2", "value"]);
|
|
|
|
assert!(als2.is_ok());
|
|
|
|
let als2 = als2.unwrap();
|
|
|
|
|
|
|
|
let als3 = a.clone().try_get_matches_from(vec!["", "-3", "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_short_alias_of_flag() {
|
|
|
|
let a = App::new("test")
|
2020-05-14 20:50:56 +00:00
|
|
|
.arg(Arg::new("flag").long("flag").short_alias('f'))
|
2020-05-03 06:13:24 +00:00
|
|
|
.try_get_matches_from(vec!["", "-f"]);
|
|
|
|
assert!(a.is_ok());
|
|
|
|
let a = a.unwrap();
|
|
|
|
assert!(a.is_present("flag"));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn multiple_short_aliases_of_flag() {
|
|
|
|
let a = App::new("test").arg(
|
2020-05-14 20:50:56 +00:00
|
|
|
Arg::new("flag")
|
2020-05-03 06:13:24 +00:00
|
|
|
.long("flag")
|
|
|
|
.short_aliases(&['a', 'b', 'c', 'd', 'e']),
|
|
|
|
);
|
|
|
|
|
|
|
|
let flag = a.clone().try_get_matches_from(vec!["", "--flag"]);
|
|
|
|
assert!(flag.is_ok());
|
|
|
|
let flag = flag.unwrap();
|
|
|
|
|
|
|
|
let als1 = a.clone().try_get_matches_from(vec!["", "-a"]);
|
|
|
|
assert!(als1.is_ok());
|
|
|
|
let als1 = als1.unwrap();
|
|
|
|
|
|
|
|
let als2 = a.clone().try_get_matches_from(vec!["", "-b"]);
|
|
|
|
assert!(als2.is_ok());
|
|
|
|
let als2 = als2.unwrap();
|
|
|
|
|
|
|
|
let als3 = a.clone().try_get_matches_from(vec!["", "-c"]);
|
|
|
|
assert!(als3.is_ok());
|
|
|
|
let als3 = als3.unwrap();
|
|
|
|
|
|
|
|
assert!(flag.is_present("flag"));
|
|
|
|
assert!(als1.is_present("flag"));
|
|
|
|
assert!(als2.is_present("flag"));
|
|
|
|
assert!(als3.is_present("flag"));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn short_alias_on_a_subcommand_option() {
|
|
|
|
let m = App::new("test")
|
|
|
|
.subcommand(
|
|
|
|
App::new("some").arg(
|
2020-05-14 20:50:56 +00:00
|
|
|
Arg::new("test")
|
2020-05-03 06:13:24 +00:00
|
|
|
.short('t')
|
|
|
|
.long("test")
|
|
|
|
.takes_value(true)
|
|
|
|
.short_alias('o')
|
2021-11-18 16:17:15 +00:00
|
|
|
.help("testing testing"),
|
2020-05-03 06:13:24 +00:00
|
|
|
),
|
|
|
|
)
|
|
|
|
.arg(
|
2020-05-14 20:50:56 +00:00
|
|
|
Arg::new("other")
|
2020-05-03 06:13:24 +00:00
|
|
|
.long("other")
|
|
|
|
.short_aliases(&['1', '2', '3']),
|
|
|
|
)
|
2021-12-27 18:56:12 +00:00
|
|
|
.try_get_matches_from(vec!["test", "some", "-o", "awesome"])
|
|
|
|
.unwrap();
|
2020-05-03 06:13:24 +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_short_arg_aliases_help_output() {
|
|
|
|
let app = App::new("ct").author("Salim Afiune").subcommand(
|
|
|
|
App::new("test")
|
|
|
|
.about("Some help")
|
|
|
|
.version("1.2")
|
|
|
|
.arg(
|
2020-05-14 20:50:56 +00:00
|
|
|
Arg::new("opt")
|
2020-05-03 06:13:24 +00:00
|
|
|
.long("opt")
|
|
|
|
.short('o')
|
|
|
|
.takes_value(true)
|
|
|
|
.short_aliases(&['a', 'b', 'c']),
|
|
|
|
)
|
2021-11-19 20:33:11 +00:00
|
|
|
.arg(arg!(-f - -flag).short_aliases(&['x', 'y', 'z'])),
|
2020-05-03 06:13:24 +00:00
|
|
|
);
|
|
|
|
assert!(utils::compare_output(
|
|
|
|
app,
|
|
|
|
"ct test --help",
|
|
|
|
SC_INVISIBLE_ALIAS_HELP,
|
|
|
|
false
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn visible_short_arg_aliases_help_output() {
|
|
|
|
let app = App::new("ct").author("Salim Afiune").subcommand(
|
|
|
|
App::new("test")
|
|
|
|
.about("Some help")
|
|
|
|
.version("1.2")
|
|
|
|
.arg(
|
2020-05-14 20:50:56 +00:00
|
|
|
Arg::new("opt")
|
2020-05-03 06:13:24 +00:00
|
|
|
.long("opt")
|
|
|
|
.short('o')
|
|
|
|
.takes_value(true)
|
|
|
|
.short_alias('i')
|
|
|
|
.visible_short_alias('v'),
|
|
|
|
)
|
|
|
|
.arg(
|
2020-05-14 20:50:56 +00:00
|
|
|
Arg::new("flg")
|
2020-05-03 06:13:24 +00:00
|
|
|
.long("flag")
|
|
|
|
.short('f')
|
|
|
|
.visible_alias("flag1")
|
|
|
|
.visible_short_aliases(&['a', 'b', '🦆']),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
assert!(utils::compare_output(
|
|
|
|
app,
|
|
|
|
"ct test --help",
|
|
|
|
SC_VISIBLE_ALIAS_HELP,
|
|
|
|
false
|
|
|
|
));
|
|
|
|
}
|