clap/tests/builder/arg_aliases_short.rs

194 lines
5.7 KiB
Rust
Raw Normal View History

use crate::utils;
2022-02-12 03:48:29 +00:00
use clap::{arg, Arg, Command};
static SC_VISIBLE_ALIAS_HELP: &str = "ct-test 1.2
Some help
USAGE:
ct test [OPTIONS]
OPTIONS:
-f, --flag [aliases: flag1] [short aliases: a, b, 🦆]
-h, --help Print help information
-o, --opt <opt> [short aliases: v]
-V, --version Print version information
";
static SC_INVISIBLE_ALIAS_HELP: &str = "ct-test 1.2
Some help
USAGE:
ct test [OPTIONS]
OPTIONS:
-f, --flag
-h, --help Print help information
-o, --opt <opt>
-V, --version Print version information
";
#[test]
fn single_short_alias_of_option() {
2022-02-12 03:48:29 +00:00
let a = Command::new("single_alias")
.arg(
Arg::new("alias")
.long("alias")
.takes_value(true)
.help("single short alias")
.short_alias('a'),
)
.try_get_matches_from(vec!["", "-a", "cool"]);
2021-12-27 19:57:38 +00:00
assert!(a.is_ok(), "{}", a.unwrap_err());
let a = a.unwrap();
assert!(a.is_present("alias"));
assert_eq!(a.value_of("alias").unwrap(), "cool");
}
#[test]
fn multiple_short_aliases_of_option() {
2022-02-12 03:48:29 +00:00
let a = Command::new("multiple_aliases").arg(
Arg::new("aliases")
.long("aliases")
.takes_value(true)
.help("multiple aliases")
.short_aliases(&['1', '2', '3']),
);
let long = a
.clone()
.try_get_matches_from(vec!["", "--aliases", "value"]);
2021-12-27 19:57:38 +00:00
assert!(long.is_ok(), "{}", long.unwrap_err());
let long = long.unwrap();
let als1 = a.clone().try_get_matches_from(vec!["", "-1", "value"]);
2021-12-27 19:57:38 +00:00
assert!(als1.is_ok(), "{}", als1.unwrap_err());
let als1 = als1.unwrap();
let als2 = a.clone().try_get_matches_from(vec!["", "-2", "value"]);
2021-12-27 19:57:38 +00:00
assert!(als2.is_ok(), "{}", als2.unwrap_err());
let als2 = als2.unwrap();
let als3 = a.clone().try_get_matches_from(vec!["", "-3", "value"]);
2021-12-27 19:57:38 +00:00
assert!(als3.is_ok(), "{}", als3.unwrap_err());
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() {
2022-02-12 03:48:29 +00:00
let a = Command::new("test")
.arg(Arg::new("flag").long("flag").short_alias('f'))
.try_get_matches_from(vec!["", "-f"]);
2021-12-27 19:57:38 +00:00
assert!(a.is_ok(), "{}", a.unwrap_err());
let a = a.unwrap();
assert!(a.is_present("flag"));
}
#[test]
fn multiple_short_aliases_of_flag() {
2022-02-12 03:48:29 +00:00
let a = Command::new("test").arg(
Arg::new("flag")
.long("flag")
.short_aliases(&['a', 'b', 'c', 'd', 'e']),
);
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());
let flag = flag.unwrap();
let als1 = a.clone().try_get_matches_from(vec!["", "-a"]);
2021-12-27 19:57:38 +00:00
assert!(als1.is_ok(), "{}", als1.unwrap_err());
let als1 = als1.unwrap();
let als2 = a.clone().try_get_matches_from(vec!["", "-b"]);
2021-12-27 19:57:38 +00:00
assert!(als2.is_ok(), "{}", als2.unwrap_err());
let als2 = als2.unwrap();
let als3 = a.clone().try_get_matches_from(vec!["", "-c"]);
2021-12-27 19:57:38 +00:00
assert!(als3.is_ok(), "{}", als3.unwrap_err());
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() {
2022-02-12 03:48:29 +00:00
let m = Command::new("test")
.subcommand(
2022-02-12 03:48:29 +00:00
Command::new("some").arg(
Arg::new("test")
.short('t')
.long("test")
.takes_value(true)
.short_alias('o')
.help("testing testing"),
),
)
.arg(
Arg::new("other")
.long("other")
.short_aliases(&['1', '2', '3']),
)
.try_get_matches_from(vec!["test", "some", "-o", "awesome"])
.unwrap();
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() {
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")
.about("Some help")
.version("1.2")
.arg(
Arg::new("opt")
.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'])),
);
2022-04-29 20:32:25 +00:00
utils::assert_output(cmd, "ct test --help", SC_INVISIBLE_ALIAS_HELP, false);
}
#[test]
fn visible_short_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")
.about("Some help")
.version("1.2")
.arg(
Arg::new("opt")
.long("opt")
.short('o')
.takes_value(true)
.short_alias('i')
.visible_short_alias('v'),
)
.arg(
Arg::new("flg")
.long("flag")
.short('f')
.visible_alias("flag1")
.visible_short_aliases(&['a', 'b', '🦆']),
),
);
2022-04-29 20:32:25 +00:00
utils::assert_output(cmd, "ct test --help", SC_VISIBLE_ALIAS_HELP, false);
}