2021-11-30 15:31:19 +00:00
|
|
|
use crate::utils;
|
2020-07-09 01:37:56 +00:00
|
|
|
|
2022-02-12 03:48:29 +00:00
|
|
|
use clap::{arg, error::ErrorKind, Arg, Command};
|
2020-06-13 15:32:24 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn flag_subcommand_normal() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let matches = Command::new("test")
|
2020-06-13 15:32:24 +00:00
|
|
|
.subcommand(
|
2022-02-12 03:48:29 +00:00
|
|
|
Command::new("some").short_flag('S').long_flag("some").arg(
|
2020-06-13 15:32:24 +00:00
|
|
|
Arg::new("test")
|
|
|
|
.short('t')
|
|
|
|
.long("test")
|
2021-11-18 16:17:15 +00:00
|
|
|
.help("testing testing"),
|
2020-06-13 15:32:24 +00:00
|
|
|
),
|
|
|
|
)
|
2021-12-27 18:56:12 +00:00
|
|
|
.try_get_matches_from(vec!["myprog", "some", "--test"])
|
|
|
|
.unwrap();
|
2020-06-13 15:32:24 +00:00
|
|
|
assert_eq!(matches.subcommand_name().unwrap(), "some");
|
|
|
|
let sub_matches = matches.subcommand_matches("some").unwrap();
|
|
|
|
assert!(sub_matches.is_present("test"));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn flag_subcommand_normal_with_alias() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let matches = Command::new("test")
|
2020-06-13 15:32:24 +00:00
|
|
|
.subcommand(
|
2022-02-12 03:48:29 +00:00
|
|
|
Command::new("some")
|
2020-06-16 01:52:36 +00:00
|
|
|
.short_flag('S')
|
|
|
|
.long_flag("S")
|
2020-06-13 15:32:24 +00:00
|
|
|
.arg(
|
|
|
|
Arg::new("test")
|
|
|
|
.short('t')
|
|
|
|
.long("test")
|
2021-11-18 16:17:15 +00:00
|
|
|
.help("testing testing"),
|
2020-06-13 15:32:24 +00:00
|
|
|
)
|
|
|
|
.alias("result"),
|
|
|
|
)
|
2021-12-27 18:56:12 +00:00
|
|
|
.try_get_matches_from(vec!["myprog", "result", "--test"])
|
|
|
|
.unwrap();
|
2020-06-13 15:32:24 +00:00
|
|
|
assert_eq!(matches.subcommand_name().unwrap(), "some");
|
|
|
|
let sub_matches = matches.subcommand_matches("some").unwrap();
|
|
|
|
assert!(sub_matches.is_present("test"));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn flag_subcommand_short() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let matches = Command::new("test")
|
2020-06-13 15:32:24 +00:00
|
|
|
.subcommand(
|
2022-02-12 03:48:29 +00:00
|
|
|
Command::new("some").short_flag('S').arg(
|
2020-06-13 15:32:24 +00:00
|
|
|
Arg::new("test")
|
|
|
|
.short('t')
|
|
|
|
.long("test")
|
2021-11-18 16:17:15 +00:00
|
|
|
.help("testing testing"),
|
2020-06-13 15:32:24 +00:00
|
|
|
),
|
|
|
|
)
|
2021-12-27 18:56:12 +00:00
|
|
|
.try_get_matches_from(vec!["myprog", "-S", "--test"])
|
|
|
|
.unwrap();
|
2020-06-13 15:32:24 +00:00
|
|
|
assert_eq!(matches.subcommand_name().unwrap(), "some");
|
|
|
|
let sub_matches = matches.subcommand_matches("some").unwrap();
|
|
|
|
assert!(sub_matches.is_present("test"));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn flag_subcommand_short_with_args() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let matches = Command::new("test")
|
2020-06-13 15:32:24 +00:00
|
|
|
.subcommand(
|
2022-02-12 03:48:29 +00:00
|
|
|
Command::new("some").short_flag('S').arg(
|
2020-06-13 15:32:24 +00:00
|
|
|
Arg::new("test")
|
|
|
|
.short('t')
|
|
|
|
.long("test")
|
2021-11-18 16:17:15 +00:00
|
|
|
.help("testing testing"),
|
2020-06-13 15:32:24 +00:00
|
|
|
),
|
|
|
|
)
|
2021-12-27 18:56:12 +00:00
|
|
|
.try_get_matches_from(vec!["myprog", "-St"])
|
|
|
|
.unwrap();
|
2020-06-13 15:32:24 +00:00
|
|
|
assert_eq!(matches.subcommand_name().unwrap(), "some");
|
|
|
|
let sub_matches = matches.subcommand_matches("some").unwrap();
|
|
|
|
assert!(sub_matches.is_present("test"));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn flag_subcommand_short_with_alias() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let matches = Command::new("test")
|
2020-06-13 15:32:24 +00:00
|
|
|
.subcommand(
|
2022-02-12 03:48:29 +00:00
|
|
|
Command::new("some")
|
2020-06-16 01:52:36 +00:00
|
|
|
.short_flag('S')
|
2020-06-13 15:32:24 +00:00
|
|
|
.arg(
|
|
|
|
Arg::new("test")
|
|
|
|
.short('t')
|
|
|
|
.long("test")
|
2021-11-18 16:17:15 +00:00
|
|
|
.help("testing testing"),
|
2020-06-13 15:32:24 +00:00
|
|
|
)
|
2020-06-16 04:15:47 +00:00
|
|
|
.short_flag_alias('M')
|
|
|
|
.short_flag_alias('B'),
|
2020-06-13 15:32:24 +00:00
|
|
|
)
|
2021-12-27 18:56:12 +00:00
|
|
|
.try_get_matches_from(vec!["myprog", "-Bt"])
|
|
|
|
.unwrap();
|
2020-06-13 15:32:24 +00:00
|
|
|
assert_eq!(matches.subcommand_name().unwrap(), "some");
|
|
|
|
let sub_matches = matches.subcommand_matches("some").unwrap();
|
|
|
|
assert!(sub_matches.is_present("test"));
|
|
|
|
}
|
|
|
|
|
2020-07-11 18:25:47 +00:00
|
|
|
#[test]
|
|
|
|
fn flag_subcommand_short_with_alias_same_as_short_flag() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let matches = Command::new("test")
|
|
|
|
.subcommand(Command::new("some").short_flag('S').short_flag_alias('S'))
|
2021-12-27 18:56:12 +00:00
|
|
|
.try_get_matches_from(vec!["myprog", "-S"])
|
|
|
|
.unwrap();
|
2020-07-11 18:25:47 +00:00
|
|
|
assert_eq!(matches.subcommand_name().unwrap(), "some");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn flag_subcommand_long_with_alias_same_as_long_flag() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let matches = Command::new("test")
|
|
|
|
.subcommand(
|
|
|
|
Command::new("some")
|
|
|
|
.long_flag("sync")
|
|
|
|
.long_flag_alias("sync"),
|
|
|
|
)
|
2021-12-27 18:56:12 +00:00
|
|
|
.try_get_matches_from(vec!["myprog", "--sync"])
|
|
|
|
.unwrap();
|
2020-07-11 18:25:47 +00:00
|
|
|
assert_eq!(matches.subcommand_name().unwrap(), "some");
|
|
|
|
}
|
|
|
|
|
2020-06-16 04:15:47 +00:00
|
|
|
#[test]
|
|
|
|
fn flag_subcommand_short_with_aliases_vis_and_hidden() {
|
2022-02-14 21:47:20 +00:00
|
|
|
let cmd = Command::new("test").subcommand(
|
2022-02-12 03:48:29 +00:00
|
|
|
Command::new("some")
|
2020-06-16 04:15:47 +00:00
|
|
|
.short_flag('S')
|
|
|
|
.arg(
|
|
|
|
Arg::new("test")
|
|
|
|
.short('t')
|
|
|
|
.long("test")
|
2021-11-18 16:17:15 +00:00
|
|
|
.help("testing testing"),
|
2020-06-16 04:15:47 +00:00
|
|
|
)
|
|
|
|
.visible_short_flag_aliases(&['M', 'B'])
|
|
|
|
.short_flag_alias('C'),
|
|
|
|
);
|
2022-02-14 21:47:20 +00:00
|
|
|
let app1 = cmd.clone();
|
2021-12-27 18:56:12 +00:00
|
|
|
let matches1 = app1.try_get_matches_from(vec!["test", "-M"]).unwrap();
|
2020-06-16 04:15:47 +00:00
|
|
|
assert_eq!(matches1.subcommand_name().unwrap(), "some");
|
|
|
|
|
2022-02-14 21:47:20 +00:00
|
|
|
let app2 = cmd.clone();
|
2021-12-27 18:56:12 +00:00
|
|
|
let matches2 = app2.try_get_matches_from(vec!["test", "-C"]).unwrap();
|
2020-06-16 04:15:47 +00:00
|
|
|
assert_eq!(matches2.subcommand_name().unwrap(), "some");
|
|
|
|
|
2022-02-14 21:47:20 +00:00
|
|
|
let app3 = cmd.clone();
|
2021-12-27 18:56:12 +00:00
|
|
|
let matches3 = app3.try_get_matches_from(vec!["test", "-B"]).unwrap();
|
2020-06-16 04:15:47 +00:00
|
|
|
assert_eq!(matches3.subcommand_name().unwrap(), "some");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn flag_subcommand_short_with_aliases() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let matches = Command::new("test")
|
2020-06-16 04:15:47 +00:00
|
|
|
.subcommand(
|
2022-02-12 03:48:29 +00:00
|
|
|
Command::new("some")
|
2020-06-16 04:15:47 +00:00
|
|
|
.short_flag('S')
|
|
|
|
.arg(
|
|
|
|
Arg::new("test")
|
|
|
|
.short('t')
|
|
|
|
.long("test")
|
2021-11-18 16:17:15 +00:00
|
|
|
.help("testing testing"),
|
2020-06-16 04:15:47 +00:00
|
|
|
)
|
|
|
|
.short_flag_aliases(&['M', 'B']),
|
|
|
|
)
|
2021-12-27 18:56:12 +00:00
|
|
|
.try_get_matches_from(vec!["myprog", "-Bt"])
|
|
|
|
.unwrap();
|
2020-06-16 04:15:47 +00:00
|
|
|
assert_eq!(matches.subcommand_name().unwrap(), "some");
|
|
|
|
let sub_matches = matches.subcommand_matches("some").unwrap();
|
|
|
|
assert!(sub_matches.is_present("test"));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[should_panic]
|
|
|
|
fn flag_subcommand_short_with_alias_hyphen() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let _ = Command::new("test")
|
2020-06-16 04:15:47 +00:00
|
|
|
.subcommand(
|
2022-02-12 03:48:29 +00:00
|
|
|
Command::new("some")
|
2020-06-16 04:15:47 +00:00
|
|
|
.short_flag('S')
|
|
|
|
.arg(
|
|
|
|
Arg::new("test")
|
|
|
|
.short('t')
|
|
|
|
.long("test")
|
2021-11-18 16:17:15 +00:00
|
|
|
.help("testing testing"),
|
2020-06-16 04:15:47 +00:00
|
|
|
)
|
|
|
|
.short_flag_alias('-'),
|
|
|
|
)
|
2021-12-27 18:56:12 +00:00
|
|
|
.try_get_matches_from(vec!["myprog", "-Bt"])
|
|
|
|
.unwrap();
|
2020-06-16 04:15:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[should_panic]
|
|
|
|
fn flag_subcommand_short_with_aliases_hyphen() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let _ = Command::new("test")
|
2020-06-16 04:15:47 +00:00
|
|
|
.subcommand(
|
2022-02-12 03:48:29 +00:00
|
|
|
Command::new("some")
|
2020-06-16 04:15:47 +00:00
|
|
|
.short_flag('S')
|
|
|
|
.arg(
|
|
|
|
Arg::new("test")
|
|
|
|
.short('t')
|
|
|
|
.long("test")
|
2021-11-18 16:17:15 +00:00
|
|
|
.help("testing testing"),
|
2020-06-16 04:15:47 +00:00
|
|
|
)
|
|
|
|
.short_flag_aliases(&['-', '-', '-']),
|
|
|
|
)
|
2021-12-27 18:56:12 +00:00
|
|
|
.try_get_matches_from(vec!["myprog", "-Bt"])
|
|
|
|
.unwrap();
|
2020-06-16 04:15:47 +00:00
|
|
|
}
|
|
|
|
|
2021-05-26 23:44:50 +00:00
|
|
|
#[test]
|
|
|
|
fn flag_subcommand_short_after_long_arg() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let m = Command::new("pacman")
|
2021-05-26 23:44:50 +00:00
|
|
|
.subcommand(
|
2022-02-12 03:48:29 +00:00
|
|
|
Command::new("sync")
|
2021-05-26 23:44:50 +00:00
|
|
|
.short_flag('S')
|
|
|
|
.arg(Arg::new("clean").short('c')),
|
|
|
|
)
|
2021-06-16 05:28:25 +00:00
|
|
|
.arg(Arg::new("arg").long("arg").takes_value(true))
|
2021-12-27 18:56:12 +00:00
|
|
|
.try_get_matches_from(vec!["pacman", "--arg", "foo", "-Sc"])
|
|
|
|
.unwrap();
|
2021-05-26 23:44:50 +00:00
|
|
|
let subm = m.subcommand_matches("sync");
|
|
|
|
assert!(subm.is_some());
|
|
|
|
let subm = subm.unwrap();
|
|
|
|
assert!(subm.is_present("clean"));
|
|
|
|
}
|
|
|
|
|
2020-06-13 15:32:24 +00:00
|
|
|
#[test]
|
|
|
|
fn flag_subcommand_long() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let matches = Command::new("test")
|
2020-06-13 15:32:24 +00:00
|
|
|
.subcommand(
|
2022-02-12 03:48:29 +00:00
|
|
|
Command::new("some").long_flag("some").arg(
|
2020-06-13 15:32:24 +00:00
|
|
|
Arg::new("test")
|
|
|
|
.short('t')
|
|
|
|
.long("test")
|
2021-11-18 16:17:15 +00:00
|
|
|
.help("testing testing"),
|
2020-06-13 15:32:24 +00:00
|
|
|
),
|
|
|
|
)
|
2021-12-27 18:56:12 +00:00
|
|
|
.try_get_matches_from(vec!["myprog", "--some", "--test"])
|
|
|
|
.unwrap();
|
2020-06-13 15:32:24 +00:00
|
|
|
assert_eq!(matches.subcommand_name().unwrap(), "some");
|
|
|
|
let sub_matches = matches.subcommand_matches("some").unwrap();
|
|
|
|
assert!(sub_matches.is_present("test"));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn flag_subcommand_long_with_alias() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let matches = Command::new("test")
|
2020-06-13 15:32:24 +00:00
|
|
|
.subcommand(
|
2022-02-12 03:48:29 +00:00
|
|
|
Command::new("some")
|
2020-06-16 01:52:36 +00:00
|
|
|
.long_flag("some")
|
2020-06-13 15:32:24 +00:00
|
|
|
.arg(
|
|
|
|
Arg::new("test")
|
|
|
|
.short('t')
|
|
|
|
.long("test")
|
2021-11-18 16:17:15 +00:00
|
|
|
.help("testing testing"),
|
2020-06-13 15:32:24 +00:00
|
|
|
)
|
2020-07-10 13:27:14 +00:00
|
|
|
.long_flag_alias("result"),
|
|
|
|
)
|
2021-12-27 18:56:12 +00:00
|
|
|
.try_get_matches_from(vec!["myprog", "--result", "--test"])
|
|
|
|
.unwrap();
|
2020-07-10 13:27:14 +00:00
|
|
|
assert_eq!(matches.subcommand_name().unwrap(), "some");
|
|
|
|
let sub_matches = matches.subcommand_matches("some").unwrap();
|
|
|
|
assert!(sub_matches.is_present("test"));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn flag_subcommand_long_with_aliases() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let matches = Command::new("test")
|
2020-07-10 13:27:14 +00:00
|
|
|
.subcommand(
|
2022-02-12 03:48:29 +00:00
|
|
|
Command::new("some")
|
2020-07-10 13:27:14 +00:00
|
|
|
.long_flag("some")
|
|
|
|
.arg(
|
|
|
|
Arg::new("test")
|
|
|
|
.short('t')
|
|
|
|
.long("test")
|
2021-11-18 16:17:15 +00:00
|
|
|
.help("testing testing"),
|
2020-07-10 13:27:14 +00:00
|
|
|
)
|
|
|
|
.long_flag_aliases(&["result", "someall"]),
|
2020-06-13 15:32:24 +00:00
|
|
|
)
|
2021-12-27 18:56:12 +00:00
|
|
|
.try_get_matches_from(vec!["myprog", "--result", "--test"])
|
|
|
|
.unwrap();
|
2020-06-13 15:32:24 +00:00
|
|
|
assert_eq!(matches.subcommand_name().unwrap(), "some");
|
|
|
|
let sub_matches = matches.subcommand_matches("some").unwrap();
|
|
|
|
assert!(sub_matches.is_present("test"));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn flag_subcommand_multiple() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let matches = Command::new("test")
|
2020-06-13 15:32:24 +00:00
|
|
|
.subcommand(
|
2022-02-12 03:48:29 +00:00
|
|
|
Command::new("some")
|
2020-06-16 01:52:36 +00:00
|
|
|
.short_flag('S')
|
|
|
|
.long_flag("some")
|
2021-11-19 20:33:11 +00:00
|
|
|
.arg(arg!(-f --flag "some flag"))
|
|
|
|
.arg(arg!(-p --print "print something"))
|
2020-06-13 15:32:24 +00:00
|
|
|
.subcommand(
|
2022-02-12 03:48:29 +00:00
|
|
|
Command::new("result")
|
2020-06-16 01:52:36 +00:00
|
|
|
.short_flag('R')
|
|
|
|
.long_flag("result")
|
2021-11-19 20:33:11 +00:00
|
|
|
.arg(arg!(-f --flag "some flag"))
|
|
|
|
.arg(arg!(-p --print "print something")),
|
2020-06-13 15:32:24 +00:00
|
|
|
),
|
|
|
|
)
|
2021-12-27 18:56:12 +00:00
|
|
|
.try_get_matches_from(vec!["myprog", "-SfpRfp"])
|
|
|
|
.unwrap();
|
2020-06-13 15:32:24 +00:00
|
|
|
assert_eq!(matches.subcommand_name().unwrap(), "some");
|
|
|
|
let sub_matches = matches.subcommand_matches("some").unwrap();
|
|
|
|
assert!(sub_matches.is_present("flag"));
|
|
|
|
assert!(sub_matches.is_present("print"));
|
|
|
|
assert_eq!(sub_matches.subcommand_name().unwrap(), "result");
|
|
|
|
let result_matches = sub_matches.subcommand_matches("result").unwrap();
|
|
|
|
assert!(result_matches.is_present("flag"));
|
|
|
|
assert!(result_matches.is_present("print"));
|
|
|
|
}
|
2020-06-16 04:15:47 +00:00
|
|
|
|
2020-06-17 05:53:53 +00:00
|
|
|
#[cfg(debug_assertions)]
|
2020-06-17 03:45:21 +00:00
|
|
|
#[test]
|
2020-08-02 17:53:07 +00:00
|
|
|
#[should_panic = "the \'-f\' short flag for the \'test\' argument conflicts with the short flag for \'some\' subcommand"]
|
2020-06-17 03:45:21 +00:00
|
|
|
fn flag_subcommand_short_conflict_with_arg() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let _ = Command::new("test")
|
|
|
|
.subcommand(Command::new("some").short_flag('f').long_flag("some"))
|
2020-06-17 03:45:21 +00:00
|
|
|
.arg(Arg::new("test").short('f'))
|
2021-12-27 18:56:12 +00:00
|
|
|
.try_get_matches_from(vec!["myprog", "-f"])
|
|
|
|
.unwrap();
|
2020-06-17 03:45:21 +00:00
|
|
|
}
|
2020-06-16 04:15:47 +00:00
|
|
|
|
2020-07-11 18:25:47 +00:00
|
|
|
#[cfg(debug_assertions)]
|
|
|
|
#[test]
|
2020-08-02 17:53:07 +00:00
|
|
|
#[should_panic = "the \'-f\' short flag is specified for both \'some\' and \'result\' subcommands"]
|
2020-07-11 18:25:47 +00:00
|
|
|
fn flag_subcommand_short_conflict_with_alias() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let _ = Command::new("test")
|
|
|
|
.subcommand(Command::new("some").short_flag('f').long_flag("some"))
|
|
|
|
.subcommand(Command::new("result").short_flag('t').short_flag_alias('f'))
|
2021-12-27 18:56:12 +00:00
|
|
|
.try_get_matches_from(vec!["myprog", "-f"])
|
|
|
|
.unwrap();
|
2020-07-11 18:25:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(debug_assertions)]
|
|
|
|
#[test]
|
2020-08-02 17:53:07 +00:00
|
|
|
#[should_panic = "the \'--flag\' long flag is specified for both \'some\' and \'result\' subcommands"]
|
2020-07-11 18:25:47 +00:00
|
|
|
fn flag_subcommand_long_conflict_with_alias() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let _ = Command::new("test")
|
|
|
|
.subcommand(Command::new("some").long_flag("flag"))
|
|
|
|
.subcommand(
|
|
|
|
Command::new("result")
|
|
|
|
.long_flag("test")
|
|
|
|
.long_flag_alias("flag"),
|
|
|
|
)
|
2021-12-27 18:56:12 +00:00
|
|
|
.try_get_matches_from(vec!["myprog", "--flag"])
|
|
|
|
.unwrap();
|
2020-07-11 18:25:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(debug_assertions)]
|
|
|
|
#[test]
|
2020-08-02 17:53:07 +00:00
|
|
|
#[should_panic = "the \'-f\' short flag for the \'test\' argument conflicts with the short flag for \'some\' subcommand"]
|
2020-07-11 18:25:47 +00:00
|
|
|
fn flag_subcommand_short_conflict_with_arg_alias() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let _ = Command::new("test")
|
|
|
|
.subcommand(Command::new("some").short_flag('f').long_flag("some"))
|
2020-07-11 18:25:47 +00:00
|
|
|
.arg(Arg::new("test").short('t').short_alias('f'))
|
2021-12-27 18:56:12 +00:00
|
|
|
.try_get_matches_from(vec!["myprog", "-f"])
|
|
|
|
.unwrap();
|
2020-07-11 18:25:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(debug_assertions)]
|
|
|
|
#[test]
|
2020-08-02 17:53:07 +00:00
|
|
|
#[should_panic = "the \'--some\' long flag for the \'test\' argument conflicts with the short flag for \'some\' subcommand"]
|
2020-07-11 18:25:47 +00:00
|
|
|
fn flag_subcommand_long_conflict_with_arg_alias() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let _ = Command::new("test")
|
|
|
|
.subcommand(Command::new("some").short_flag('f').long_flag("some"))
|
2020-07-11 18:25:47 +00:00
|
|
|
.arg(Arg::new("test").long("test").alias("some"))
|
2021-12-27 18:56:12 +00:00
|
|
|
.try_get_matches_from(vec!["myprog", "--some"])
|
|
|
|
.unwrap();
|
2020-07-11 18:25:47 +00:00
|
|
|
}
|
|
|
|
|
2020-06-17 05:53:53 +00:00
|
|
|
#[cfg(debug_assertions)]
|
2020-06-17 03:45:21 +00:00
|
|
|
#[test]
|
2020-08-02 17:53:07 +00:00
|
|
|
#[should_panic = "the \'--flag\' long flag for the \'flag\' argument conflicts with the short flag for \'some\' subcommand"]
|
2020-06-17 03:45:21 +00:00
|
|
|
fn flag_subcommand_long_conflict_with_arg() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let _ = Command::new("test")
|
|
|
|
.subcommand(Command::new("some").short_flag('a').long_flag("flag"))
|
2020-06-17 03:45:21 +00:00
|
|
|
.arg(Arg::new("flag").long("flag"))
|
2021-12-27 18:56:12 +00:00
|
|
|
.try_get_matches_from(vec!["myprog", "--flag"])
|
|
|
|
.unwrap();
|
2020-06-17 03:45:21 +00:00
|
|
|
}
|
2020-06-16 04:15:47 +00:00
|
|
|
|
2020-06-17 03:45:21 +00:00
|
|
|
#[test]
|
|
|
|
fn flag_subcommand_conflict_with_help() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let _ = Command::new("test")
|
|
|
|
.subcommand(Command::new("help").short_flag('h').long_flag("help"))
|
2021-12-27 18:56:12 +00:00
|
|
|
.try_get_matches_from(vec!["myprog", "--help"])
|
|
|
|
.unwrap();
|
2020-06-17 03:45:21 +00:00
|
|
|
}
|
2020-06-16 04:15:47 +00:00
|
|
|
|
2020-06-17 03:45:21 +00:00
|
|
|
#[test]
|
|
|
|
fn flag_subcommand_conflict_with_version() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let _ = Command::new("test")
|
|
|
|
.subcommand(Command::new("ver").short_flag('V').long_flag("version"))
|
2021-12-27 18:56:12 +00:00
|
|
|
.try_get_matches_from(vec!["myprog", "--version"])
|
|
|
|
.unwrap();
|
2020-06-17 03:45:21 +00:00
|
|
|
}
|
2020-07-08 04:11:28 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn flag_subcommand_long_infer_pass() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let m = Command::new("prog")
|
2022-02-10 17:51:40 +00:00
|
|
|
.infer_subcommands(true)
|
2022-02-12 03:48:29 +00:00
|
|
|
.subcommand(Command::new("test").long_flag("test"))
|
2021-12-27 18:56:12 +00:00
|
|
|
.try_get_matches_from(vec!["prog", "--te"])
|
|
|
|
.unwrap();
|
2020-07-08 04:11:28 +00:00
|
|
|
assert_eq!(m.subcommand_name(), Some("test"));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(not(feature = "suggestions"))]
|
|
|
|
#[test]
|
|
|
|
fn flag_subcommand_long_infer_fail() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let m = Command::new("prog")
|
2022-02-10 17:51:40 +00:00
|
|
|
.infer_subcommands(true)
|
2022-02-12 03:48:29 +00:00
|
|
|
.subcommand(Command::new("test").long_flag("test"))
|
|
|
|
.subcommand(Command::new("temp").long_flag("temp"))
|
2020-07-08 04:11:28 +00:00
|
|
|
.try_get_matches_from(vec!["prog", "--te"]);
|
|
|
|
assert!(m.is_err(), "{:#?}", m.unwrap());
|
2022-01-25 22:19:28 +00:00
|
|
|
assert_eq!(m.unwrap_err().kind(), ErrorKind::UnknownArgument);
|
2020-07-08 04:11:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(feature = "suggestions")]
|
|
|
|
#[test]
|
|
|
|
fn flag_subcommand_long_infer_fail() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let m = Command::new("prog")
|
2022-02-10 17:51:40 +00:00
|
|
|
.infer_subcommands(true)
|
2022-02-12 03:48:29 +00:00
|
|
|
.subcommand(Command::new("test").long_flag("test"))
|
|
|
|
.subcommand(Command::new("temp").long_flag("temp"))
|
2020-07-08 04:11:28 +00:00
|
|
|
.try_get_matches_from(vec!["prog", "--te"]);
|
|
|
|
assert!(m.is_err(), "{:#?}", m.unwrap());
|
2022-01-25 22:19:28 +00:00
|
|
|
assert_eq!(m.unwrap_err().kind(), ErrorKind::UnknownArgument);
|
2020-07-08 04:11:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2020-07-09 01:37:56 +00:00
|
|
|
fn flag_subcommand_long_infer_pass_close() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let m = Command::new("prog")
|
2022-02-10 17:51:40 +00:00
|
|
|
.infer_subcommands(true)
|
2022-02-12 03:48:29 +00:00
|
|
|
.subcommand(Command::new("test").long_flag("test"))
|
|
|
|
.subcommand(Command::new("temp").long_flag("temp"))
|
2021-12-27 18:56:12 +00:00
|
|
|
.try_get_matches_from(vec!["prog", "--tes"])
|
|
|
|
.unwrap();
|
2020-07-08 04:11:28 +00:00
|
|
|
assert_eq!(m.subcommand_name(), Some("test"));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2020-07-09 01:37:56 +00:00
|
|
|
fn flag_subcommand_long_infer_exact_match() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let m = Command::new("prog")
|
2022-02-10 17:51:40 +00:00
|
|
|
.infer_subcommands(true)
|
2022-02-12 03:48:29 +00:00
|
|
|
.subcommand(Command::new("test").long_flag("test"))
|
|
|
|
.subcommand(Command::new("testa").long_flag("testa"))
|
|
|
|
.subcommand(Command::new("testb").long_flag("testb"))
|
2021-12-27 18:56:12 +00:00
|
|
|
.try_get_matches_from(vec!["prog", "--test"])
|
|
|
|
.unwrap();
|
2020-07-08 04:11:28 +00:00
|
|
|
assert_eq!(m.subcommand_name(), Some("test"));
|
|
|
|
}
|
2020-07-09 01:37:56 +00:00
|
|
|
|
|
|
|
static FLAG_SUBCOMMAND_HELP: &str = "pacman-query
|
|
|
|
Query the package database.
|
|
|
|
|
|
|
|
USAGE:
|
2022-02-14 22:22:59 +00:00
|
|
|
pacman {query|--query|-Q} [OPTIONS]
|
2020-07-09 01:37:56 +00:00
|
|
|
|
|
|
|
OPTIONS:
|
2021-10-11 22:01:33 +00:00
|
|
|
-h, --help Print help information
|
2020-07-09 01:37:56 +00:00
|
|
|
-i, --info <info>... view package information
|
2021-09-24 15:58:39 +00:00
|
|
|
-s, --search <search>... search locally installed packages for matching strings
|
|
|
|
";
|
2020-07-09 01:37:56 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn flag_subcommand_long_short_normal_usage_string() {
|
2022-02-14 21:47:20 +00:00
|
|
|
let cmd = Command::new("pacman")
|
2020-07-09 01:37:56 +00:00
|
|
|
.about("package manager utility")
|
|
|
|
.version("5.2.1")
|
2022-02-10 17:51:40 +00:00
|
|
|
.subcommand_required(true)
|
2020-07-09 01:37:56 +00:00
|
|
|
.author("Pacman Development Team")
|
|
|
|
// Query subcommand
|
|
|
|
//
|
|
|
|
// Only a few of its arguments are implemented below.
|
|
|
|
.subcommand(
|
2022-02-12 03:48:29 +00:00
|
|
|
Command::new("query")
|
2020-07-09 01:37:56 +00:00
|
|
|
.short_flag('Q')
|
|
|
|
.long_flag("query")
|
|
|
|
.about("Query the package database.")
|
|
|
|
.arg(
|
|
|
|
Arg::new("search")
|
|
|
|
.short('s')
|
|
|
|
.long("search")
|
2021-11-18 16:17:15 +00:00
|
|
|
.help("search locally installed packages for matching strings")
|
2020-07-09 01:37:56 +00:00
|
|
|
.conflicts_with("info")
|
2021-02-24 15:07:57 +00:00
|
|
|
.takes_value(true)
|
2020-07-09 01:37:56 +00:00
|
|
|
.multiple_values(true),
|
|
|
|
)
|
|
|
|
.arg(
|
|
|
|
Arg::new("info")
|
|
|
|
.long("info")
|
|
|
|
.short('i')
|
|
|
|
.conflicts_with("search")
|
2021-11-18 16:17:15 +00:00
|
|
|
.help("view package information")
|
2021-02-24 15:07:57 +00:00
|
|
|
.takes_value(true)
|
2020-07-09 01:37:56 +00:00
|
|
|
.multiple_values(true),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
assert!(utils::compare_output(
|
2022-02-14 21:47:20 +00:00
|
|
|
cmd,
|
2020-07-09 01:37:56 +00:00
|
|
|
"pacman -Qh",
|
|
|
|
FLAG_SUBCOMMAND_HELP,
|
|
|
|
false
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
static FLAG_SUBCOMMAND_NO_SHORT_HELP: &str = "pacman-query
|
|
|
|
Query the package database.
|
|
|
|
|
|
|
|
USAGE:
|
2022-02-14 22:22:59 +00:00
|
|
|
pacman {query|--query} [OPTIONS]
|
2020-07-09 01:37:56 +00:00
|
|
|
|
|
|
|
OPTIONS:
|
2021-10-11 22:01:33 +00:00
|
|
|
-h, --help Print help information
|
2020-07-09 01:37:56 +00:00
|
|
|
-i, --info <info>... view package information
|
2021-09-24 15:58:39 +00:00
|
|
|
-s, --search <search>... search locally installed packages for matching strings
|
|
|
|
";
|
2020-07-09 01:37:56 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn flag_subcommand_long_normal_usage_string() {
|
2022-02-14 21:47:20 +00:00
|
|
|
let cmd = Command::new("pacman")
|
2020-07-09 01:37:56 +00:00
|
|
|
.about("package manager utility")
|
|
|
|
.version("5.2.1")
|
2022-02-10 17:51:40 +00:00
|
|
|
.subcommand_required(true)
|
2020-07-09 01:37:56 +00:00
|
|
|
.author("Pacman Development Team")
|
|
|
|
// Query subcommand
|
|
|
|
//
|
|
|
|
// Only a few of its arguments are implemented below.
|
|
|
|
.subcommand(
|
2022-02-12 03:48:29 +00:00
|
|
|
Command::new("query")
|
2020-07-09 01:37:56 +00:00
|
|
|
.long_flag("query")
|
|
|
|
.about("Query the package database.")
|
|
|
|
.arg(
|
|
|
|
Arg::new("search")
|
|
|
|
.short('s')
|
|
|
|
.long("search")
|
2021-11-18 16:17:15 +00:00
|
|
|
.help("search locally installed packages for matching strings")
|
2020-07-09 01:37:56 +00:00
|
|
|
.conflicts_with("info")
|
2021-02-24 15:07:57 +00:00
|
|
|
.takes_value(true)
|
2020-07-09 01:37:56 +00:00
|
|
|
.multiple_values(true),
|
|
|
|
)
|
|
|
|
.arg(
|
|
|
|
Arg::new("info")
|
|
|
|
.long("info")
|
|
|
|
.short('i')
|
|
|
|
.conflicts_with("search")
|
2021-11-18 16:17:15 +00:00
|
|
|
.help("view package information")
|
2021-02-24 15:07:57 +00:00
|
|
|
.takes_value(true)
|
2020-07-09 01:37:56 +00:00
|
|
|
.multiple_values(true),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
assert!(utils::compare_output(
|
2022-02-14 21:47:20 +00:00
|
|
|
cmd,
|
2020-07-09 01:37:56 +00:00
|
|
|
"pacman query --help",
|
|
|
|
FLAG_SUBCOMMAND_NO_SHORT_HELP,
|
|
|
|
false
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
static FLAG_SUBCOMMAND_NO_LONG_HELP: &str = "pacman-query
|
|
|
|
Query the package database.
|
|
|
|
|
|
|
|
USAGE:
|
2022-02-14 22:22:59 +00:00
|
|
|
pacman {query|-Q} [OPTIONS]
|
2020-07-09 01:37:56 +00:00
|
|
|
|
|
|
|
OPTIONS:
|
2021-10-11 22:01:33 +00:00
|
|
|
-h, --help Print help information
|
2020-07-09 01:37:56 +00:00
|
|
|
-i, --info <info>... view package information
|
2021-09-24 15:58:39 +00:00
|
|
|
-s, --search <search>... search locally installed packages for matching strings
|
|
|
|
";
|
2020-07-09 01:37:56 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn flag_subcommand_short_normal_usage_string() {
|
2022-02-14 21:47:20 +00:00
|
|
|
let cmd = Command::new("pacman")
|
2020-07-09 01:37:56 +00:00
|
|
|
.about("package manager utility")
|
|
|
|
.version("5.2.1")
|
2022-02-10 17:51:40 +00:00
|
|
|
.subcommand_required(true)
|
2020-07-09 01:37:56 +00:00
|
|
|
.author("Pacman Development Team")
|
|
|
|
// Query subcommand
|
|
|
|
//
|
|
|
|
// Only a few of its arguments are implemented below.
|
|
|
|
.subcommand(
|
2022-02-12 03:48:29 +00:00
|
|
|
Command::new("query")
|
2020-07-09 01:37:56 +00:00
|
|
|
.short_flag('Q')
|
|
|
|
.about("Query the package database.")
|
|
|
|
.arg(
|
|
|
|
Arg::new("search")
|
|
|
|
.short('s')
|
|
|
|
.long("search")
|
2021-11-18 16:17:15 +00:00
|
|
|
.help("search locally installed packages for matching strings")
|
2020-07-09 01:37:56 +00:00
|
|
|
.conflicts_with("info")
|
2021-02-24 15:07:57 +00:00
|
|
|
.takes_value(true)
|
2020-07-09 01:37:56 +00:00
|
|
|
.multiple_values(true),
|
|
|
|
)
|
|
|
|
.arg(
|
|
|
|
Arg::new("info")
|
|
|
|
.long("info")
|
|
|
|
.short('i')
|
|
|
|
.conflicts_with("search")
|
2021-11-18 16:17:15 +00:00
|
|
|
.help("view package information")
|
2021-02-24 15:07:57 +00:00
|
|
|
.takes_value(true)
|
2020-07-09 01:37:56 +00:00
|
|
|
.multiple_values(true),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
assert!(utils::compare_output(
|
2022-02-14 21:47:20 +00:00
|
|
|
cmd,
|
2020-07-09 01:37:56 +00:00
|
|
|
"pacman query --help",
|
|
|
|
FLAG_SUBCOMMAND_NO_LONG_HELP,
|
|
|
|
false
|
|
|
|
));
|
|
|
|
}
|