2022-06-10 01:03:28 +00:00
|
|
|
use clap::{arg, error::ErrorKind, Arg, ArgAction, ArgMatches, Command};
|
2015-08-27 21:03:45 +00:00
|
|
|
|
2022-09-19 14:59:04 +00:00
|
|
|
#[cfg(feature = "error-context")]
|
|
|
|
use super::utils;
|
2020-04-12 15:44:48 +00:00
|
|
|
|
2017-02-18 02:56:34 +00:00
|
|
|
#[test]
|
|
|
|
fn require_equals_fail() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let res = Command::new("prog")
|
2018-01-25 04:05:05 +00:00
|
|
|
.arg(
|
2020-05-14 20:50:56 +00:00
|
|
|
Arg::new("cfg")
|
2021-11-29 14:56:30 +00:00
|
|
|
.require_equals(true)
|
2022-05-23 21:58:07 +00:00
|
|
|
.value_parser(clap::builder::NonEmptyStringValueParser::new())
|
2022-07-26 00:17:01 +00:00
|
|
|
.action(ArgAction::Set)
|
2018-01-25 04:05:05 +00:00
|
|
|
.long("config"),
|
|
|
|
)
|
2018-01-31 20:15:01 +00:00
|
|
|
.try_get_matches_from(vec!["prog", "--config", "file.conf"]);
|
2017-02-18 02:56:34 +00:00
|
|
|
assert!(res.is_err());
|
2022-01-25 22:19:28 +00:00
|
|
|
assert_eq!(res.unwrap_err().kind(), ErrorKind::NoEquals);
|
2021-03-09 16:59:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2022-09-19 14:59:04 +00:00
|
|
|
#[cfg(feature = "error-context")]
|
2021-03-09 16:59:12 +00:00
|
|
|
fn require_equals_fail_message() {
|
2023-01-03 16:22:40 +00:00
|
|
|
static NO_EQUALS: &str = "error: equal sign is needed when assigning values to '--config=<cfg>'
|
2021-03-09 16:59:12 +00:00
|
|
|
|
2022-09-07 16:03:55 +00:00
|
|
|
Usage: prog [OPTIONS]
|
2021-03-09 16:59:12 +00:00
|
|
|
|
2023-01-03 19:22:35 +00:00
|
|
|
For more information, try '--help'.
|
2021-09-24 15:58:39 +00:00
|
|
|
";
|
2022-02-14 21:47:20 +00:00
|
|
|
let cmd = Command::new("prog").arg(
|
2021-03-09 16:59:12 +00:00
|
|
|
Arg::new("cfg")
|
2021-11-29 14:56:30 +00:00
|
|
|
.require_equals(true)
|
2022-07-26 00:17:01 +00:00
|
|
|
.action(ArgAction::Set)
|
2021-03-09 16:59:12 +00:00
|
|
|
.long("config"),
|
|
|
|
);
|
2022-04-29 20:32:25 +00:00
|
|
|
utils::assert_output(cmd, "prog --config file.conf", NO_EQUALS, true);
|
2017-02-18 02:56:34 +00:00
|
|
|
}
|
|
|
|
|
2017-09-13 19:27:19 +00:00
|
|
|
#[test]
|
|
|
|
fn require_equals_min_values_zero() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let res = Command::new("prog")
|
2018-01-25 04:05:05 +00:00
|
|
|
.arg(
|
2020-05-14 20:50:56 +00:00
|
|
|
Arg::new("cfg")
|
2022-07-26 00:17:01 +00:00
|
|
|
.action(ArgAction::Set)
|
2021-11-29 14:56:30 +00:00
|
|
|
.require_equals(true)
|
2022-08-03 16:20:07 +00:00
|
|
|
.num_args(0..)
|
2018-01-25 04:05:05 +00:00
|
|
|
.long("config"),
|
|
|
|
)
|
2020-05-14 20:50:56 +00:00
|
|
|
.arg(Arg::new("cmd"))
|
2018-01-31 20:15:01 +00:00
|
|
|
.try_get_matches_from(vec!["prog", "--config", "cmd"]);
|
2021-12-27 19:57:38 +00:00
|
|
|
assert!(res.is_ok(), "{}", res.unwrap_err());
|
2017-09-13 19:27:19 +00:00
|
|
|
let m = res.unwrap();
|
2022-06-10 01:03:28 +00:00
|
|
|
assert!(m.contains_id("cfg"));
|
2022-05-24 15:16:50 +00:00
|
|
|
assert_eq!(m.get_one::<String>("cmd").map(|v| v.as_str()), Some("cmd"));
|
2017-09-13 19:27:19 +00:00
|
|
|
}
|
|
|
|
|
2017-05-15 22:08:33 +00:00
|
|
|
#[test]
|
|
|
|
fn double_hyphen_as_value() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let res = Command::new("prog")
|
2018-01-25 04:05:05 +00:00
|
|
|
.arg(
|
2020-05-14 20:50:56 +00:00
|
|
|
Arg::new("cfg")
|
2022-07-26 00:17:01 +00:00
|
|
|
.action(ArgAction::Set)
|
2021-11-29 14:56:30 +00:00
|
|
|
.allow_hyphen_values(true)
|
2018-01-25 04:05:05 +00:00
|
|
|
.long("config"),
|
|
|
|
)
|
2018-01-31 20:15:01 +00:00
|
|
|
.try_get_matches_from(vec!["prog", "--config", "--"]);
|
2023-05-04 01:40:21 +00:00
|
|
|
assert!(res.is_ok(), "{res:?}");
|
2022-05-24 15:16:50 +00:00
|
|
|
assert_eq!(
|
|
|
|
res.unwrap().get_one::<String>("cfg").map(|v| v.as_str()),
|
|
|
|
Some("--")
|
|
|
|
);
|
2017-05-15 22:08:33 +00:00
|
|
|
}
|
|
|
|
|
2017-02-18 02:56:34 +00:00
|
|
|
#[test]
|
|
|
|
fn require_equals_no_empty_values_fail() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let res = Command::new("prog")
|
2018-01-25 04:05:05 +00:00
|
|
|
.arg(
|
2020-05-14 20:50:56 +00:00
|
|
|
Arg::new("cfg")
|
2022-07-26 00:17:01 +00:00
|
|
|
.action(ArgAction::Set)
|
2021-11-29 14:56:30 +00:00
|
|
|
.require_equals(true)
|
2022-05-23 21:58:07 +00:00
|
|
|
.value_parser(clap::builder::NonEmptyStringValueParser::new())
|
2018-01-25 04:05:05 +00:00
|
|
|
.long("config"),
|
|
|
|
)
|
2020-05-14 20:50:56 +00:00
|
|
|
.arg(Arg::new("some"))
|
2018-01-31 20:15:01 +00:00
|
|
|
.try_get_matches_from(vec!["prog", "--config=", "file.conf"]);
|
2017-02-18 02:56:34 +00:00
|
|
|
assert!(res.is_err());
|
2022-07-22 14:03:28 +00:00
|
|
|
assert_eq!(res.unwrap_err().kind(), ErrorKind::InvalidValue);
|
2017-02-18 02:56:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn require_equals_empty_vals_pass() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let res = Command::new("prog")
|
2018-01-25 04:05:05 +00:00
|
|
|
.arg(
|
2020-05-14 20:50:56 +00:00
|
|
|
Arg::new("cfg")
|
2022-07-26 00:17:01 +00:00
|
|
|
.action(ArgAction::Set)
|
2021-11-29 14:56:30 +00:00
|
|
|
.require_equals(true)
|
2018-01-25 04:05:05 +00:00
|
|
|
.long("config"),
|
|
|
|
)
|
2018-01-31 20:15:01 +00:00
|
|
|
.try_get_matches_from(vec!["prog", "--config="]);
|
2021-12-27 19:57:38 +00:00
|
|
|
assert!(res.is_ok(), "{}", res.unwrap_err());
|
2017-02-18 02:56:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn require_equals_pass() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let res = Command::new("prog")
|
2018-01-25 04:05:05 +00:00
|
|
|
.arg(
|
2020-05-14 20:50:56 +00:00
|
|
|
Arg::new("cfg")
|
2022-07-26 00:17:01 +00:00
|
|
|
.action(ArgAction::Set)
|
2021-11-29 14:56:30 +00:00
|
|
|
.require_equals(true)
|
2018-01-25 04:05:05 +00:00
|
|
|
.long("config"),
|
|
|
|
)
|
2018-01-31 20:15:01 +00:00
|
|
|
.try_get_matches_from(vec!["prog", "--config=file.conf"]);
|
2021-12-27 19:57:38 +00:00
|
|
|
assert!(res.is_ok(), "{}", res.unwrap_err());
|
2017-02-18 02:56:34 +00:00
|
|
|
}
|
|
|
|
|
2016-02-04 06:34:46 +00:00
|
|
|
#[test]
|
|
|
|
fn stdin_char() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let r = Command::new("opts")
|
2021-11-19 20:33:11 +00:00
|
|
|
.arg(arg!(f: -f [flag] "some flag"))
|
2018-01-31 20:15:01 +00:00
|
|
|
.try_get_matches_from(vec!["", "-f", "-"]);
|
2021-12-27 19:57:38 +00:00
|
|
|
assert!(r.is_ok(), "{}", r.unwrap_err());
|
2016-02-04 06:34:46 +00:00
|
|
|
let m = r.unwrap();
|
2022-06-10 01:03:28 +00:00
|
|
|
assert!(m.contains_id("f"));
|
2022-05-24 15:16:50 +00:00
|
|
|
assert_eq!(m.get_one::<String>("f").map(|v| v.as_str()).unwrap(), "-");
|
2016-02-04 06:34:46 +00:00
|
|
|
}
|
|
|
|
|
2015-08-27 21:03:45 +00:00
|
|
|
#[test]
|
|
|
|
fn opts_using_short() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let r = Command::new("opts")
|
2022-11-24 13:54:25 +00:00
|
|
|
.args([
|
2021-11-19 20:33:11 +00:00
|
|
|
arg!(f: -f [flag] "some flag"),
|
|
|
|
arg!(c: -c [color] "some other flag"),
|
2018-01-25 04:05:05 +00:00
|
|
|
])
|
2018-01-31 20:15:01 +00:00
|
|
|
.try_get_matches_from(vec!["", "-f", "some", "-c", "other"]);
|
2021-12-27 19:57:38 +00:00
|
|
|
assert!(r.is_ok(), "{}", r.unwrap_err());
|
2016-01-26 07:05:32 +00:00
|
|
|
let m = r.unwrap();
|
2022-06-10 01:03:28 +00:00
|
|
|
assert!(m.contains_id("f"));
|
2022-05-24 15:16:50 +00:00
|
|
|
assert_eq!(
|
|
|
|
m.get_one::<String>("f").map(|v| v.as_str()).unwrap(),
|
|
|
|
"some"
|
|
|
|
);
|
2022-06-10 01:03:28 +00:00
|
|
|
assert!(m.contains_id("c"));
|
2022-05-24 15:16:50 +00:00
|
|
|
assert_eq!(
|
|
|
|
m.get_one::<String>("c").map(|v| v.as_str()).unwrap(),
|
|
|
|
"other"
|
|
|
|
);
|
2015-08-27 21:03:45 +00:00
|
|
|
}
|
|
|
|
|
2016-02-02 12:43:53 +00:00
|
|
|
#[test]
|
|
|
|
fn lots_o_vals() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let r = Command::new("opts")
|
2022-09-12 21:59:57 +00:00
|
|
|
.arg(arg!(o: -o <opt> "some opt").num_args(1..).required(true))
|
2018-01-31 20:15:01 +00:00
|
|
|
.try_get_matches_from(vec![
|
2018-01-25 04:05:05 +00:00
|
|
|
"", "-o", "some", "some", "some", "some", "some", "some", "some", "some", "some",
|
|
|
|
"some", "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
|
|
|
|
"some", "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
|
|
|
|
"some", "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
|
|
|
|
"some", "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
|
|
|
|
"some", "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
|
|
|
|
"some", "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
|
|
|
|
"some", "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
|
|
|
|
"some", "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
|
|
|
|
"some", "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
|
|
|
|
"some", "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
|
|
|
|
"some", "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
|
|
|
|
"some", "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
|
|
|
|
"some", "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
|
|
|
|
"some", "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
|
|
|
|
"some", "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
|
|
|
|
"some", "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
|
|
|
|
"some", "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
|
|
|
|
"some", "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
|
|
|
|
"some", "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
|
|
|
|
"some", "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
|
|
|
|
"some", "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
|
|
|
|
"some", "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
|
|
|
|
"some", "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
|
|
|
|
"some", "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
|
|
|
|
"some", "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
|
|
|
|
"some", "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
|
|
|
|
"some", "some",
|
|
|
|
]);
|
2021-12-27 19:57:38 +00:00
|
|
|
assert!(r.is_ok(), "{}", r.unwrap_err());
|
2016-02-02 12:43:53 +00:00
|
|
|
let m = r.unwrap();
|
2022-06-10 01:03:28 +00:00
|
|
|
assert!(m.contains_id("o"));
|
2022-05-24 15:16:50 +00:00
|
|
|
assert_eq!(m.get_many::<String>("o").unwrap().count(), 297); // i.e. more than u8
|
2016-02-02 12:43:53 +00:00
|
|
|
}
|
|
|
|
|
2015-08-27 21:03:45 +00:00
|
|
|
#[test]
|
|
|
|
fn opts_using_long_space() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let r = Command::new("opts")
|
2022-11-24 13:54:25 +00:00
|
|
|
.args([
|
2021-11-19 20:33:11 +00:00
|
|
|
arg!(--flag [flag] "some flag"),
|
|
|
|
arg!(--color [color] "some other flag"),
|
2018-01-25 04:05:05 +00:00
|
|
|
])
|
2018-01-31 20:15:01 +00:00
|
|
|
.try_get_matches_from(vec!["", "--flag", "some", "--color", "other"]);
|
2021-12-27 19:57:38 +00:00
|
|
|
assert!(r.is_ok(), "{}", r.unwrap_err());
|
2016-01-26 07:05:32 +00:00
|
|
|
let m = r.unwrap();
|
2022-06-10 01:03:28 +00:00
|
|
|
assert!(m.contains_id("flag"));
|
2022-05-24 15:16:50 +00:00
|
|
|
assert_eq!(
|
|
|
|
m.get_one::<String>("flag").map(|v| v.as_str()).unwrap(),
|
|
|
|
"some"
|
|
|
|
);
|
2022-06-10 01:03:28 +00:00
|
|
|
assert!(m.contains_id("color"));
|
2022-05-24 15:16:50 +00:00
|
|
|
assert_eq!(
|
|
|
|
m.get_one::<String>("color").map(|v| v.as_str()).unwrap(),
|
|
|
|
"other"
|
|
|
|
);
|
2015-08-27 21:03:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn opts_using_long_equals() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let r = Command::new("opts")
|
2022-11-24 13:54:25 +00:00
|
|
|
.args([
|
2021-11-19 20:33:11 +00:00
|
|
|
arg!(--flag [flag] "some flag"),
|
|
|
|
arg!(--color [color] "some other flag"),
|
2018-01-25 04:05:05 +00:00
|
|
|
])
|
2018-01-31 20:15:01 +00:00
|
|
|
.try_get_matches_from(vec!["", "--flag=some", "--color=other"]);
|
2021-12-27 19:57:38 +00:00
|
|
|
assert!(r.is_ok(), "{}", r.unwrap_err());
|
2016-01-26 07:05:32 +00:00
|
|
|
let m = r.unwrap();
|
2022-06-10 01:03:28 +00:00
|
|
|
assert!(m.contains_id("flag"));
|
2022-05-24 15:16:50 +00:00
|
|
|
assert_eq!(
|
|
|
|
m.get_one::<String>("flag").map(|v| v.as_str()).unwrap(),
|
|
|
|
"some"
|
|
|
|
);
|
2022-06-10 01:03:28 +00:00
|
|
|
assert!(m.contains_id("color"));
|
2022-05-24 15:16:50 +00:00
|
|
|
assert_eq!(
|
|
|
|
m.get_one::<String>("color").map(|v| v.as_str()).unwrap(),
|
|
|
|
"other"
|
|
|
|
);
|
2015-08-27 21:03:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn opts_using_mixed() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let r = Command::new("opts")
|
2022-11-24 13:54:25 +00:00
|
|
|
.args([
|
2021-11-19 20:33:11 +00:00
|
|
|
arg!(-f --flag [flag] "some flag"),
|
|
|
|
arg!(-c --color [color] "some other flag"),
|
2018-01-25 04:05:05 +00:00
|
|
|
])
|
2018-01-31 20:15:01 +00:00
|
|
|
.try_get_matches_from(vec!["", "-f", "some", "--color", "other"]);
|
2021-12-27 19:57:38 +00:00
|
|
|
assert!(r.is_ok(), "{}", r.unwrap_err());
|
2016-01-26 07:05:32 +00:00
|
|
|
let m = r.unwrap();
|
2022-06-10 01:03:28 +00:00
|
|
|
assert!(m.contains_id("flag"));
|
2022-05-24 15:16:50 +00:00
|
|
|
assert_eq!(
|
|
|
|
m.get_one::<String>("flag").map(|v| v.as_str()).unwrap(),
|
|
|
|
"some"
|
|
|
|
);
|
2022-06-10 01:03:28 +00:00
|
|
|
assert!(m.contains_id("color"));
|
2022-05-24 15:16:50 +00:00
|
|
|
assert_eq!(
|
|
|
|
m.get_one::<String>("color").map(|v| v.as_str()).unwrap(),
|
|
|
|
"other"
|
|
|
|
);
|
2016-01-26 07:05:32 +00:00
|
|
|
}
|
2015-08-27 21:03:45 +00:00
|
|
|
|
2016-01-26 07:05:32 +00:00
|
|
|
#[test]
|
|
|
|
fn opts_using_mixed2() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let r = Command::new("opts")
|
2022-11-24 13:54:25 +00:00
|
|
|
.args([
|
2021-11-19 20:33:11 +00:00
|
|
|
arg!(-f --flag [flag] "some flag"),
|
|
|
|
arg!(-c --color [color] "some other flag"),
|
2018-01-25 04:05:05 +00:00
|
|
|
])
|
2018-01-31 20:15:01 +00:00
|
|
|
.try_get_matches_from(vec!["", "--flag=some", "-c", "other"]);
|
2021-12-27 19:57:38 +00:00
|
|
|
assert!(r.is_ok(), "{}", r.unwrap_err());
|
2016-01-26 07:05:32 +00:00
|
|
|
let m = r.unwrap();
|
2022-06-10 01:03:28 +00:00
|
|
|
assert!(m.contains_id("flag"));
|
2022-05-24 15:16:50 +00:00
|
|
|
assert_eq!(
|
|
|
|
m.get_one::<String>("flag").map(|v| v.as_str()).unwrap(),
|
|
|
|
"some"
|
|
|
|
);
|
2022-06-10 01:03:28 +00:00
|
|
|
assert!(m.contains_id("color"));
|
2022-05-24 15:16:50 +00:00
|
|
|
assert_eq!(
|
|
|
|
m.get_one::<String>("color").map(|v| v.as_str()).unwrap(),
|
|
|
|
"other"
|
|
|
|
);
|
2016-01-21 05:18:53 +00:00
|
|
|
}
|
2016-02-09 14:05:19 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn default_values_user_value() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let r = Command::new("df")
|
2021-11-19 20:33:11 +00:00
|
|
|
.arg(arg!(o: -o [opt] "some opt").default_value("default"))
|
2018-01-31 20:15:01 +00:00
|
|
|
.try_get_matches_from(vec!["", "-o", "value"]);
|
2021-12-27 19:57:38 +00:00
|
|
|
assert!(r.is_ok(), "{}", r.unwrap_err());
|
2016-02-09 14:05:19 +00:00
|
|
|
let m = r.unwrap();
|
2022-06-10 01:03:28 +00:00
|
|
|
assert!(m.contains_id("o"));
|
2022-05-24 15:16:50 +00:00
|
|
|
assert_eq!(
|
|
|
|
m.get_one::<String>("o").map(|v| v.as_str()).unwrap(),
|
|
|
|
"value"
|
|
|
|
);
|
2016-02-09 14:05:19 +00:00
|
|
|
}
|
2016-05-09 03:20:50 +00:00
|
|
|
|
2016-06-29 17:30:30 +00:00
|
|
|
#[test]
|
|
|
|
fn multiple_vals_pos_arg_equals() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let r = Command::new("mvae")
|
2021-11-19 20:33:11 +00:00
|
|
|
.arg(arg!(o: -o [opt] ... "some opt"))
|
|
|
|
.arg(arg!([file] "some file"))
|
2018-01-31 20:15:01 +00:00
|
|
|
.try_get_matches_from(vec!["", "-o=1", "some"]);
|
2021-12-27 19:57:38 +00:00
|
|
|
assert!(r.is_ok(), "{}", r.unwrap_err());
|
2016-06-29 17:30:30 +00:00
|
|
|
let m = r.unwrap();
|
2022-06-10 01:03:28 +00:00
|
|
|
assert!(m.contains_id("o"));
|
2022-05-24 15:16:50 +00:00
|
|
|
assert_eq!(m.get_one::<String>("o").map(|v| v.as_str()).unwrap(), "1");
|
2022-06-10 01:03:28 +00:00
|
|
|
assert!(m.contains_id("file"));
|
2022-05-24 15:16:50 +00:00
|
|
|
assert_eq!(
|
|
|
|
m.get_one::<String>("file").map(|v| v.as_str()).unwrap(),
|
|
|
|
"some"
|
|
|
|
);
|
2016-06-29 17:30:30 +00:00
|
|
|
}
|
|
|
|
|
2016-06-30 03:01:41 +00:00
|
|
|
#[test]
|
|
|
|
fn require_delims_no_delim() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let r = Command::new("mvae")
|
2022-08-03 19:48:50 +00:00
|
|
|
.arg(arg!(o: -o [opt] ... "some opt").value_delimiter(','))
|
2021-11-19 20:33:11 +00:00
|
|
|
.arg(arg!([file] "some file"))
|
2018-01-31 20:15:01 +00:00
|
|
|
.try_get_matches_from(vec!["mvae", "-o", "1", "2", "some"]);
|
2016-06-30 03:01:41 +00:00
|
|
|
assert!(r.is_err());
|
|
|
|
let err = r.unwrap_err();
|
2022-01-25 22:19:28 +00:00
|
|
|
assert_eq!(err.kind(), ErrorKind::UnknownArgument);
|
2016-06-30 03:01:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn require_delims() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let r = Command::new("mvae")
|
2022-09-12 21:59:57 +00:00
|
|
|
.arg(
|
|
|
|
arg!(o: -o <opt> "some opt")
|
|
|
|
.value_delimiter(',')
|
|
|
|
.required(true),
|
|
|
|
)
|
2021-11-19 20:33:11 +00:00
|
|
|
.arg(arg!([file] "some file"))
|
2018-01-31 20:15:01 +00:00
|
|
|
.try_get_matches_from(vec!["", "-o", "1,2", "some"]);
|
2021-12-27 19:57:38 +00:00
|
|
|
assert!(r.is_ok(), "{}", r.unwrap_err());
|
2016-06-30 03:01:41 +00:00
|
|
|
let m = r.unwrap();
|
2022-06-10 01:03:28 +00:00
|
|
|
assert!(m.contains_id("o"));
|
2022-05-24 15:16:50 +00:00
|
|
|
assert_eq!(
|
|
|
|
m.get_many::<String>("o")
|
|
|
|
.unwrap()
|
|
|
|
.map(|v| v.as_str())
|
|
|
|
.collect::<Vec<_>>(),
|
2022-11-24 13:54:25 +00:00
|
|
|
["1", "2"]
|
2022-05-24 15:16:50 +00:00
|
|
|
);
|
2022-06-10 01:03:28 +00:00
|
|
|
assert!(m.contains_id("file"));
|
2022-05-24 15:16:50 +00:00
|
|
|
assert_eq!(
|
|
|
|
m.get_one::<String>("file").map(|v| v.as_str()).unwrap(),
|
|
|
|
"some"
|
|
|
|
);
|
2016-06-30 03:01:41 +00:00
|
|
|
}
|
|
|
|
|
2016-11-20 01:40:53 +00:00
|
|
|
#[test]
|
|
|
|
fn leading_hyphen_pass() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let r = Command::new("mvae")
|
2021-11-19 20:33:11 +00:00
|
|
|
.arg(
|
|
|
|
arg!(o: -o <opt> "some opt")
|
2022-09-12 21:59:57 +00:00
|
|
|
.required(true)
|
2022-08-03 16:20:07 +00:00
|
|
|
.num_args(1..)
|
2021-11-29 14:56:30 +00:00
|
|
|
.allow_hyphen_values(true),
|
2021-11-19 20:33:11 +00:00
|
|
|
)
|
2018-01-31 20:15:01 +00:00
|
|
|
.try_get_matches_from(vec!["", "-o", "-2", "3"]);
|
2021-12-27 19:57:38 +00:00
|
|
|
assert!(r.is_ok(), "{}", r.unwrap_err());
|
2016-11-20 01:40:53 +00:00
|
|
|
let m = r.unwrap();
|
2022-06-10 01:03:28 +00:00
|
|
|
assert!(m.contains_id("o"));
|
2022-05-24 15:16:50 +00:00
|
|
|
assert_eq!(
|
|
|
|
m.get_many::<String>("o")
|
|
|
|
.unwrap()
|
|
|
|
.map(|v| v.as_str())
|
|
|
|
.collect::<Vec<_>>(),
|
2022-11-24 13:54:25 +00:00
|
|
|
["-2", "3"]
|
2022-05-24 15:16:50 +00:00
|
|
|
);
|
2016-11-20 01:40:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn leading_hyphen_fail() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let r = Command::new("mvae")
|
2022-09-12 21:59:57 +00:00
|
|
|
.arg(arg!(o: -o <opt> "some opt").required(true))
|
2018-01-31 20:15:01 +00:00
|
|
|
.try_get_matches_from(vec!["", "-o", "-2"]);
|
2016-11-20 01:40:53 +00:00
|
|
|
assert!(r.is_err());
|
|
|
|
let m = r.unwrap_err();
|
2022-01-25 22:19:28 +00:00
|
|
|
assert_eq!(m.kind(), ErrorKind::UnknownArgument);
|
2016-11-20 01:40:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn leading_hyphen_with_flag_after() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let r = Command::new("mvae")
|
2021-11-19 20:33:11 +00:00
|
|
|
.arg(
|
|
|
|
arg!(o: -o <opt> "some opt")
|
2022-09-12 21:59:57 +00:00
|
|
|
.required(true)
|
2022-08-03 16:20:07 +00:00
|
|
|
.num_args(1..)
|
2021-11-29 14:56:30 +00:00
|
|
|
.allow_hyphen_values(true),
|
2021-11-19 20:33:11 +00:00
|
|
|
)
|
2022-06-10 01:03:28 +00:00
|
|
|
.arg(arg!(f: -f "some flag").action(ArgAction::SetTrue))
|
2018-01-31 20:15:01 +00:00
|
|
|
.try_get_matches_from(vec!["", "-o", "-2", "-f"]);
|
2021-12-27 19:57:38 +00:00
|
|
|
assert!(r.is_ok(), "{}", r.unwrap_err());
|
2016-11-20 01:40:53 +00:00
|
|
|
let m = r.unwrap();
|
2022-06-10 01:03:28 +00:00
|
|
|
assert!(m.contains_id("o"));
|
2022-05-24 15:16:50 +00:00
|
|
|
assert_eq!(
|
|
|
|
m.get_many::<String>("o")
|
|
|
|
.unwrap()
|
|
|
|
.map(|v| v.as_str())
|
|
|
|
.collect::<Vec<_>>(),
|
2022-11-24 13:54:25 +00:00
|
|
|
["-2", "-f"]
|
2022-05-24 15:16:50 +00:00
|
|
|
);
|
2022-06-10 01:03:28 +00:00
|
|
|
assert!(!*m.get_one::<bool>("f").expect("defaulted by clap"));
|
2016-11-20 01:40:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn leading_hyphen_with_flag_before() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let r = Command::new("mvae")
|
2021-11-29 14:56:30 +00:00
|
|
|
.arg(arg!(o: -o [opt] ... "some opt").allow_hyphen_values(true))
|
2022-06-10 01:03:28 +00:00
|
|
|
.arg(arg!(f: -f "some flag").action(ArgAction::SetTrue))
|
2018-01-31 20:15:01 +00:00
|
|
|
.try_get_matches_from(vec!["", "-f", "-o", "-2"]);
|
2021-12-27 19:57:38 +00:00
|
|
|
assert!(r.is_ok(), "{}", r.unwrap_err());
|
2016-11-20 01:40:53 +00:00
|
|
|
let m = r.unwrap();
|
2022-06-10 01:03:28 +00:00
|
|
|
assert!(m.contains_id("o"));
|
2022-05-24 15:16:50 +00:00
|
|
|
assert_eq!(
|
|
|
|
m.get_many::<String>("o")
|
|
|
|
.unwrap()
|
|
|
|
.map(|v| v.as_str())
|
|
|
|
.collect::<Vec<_>>(),
|
2022-11-24 13:54:25 +00:00
|
|
|
["-2"]
|
2022-05-24 15:16:50 +00:00
|
|
|
);
|
2022-06-10 01:03:28 +00:00
|
|
|
assert!(*m.get_one::<bool>("f").expect("defaulted by clap"));
|
2016-11-20 01:40:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn leading_hyphen_with_only_pos_follows() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let r = Command::new("mvae")
|
2018-01-25 04:05:05 +00:00
|
|
|
.arg(
|
2021-11-19 20:33:11 +00:00
|
|
|
arg!(o: -o [opt] ... "some opt")
|
2022-07-26 00:17:01 +00:00
|
|
|
.action(ArgAction::Set)
|
2021-11-29 14:56:30 +00:00
|
|
|
.allow_hyphen_values(true),
|
2018-01-25 04:05:05 +00:00
|
|
|
)
|
2021-11-19 20:33:11 +00:00
|
|
|
.arg(arg!([arg] "some arg"))
|
2018-01-31 20:15:01 +00:00
|
|
|
.try_get_matches_from(vec!["", "-o", "-2", "--", "val"]);
|
2023-05-04 01:40:21 +00:00
|
|
|
assert!(r.is_ok(), "{r:?}");
|
2016-11-20 01:40:53 +00:00
|
|
|
let m = r.unwrap();
|
2022-06-10 01:03:28 +00:00
|
|
|
assert!(m.contains_id("o"));
|
2022-05-24 15:16:50 +00:00
|
|
|
assert_eq!(
|
|
|
|
m.get_many::<String>("o")
|
|
|
|
.unwrap()
|
|
|
|
.map(|v| v.as_str())
|
|
|
|
.collect::<Vec<_>>(),
|
2022-11-24 13:54:25 +00:00
|
|
|
["-2"]
|
2022-05-24 15:16:50 +00:00
|
|
|
);
|
|
|
|
assert_eq!(m.get_one::<String>("arg").map(|v| v.as_str()), Some("val"));
|
2016-11-20 01:40:53 +00:00
|
|
|
}
|
|
|
|
|
2016-05-09 03:20:50 +00:00
|
|
|
#[test]
|
2018-01-25 04:05:05 +00:00
|
|
|
#[cfg(feature = "suggestions")]
|
2022-09-19 14:59:04 +00:00
|
|
|
#[cfg(feature = "error-context")]
|
2016-05-09 03:20:50 +00:00
|
|
|
fn did_you_mean() {
|
2022-09-19 14:59:04 +00:00
|
|
|
static DYM: &str = "\
|
2023-01-14 03:29:49 +00:00
|
|
|
error: unexpected argument '--optio' found
|
2022-09-19 14:59:04 +00:00
|
|
|
|
2023-03-28 01:41:21 +00:00
|
|
|
tip: a similar argument exists: '--option'
|
2022-09-19 14:59:04 +00:00
|
|
|
|
|
|
|
Usage: clap-test --option <opt>... [positional] [positional2] [positional3]...
|
|
|
|
|
2023-01-03 19:22:35 +00:00
|
|
|
For more information, try '--help'.
|
2022-09-19 14:59:04 +00:00
|
|
|
";
|
|
|
|
|
2022-04-29 20:32:25 +00:00
|
|
|
utils::assert_output(utils::complex_app(), "clap-test --optio=foo", DYM, true);
|
2016-05-09 03:20:50 +00:00
|
|
|
}
|
2016-12-30 21:25:48 +00:00
|
|
|
|
2017-09-14 17:18:45 +00:00
|
|
|
#[test]
|
|
|
|
fn issue_1047_min_zero_vals_default_val() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let m = Command::new("foo")
|
2017-09-14 17:18:45 +00:00
|
|
|
.arg(
|
2020-05-14 20:50:56 +00:00
|
|
|
Arg::new("del")
|
2018-07-23 19:09:42 +00:00
|
|
|
.short('d')
|
2017-09-14 17:18:45 +00:00
|
|
|
.long("del")
|
2022-07-26 00:17:01 +00:00
|
|
|
.action(ArgAction::Set)
|
2021-11-29 14:56:30 +00:00
|
|
|
.require_equals(true)
|
2022-08-03 16:20:07 +00:00
|
|
|
.num_args(0..)
|
2020-10-13 21:21:01 +00:00
|
|
|
.default_missing_value("default"),
|
2017-09-14 17:18:45 +00:00
|
|
|
)
|
2021-12-27 18:56:12 +00:00
|
|
|
.try_get_matches_from(vec!["foo", "-d"])
|
|
|
|
.unwrap();
|
2022-05-24 15:16:50 +00:00
|
|
|
assert_eq!(
|
|
|
|
m.get_one::<String>("del").map(|v| v.as_str()),
|
|
|
|
Some("default")
|
|
|
|
);
|
2017-11-13 22:05:28 +00:00
|
|
|
}
|
|
|
|
|
2019-04-05 02:06:23 +00:00
|
|
|
fn issue_1105_setup(argv: Vec<&'static str>) -> Result<ArgMatches, clap::Error> {
|
2022-02-12 03:48:29 +00:00
|
|
|
Command::new("opts")
|
2022-09-12 21:59:57 +00:00
|
|
|
.arg(arg!(-o --option <opt> "some option").required(true))
|
2021-11-19 20:33:11 +00:00
|
|
|
.arg(arg!(--flag "some flag"))
|
2018-01-31 20:15:01 +00:00
|
|
|
.try_get_matches_from(argv)
|
2017-11-13 22:05:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn issue_1105_empty_value_long_fail() {
|
2022-02-14 21:47:20 +00:00
|
|
|
let r = issue_1105_setup(vec!["cmd", "--option", "--flag"]);
|
2017-11-13 22:05:28 +00:00
|
|
|
assert!(r.is_err());
|
2022-07-22 14:03:28 +00:00
|
|
|
assert_eq!(r.unwrap_err().kind(), ErrorKind::InvalidValue);
|
2017-11-13 22:05:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn issue_1105_empty_value_long_explicit() {
|
2022-02-14 21:47:20 +00:00
|
|
|
let r = issue_1105_setup(vec!["cmd", "--option", ""]);
|
2021-12-27 19:57:38 +00:00
|
|
|
assert!(r.is_ok(), "{}", r.unwrap_err());
|
2017-11-13 22:05:28 +00:00
|
|
|
let m = r.unwrap();
|
2022-05-24 15:16:50 +00:00
|
|
|
assert_eq!(m.get_one::<String>("option").map(|v| v.as_str()), Some(""));
|
2017-11-13 22:05:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn issue_1105_empty_value_long_equals() {
|
2022-02-14 21:47:20 +00:00
|
|
|
let r = issue_1105_setup(vec!["cmd", "--option="]);
|
2021-12-27 19:57:38 +00:00
|
|
|
assert!(r.is_ok(), "{}", r.unwrap_err());
|
2017-11-13 22:05:28 +00:00
|
|
|
let m = r.unwrap();
|
2022-05-24 15:16:50 +00:00
|
|
|
assert_eq!(m.get_one::<String>("option").map(|v| v.as_str()), Some(""));
|
2017-11-13 22:05:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn issue_1105_empty_value_short_fail() {
|
2022-02-14 21:47:20 +00:00
|
|
|
let r = issue_1105_setup(vec!["cmd", "-o", "--flag"]);
|
2017-11-13 22:05:28 +00:00
|
|
|
assert!(r.is_err());
|
2022-07-22 14:03:28 +00:00
|
|
|
assert_eq!(r.unwrap_err().kind(), ErrorKind::InvalidValue);
|
2017-11-13 22:05:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn issue_1105_empty_value_short_explicit() {
|
2022-02-14 21:47:20 +00:00
|
|
|
let r = issue_1105_setup(vec!["cmd", "-o", ""]);
|
2021-12-27 19:57:38 +00:00
|
|
|
assert!(r.is_ok(), "{}", r.unwrap_err());
|
2017-11-13 22:05:28 +00:00
|
|
|
let m = r.unwrap();
|
2022-05-24 15:16:50 +00:00
|
|
|
assert_eq!(m.get_one::<String>("option").map(|v| v.as_str()), Some(""));
|
2017-11-13 22:05:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn issue_1105_empty_value_short_equals() {
|
2022-02-14 21:47:20 +00:00
|
|
|
let r = issue_1105_setup(vec!["cmd", "-o="]);
|
2021-12-27 19:57:38 +00:00
|
|
|
assert!(r.is_ok(), "{}", r.unwrap_err());
|
2017-11-13 22:05:28 +00:00
|
|
|
let m = r.unwrap();
|
2022-05-24 15:16:50 +00:00
|
|
|
assert_eq!(m.get_one::<String>("option").map(|v| v.as_str()), Some(""));
|
2017-11-13 22:05:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn issue_1105_empty_value_short_explicit_no_space() {
|
2022-02-14 21:47:20 +00:00
|
|
|
let r = issue_1105_setup(vec!["cmd", "-o", ""]);
|
2021-12-27 19:57:38 +00:00
|
|
|
assert!(r.is_ok(), "{}", r.unwrap_err());
|
2017-11-13 22:05:28 +00:00
|
|
|
let m = r.unwrap();
|
2022-05-24 15:16:50 +00:00
|
|
|
assert_eq!(m.get_one::<String>("option").map(|v| v.as_str()), Some(""));
|
2017-11-13 22:05:28 +00:00
|
|
|
}
|
2020-04-12 15:44:48 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[cfg(feature = "suggestions")]
|
2022-09-19 14:59:04 +00:00
|
|
|
#[cfg(feature = "error-context")]
|
2020-04-12 15:44:48 +00:00
|
|
|
fn issue_1073_suboptimal_flag_suggestion() {
|
2022-09-19 14:59:04 +00:00
|
|
|
static DYM_ISSUE_1073: &str = "\
|
2023-01-14 03:29:49 +00:00
|
|
|
error: unexpected argument '--files-without-matches' found
|
2022-09-19 14:59:04 +00:00
|
|
|
|
2023-03-28 01:41:21 +00:00
|
|
|
tip: a similar argument exists: '--files-without-match'
|
2022-09-19 14:59:04 +00:00
|
|
|
|
|
|
|
Usage: ripgrep-616 --files-without-match
|
|
|
|
|
2023-01-03 19:22:35 +00:00
|
|
|
For more information, try '--help'.
|
2022-09-19 14:59:04 +00:00
|
|
|
";
|
|
|
|
|
2022-02-14 21:47:20 +00:00
|
|
|
let cmd = Command::new("ripgrep-616")
|
2022-07-26 00:17:01 +00:00
|
|
|
.arg(
|
|
|
|
Arg::new("files-with-matches")
|
|
|
|
.long("files-with-matches")
|
|
|
|
.action(ArgAction::SetTrue),
|
|
|
|
)
|
|
|
|
.arg(
|
|
|
|
Arg::new("files-without-match")
|
|
|
|
.long("files-without-match")
|
|
|
|
.action(ArgAction::SetTrue),
|
|
|
|
);
|
2022-04-29 20:32:25 +00:00
|
|
|
utils::assert_output(
|
2022-02-14 21:47:20 +00:00
|
|
|
cmd,
|
2020-04-12 15:44:48 +00:00
|
|
|
"ripgrep-616 --files-without-matches",
|
|
|
|
DYM_ISSUE_1073,
|
2022-04-29 20:32:25 +00:00
|
|
|
true,
|
|
|
|
);
|
2020-04-12 15:44:48 +00:00
|
|
|
}
|
2020-04-20 15:29:29 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn short_non_ascii_no_space() {
|
2022-02-14 21:47:20 +00:00
|
|
|
let matches = Command::new("cmd")
|
2022-09-12 21:59:57 +00:00
|
|
|
.arg(arg!(opt: -'磨' <opt>).required(true))
|
2022-11-24 13:54:25 +00:00
|
|
|
.try_get_matches_from(["test", "-磨VALUE"])
|
2021-12-27 18:56:12 +00:00
|
|
|
.unwrap();
|
2020-04-20 15:29:29 +00:00
|
|
|
|
2022-05-24 15:16:50 +00:00
|
|
|
assert_eq!(
|
|
|
|
"VALUE",
|
|
|
|
matches
|
|
|
|
.get_one::<String>("opt")
|
|
|
|
.map(|v| v.as_str())
|
|
|
|
.unwrap()
|
|
|
|
);
|
2020-04-20 15:29:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn short_eq_val_starts_with_eq() {
|
2022-02-14 21:47:20 +00:00
|
|
|
let matches = Command::new("cmd")
|
2022-09-12 21:59:57 +00:00
|
|
|
.arg(arg!(opt: -f <opt>).required(true))
|
2022-11-24 13:54:25 +00:00
|
|
|
.try_get_matches_from(["test", "-f==value"])
|
2021-12-27 18:56:12 +00:00
|
|
|
.unwrap();
|
2020-04-20 15:29:29 +00:00
|
|
|
|
2022-05-24 15:16:50 +00:00
|
|
|
assert_eq!(
|
|
|
|
"=value",
|
|
|
|
matches
|
|
|
|
.get_one::<String>("opt")
|
|
|
|
.map(|v| v.as_str())
|
|
|
|
.unwrap()
|
|
|
|
);
|
2020-04-20 15:29:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn long_eq_val_starts_with_eq() {
|
2022-02-14 21:47:20 +00:00
|
|
|
let matches = Command::new("cmd")
|
2022-09-12 21:59:57 +00:00
|
|
|
.arg(arg!(opt: --foo <opt>).required(true))
|
2022-11-24 13:54:25 +00:00
|
|
|
.try_get_matches_from(["test", "--foo==value"])
|
2021-12-27 18:56:12 +00:00
|
|
|
.unwrap();
|
2020-04-20 15:29:29 +00:00
|
|
|
|
2022-05-24 15:16:50 +00:00
|
|
|
assert_eq!(
|
|
|
|
"=value",
|
|
|
|
matches
|
|
|
|
.get_one::<String>("opt")
|
|
|
|
.map(|v| v.as_str())
|
|
|
|
.unwrap()
|
|
|
|
);
|
2020-04-20 15:29:29 +00:00
|
|
|
}
|
2020-10-09 18:43:46 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn issue_2022_get_flags_misuse() {
|
2022-02-14 21:47:20 +00:00
|
|
|
let cmd = Command::new("test")
|
2022-02-07 21:15:40 +00:00
|
|
|
.next_help_heading(Some("test"))
|
2020-10-09 18:43:46 +00:00
|
|
|
.arg(Arg::new("a").long("a").default_value("32"));
|
2022-11-24 13:54:25 +00:00
|
|
|
let matches = cmd.try_get_matches_from([""]).unwrap();
|
2022-05-24 15:16:50 +00:00
|
|
|
assert!(matches.get_one::<String>("a").map(|v| v.as_str()).is_some())
|
2020-10-09 18:43:46 +00:00
|
|
|
}
|
2021-01-20 17:09:20 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn issue_2279() {
|
2022-02-14 21:47:20 +00:00
|
|
|
let before_help_heading = Command::new("cmd")
|
2021-01-20 17:09:20 +00:00
|
|
|
.arg(Arg::new("foo").short('f').default_value("bar"))
|
2022-02-07 21:15:40 +00:00
|
|
|
.next_help_heading(Some("This causes default_value to be ignored"))
|
2022-11-24 13:54:25 +00:00
|
|
|
.try_get_matches_from([""])
|
2021-12-27 18:56:12 +00:00
|
|
|
.unwrap();
|
2021-01-20 17:09:20 +00:00
|
|
|
|
2022-05-24 15:16:50 +00:00
|
|
|
assert_eq!(
|
|
|
|
before_help_heading
|
|
|
|
.get_one::<String>("foo")
|
|
|
|
.map(|v| v.as_str()),
|
|
|
|
Some("bar")
|
|
|
|
);
|
2021-01-20 17:09:20 +00:00
|
|
|
|
2022-02-14 21:47:20 +00:00
|
|
|
let after_help_heading = Command::new("cmd")
|
2022-02-07 21:15:40 +00:00
|
|
|
.next_help_heading(Some("This causes default_value to be ignored"))
|
2021-01-20 17:09:20 +00:00
|
|
|
.arg(Arg::new("foo").short('f').default_value("bar"))
|
2022-11-24 13:54:25 +00:00
|
|
|
.try_get_matches_from([""])
|
2021-12-27 18:56:12 +00:00
|
|
|
.unwrap();
|
2021-01-20 17:09:20 +00:00
|
|
|
|
2022-05-24 15:16:50 +00:00
|
|
|
assert_eq!(
|
|
|
|
after_help_heading
|
|
|
|
.get_one::<String>("foo")
|
|
|
|
.map(|v| v.as_str()),
|
|
|
|
Some("bar")
|
|
|
|
);
|
2021-01-20 17:09:20 +00:00
|
|
|
}
|
2021-06-05 11:18:06 +00:00
|
|
|
|
|
|
|
#[test]
|
2022-12-29 02:15:44 +00:00
|
|
|
fn infer_long_arg_pass() {
|
2022-02-14 21:47:20 +00:00
|
|
|
let cmd = Command::new("test")
|
2022-02-10 17:51:40 +00:00
|
|
|
.infer_long_args(true)
|
2022-06-10 01:03:28 +00:00
|
|
|
.arg(
|
|
|
|
Arg::new("racetrack")
|
|
|
|
.long("racetrack")
|
|
|
|
.alias("autobahn")
|
|
|
|
.action(ArgAction::SetTrue),
|
|
|
|
)
|
2022-07-26 00:17:01 +00:00
|
|
|
.arg(Arg::new("racecar").long("racecar").action(ArgAction::Set));
|
2021-06-05 11:18:06 +00:00
|
|
|
|
2022-02-14 21:47:20 +00:00
|
|
|
let matches = cmd
|
2021-12-27 18:56:12 +00:00
|
|
|
.clone()
|
2022-11-24 13:54:25 +00:00
|
|
|
.try_get_matches_from(["test", "--racec=hello"])
|
2021-12-27 18:56:12 +00:00
|
|
|
.unwrap();
|
2022-06-10 01:03:28 +00:00
|
|
|
assert!(!*matches
|
|
|
|
.get_one::<bool>("racetrack")
|
|
|
|
.expect("defaulted by clap"));
|
2022-05-24 15:16:50 +00:00
|
|
|
assert_eq!(
|
|
|
|
matches.get_one::<String>("racecar").map(|v| v.as_str()),
|
|
|
|
Some("hello")
|
|
|
|
);
|
2021-06-05 11:18:06 +00:00
|
|
|
|
2022-02-14 21:47:20 +00:00
|
|
|
let matches = cmd
|
2021-12-27 18:56:12 +00:00
|
|
|
.clone()
|
2022-11-24 13:54:25 +00:00
|
|
|
.try_get_matches_from(["test", "--racet"])
|
2021-12-27 18:56:12 +00:00
|
|
|
.unwrap();
|
2022-06-10 01:03:28 +00:00
|
|
|
assert!(*matches
|
|
|
|
.get_one::<bool>("racetrack")
|
|
|
|
.expect("defaulted by clap"));
|
2022-05-24 15:16:50 +00:00
|
|
|
assert_eq!(
|
|
|
|
matches.get_one::<String>("racecar").map(|v| v.as_str()),
|
|
|
|
None
|
|
|
|
);
|
2021-06-05 11:18:06 +00:00
|
|
|
|
2022-02-14 21:47:20 +00:00
|
|
|
let matches = cmd
|
2021-12-27 18:56:12 +00:00
|
|
|
.clone()
|
2022-11-24 13:54:25 +00:00
|
|
|
.try_get_matches_from(["test", "--auto"])
|
2021-12-27 18:56:12 +00:00
|
|
|
.unwrap();
|
2022-06-10 01:03:28 +00:00
|
|
|
assert!(*matches
|
|
|
|
.get_one::<bool>("racetrack")
|
|
|
|
.expect("defaulted by clap"));
|
2022-05-24 15:16:50 +00:00
|
|
|
assert_eq!(
|
|
|
|
matches.get_one::<String>("racecar").map(|v| v.as_str()),
|
|
|
|
None
|
|
|
|
);
|
2021-06-05 11:18:06 +00:00
|
|
|
|
2022-02-14 21:47:20 +00:00
|
|
|
let cmd = Command::new("test")
|
2022-02-10 17:51:40 +00:00
|
|
|
.infer_long_args(true)
|
2022-06-10 01:03:28 +00:00
|
|
|
.arg(Arg::new("arg").long("arg").action(ArgAction::SetTrue));
|
2021-06-05 11:18:06 +00:00
|
|
|
|
2022-11-24 13:54:25 +00:00
|
|
|
let matches = cmd.clone().try_get_matches_from(["test", "--"]).unwrap();
|
2022-06-10 01:03:28 +00:00
|
|
|
assert!(!*matches.get_one::<bool>("arg").expect("defaulted by clap"));
|
2021-06-05 11:18:06 +00:00
|
|
|
|
2022-11-24 13:54:25 +00:00
|
|
|
let matches = cmd.clone().try_get_matches_from(["test", "--a"]).unwrap();
|
2022-06-10 01:03:28 +00:00
|
|
|
assert!(*matches.get_one::<bool>("arg").expect("defaulted by clap"));
|
2021-06-05 11:18:06 +00:00
|
|
|
}
|
2022-12-29 02:15:44 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn infer_long_arg_pass_conflicts_exact_match() {
|
|
|
|
let cmd = Command::new("test")
|
|
|
|
.infer_long_args(true)
|
|
|
|
.arg(Arg::new("arg").long("arg").action(ArgAction::SetTrue))
|
|
|
|
.arg(Arg::new("arg2").long("arg2").action(ArgAction::SetTrue));
|
|
|
|
|
|
|
|
let matches = cmd.clone().try_get_matches_from(["test", "--arg"]).unwrap();
|
|
|
|
assert!(*matches.get_one::<bool>("arg").expect("defaulted by clap"));
|
|
|
|
|
|
|
|
let matches = cmd
|
|
|
|
.clone()
|
|
|
|
.try_get_matches_from(["test", "--arg2"])
|
|
|
|
.unwrap();
|
|
|
|
assert!(*matches.get_one::<bool>("arg2").expect("defaulted by clap"));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn infer_long_arg_pass_conflicting_aliases() {
|
|
|
|
let cmd = Command::new("test").infer_long_args(true).arg(
|
|
|
|
Arg::new("abc-123")
|
|
|
|
.long("abc-123")
|
|
|
|
.aliases(["a", "abc-xyz"])
|
|
|
|
.action(ArgAction::SetTrue),
|
|
|
|
);
|
|
|
|
|
|
|
|
let matches = cmd.clone().try_get_matches_from(["test", "--ab"]).unwrap();
|
|
|
|
assert!(*matches
|
|
|
|
.get_one::<bool>("abc-123")
|
|
|
|
.expect("defaulted by clap"));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn infer_long_arg_fail_conflicts() {
|
|
|
|
let cmd = Command::new("test")
|
|
|
|
.infer_long_args(true)
|
|
|
|
.arg(
|
|
|
|
Arg::new("abc-123")
|
|
|
|
.long("abc-123")
|
|
|
|
.action(ArgAction::SetTrue),
|
|
|
|
)
|
|
|
|
.arg(
|
|
|
|
Arg::new("abc-xyz")
|
|
|
|
.long("abc-xyz")
|
|
|
|
.action(ArgAction::SetTrue),
|
|
|
|
);
|
|
|
|
|
|
|
|
let error = cmd
|
|
|
|
.clone()
|
|
|
|
.try_get_matches_from(["test", "--abc"])
|
|
|
|
.unwrap_err();
|
|
|
|
assert_eq!(error.kind(), ErrorKind::UnknownArgument);
|
|
|
|
}
|