2022-08-01 14:02:52 +00:00
|
|
|
use clap::{builder::PossibleValue, error::ErrorKind, Arg, ArgAction, Command};
|
2016-01-13 16:52:42 +00:00
|
|
|
|
2022-09-19 14:59:04 +00:00
|
|
|
#[cfg(feature = "error-context")]
|
|
|
|
use super::utils;
|
2016-05-09 03:20:50 +00:00
|
|
|
|
2016-01-13 16:52:42 +00:00
|
|
|
#[test]
|
|
|
|
fn possible_values_of_positional() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let m = Command::new("possible_values")
|
2022-05-24 01:16:02 +00:00
|
|
|
.arg(Arg::new("positional").index(1).value_parser(["test123"]))
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec!["myprog", "test123"]);
|
2016-01-13 16:52:42 +00:00
|
|
|
|
2021-12-27 19:57:38 +00:00
|
|
|
assert!(m.is_ok(), "{}", m.unwrap_err());
|
2016-01-13 16:52:42 +00:00
|
|
|
let m = m.unwrap();
|
|
|
|
|
2022-06-10 01:03:28 +00:00
|
|
|
assert!(m.contains_id("positional"));
|
2022-05-24 15:16:50 +00:00
|
|
|
assert_eq!(
|
|
|
|
m.get_one::<String>("positional").map(|v| v.as_str()),
|
|
|
|
Some("test123")
|
|
|
|
);
|
2016-01-13 16:52:42 +00:00
|
|
|
}
|
|
|
|
|
2021-09-19 10:29:09 +00:00
|
|
|
#[test]
|
|
|
|
fn possible_value_arg_value() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let m = Command::new("possible_values")
|
2021-09-19 10:29:09 +00:00
|
|
|
.arg(
|
2022-05-24 01:16:02 +00:00
|
|
|
Arg::new("arg_value")
|
|
|
|
.index(1)
|
|
|
|
.value_parser([PossibleValue::new("test123")
|
2021-11-29 16:45:30 +00:00
|
|
|
.hide(false)
|
2022-05-24 01:16:02 +00:00
|
|
|
.help("It's just a test")]),
|
2021-09-19 10:29:09 +00:00
|
|
|
)
|
|
|
|
.try_get_matches_from(vec!["myprog", "test123"]);
|
|
|
|
|
2021-12-27 19:57:38 +00:00
|
|
|
assert!(m.is_ok(), "{}", m.unwrap_err());
|
2021-09-19 10:29:09 +00:00
|
|
|
let m = m.unwrap();
|
|
|
|
|
2022-06-10 01:03:28 +00:00
|
|
|
assert!(m.contains_id("arg_value"));
|
2022-05-24 15:16:50 +00:00
|
|
|
assert_eq!(
|
|
|
|
m.get_one::<String>("arg_value").map(|v| v.as_str()),
|
|
|
|
Some("test123")
|
|
|
|
);
|
2021-09-19 10:29:09 +00:00
|
|
|
}
|
|
|
|
|
2016-01-13 16:52:42 +00:00
|
|
|
#[test]
|
|
|
|
fn possible_values_of_positional_fail() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let m = Command::new("possible_values")
|
2022-05-24 01:16:02 +00:00
|
|
|
.arg(Arg::new("positional").index(1).value_parser(["test123"]))
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec!["myprog", "notest"]);
|
2016-01-13 16:52:42 +00:00
|
|
|
|
|
|
|
assert!(m.is_err());
|
2022-01-25 22:19:28 +00:00
|
|
|
assert_eq!(m.unwrap_err().kind(), ErrorKind::InvalidValue);
|
2016-01-13 16:52:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn possible_values_of_positional_multiple() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let m = Command::new("possible_values")
|
2017-11-28 12:30:06 +00:00
|
|
|
.arg(
|
2020-05-14 20:50:56 +00:00
|
|
|
Arg::new("positional")
|
2017-11-28 12:30:06 +00:00
|
|
|
.index(1)
|
2022-07-26 00:17:01 +00:00
|
|
|
.action(ArgAction::Set)
|
2022-05-24 01:16:02 +00:00
|
|
|
.value_parser(["test123", "test321"])
|
2022-08-03 16:20:07 +00:00
|
|
|
.num_args(1..),
|
2017-11-28 12:30:06 +00:00
|
|
|
)
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec!["myprog", "test123", "test321"]);
|
2016-01-13 16:52:42 +00:00
|
|
|
|
2021-12-27 19:57:38 +00:00
|
|
|
assert!(m.is_ok(), "{}", m.unwrap_err());
|
2016-01-13 16:52:42 +00:00
|
|
|
let m = m.unwrap();
|
|
|
|
|
2022-06-10 01:03:28 +00:00
|
|
|
assert!(m.contains_id("positional"));
|
2017-11-28 12:30:06 +00:00
|
|
|
assert_eq!(
|
2022-05-24 15:16:50 +00:00
|
|
|
m.get_many::<String>("positional")
|
|
|
|
.unwrap()
|
|
|
|
.map(|v| v.as_str())
|
|
|
|
.collect::<Vec<_>>(),
|
2017-11-28 12:30:06 +00:00
|
|
|
vec!["test123", "test321"]
|
|
|
|
);
|
2016-01-13 16:52:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn possible_values_of_positional_multiple_fail() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let m = Command::new("possible_values")
|
2017-11-28 12:30:06 +00:00
|
|
|
.arg(
|
2020-05-14 20:50:56 +00:00
|
|
|
Arg::new("positional")
|
2017-11-28 12:30:06 +00:00
|
|
|
.index(1)
|
2022-07-26 00:17:01 +00:00
|
|
|
.action(ArgAction::Set)
|
2022-05-24 01:16:02 +00:00
|
|
|
.value_parser(["test123", "test321"])
|
2022-08-03 16:20:07 +00:00
|
|
|
.num_args(1..),
|
2017-11-28 12:30:06 +00:00
|
|
|
)
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec!["myprog", "test123", "notest"]);
|
2016-01-13 16:52:42 +00:00
|
|
|
|
|
|
|
assert!(m.is_err());
|
2022-01-25 22:19:28 +00:00
|
|
|
assert_eq!(m.unwrap_err().kind(), ErrorKind::InvalidValue);
|
2016-01-13 16:52:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn possible_values_of_option() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let m = Command::new("possible_values")
|
2017-11-28 12:30:06 +00:00
|
|
|
.arg(
|
2020-05-14 20:50:56 +00:00
|
|
|
Arg::new("option")
|
2018-07-23 19:09:42 +00:00
|
|
|
.short('o')
|
2022-05-04 20:38:06 +00:00
|
|
|
.long("option")
|
2022-07-26 00:17:01 +00:00
|
|
|
.action(ArgAction::Set)
|
2022-05-24 01:16:02 +00:00
|
|
|
.value_parser(["test123"]),
|
2017-11-28 12:30:06 +00:00
|
|
|
)
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec!["myprog", "--option", "test123"]);
|
2016-01-13 16:52:42 +00:00
|
|
|
|
2021-12-27 19:57:38 +00:00
|
|
|
assert!(m.is_ok(), "{}", m.unwrap_err());
|
2016-01-13 16:52:42 +00:00
|
|
|
let m = m.unwrap();
|
|
|
|
|
2022-06-10 01:03:28 +00:00
|
|
|
assert!(m.contains_id("option"));
|
2022-05-24 15:16:50 +00:00
|
|
|
assert_eq!(
|
|
|
|
m.get_one::<String>("option").map(|v| v.as_str()),
|
|
|
|
Some("test123")
|
|
|
|
);
|
2016-01-13 16:52:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn possible_values_of_option_fail() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let m = Command::new("possible_values")
|
2017-11-28 12:30:06 +00:00
|
|
|
.arg(
|
2020-05-14 20:50:56 +00:00
|
|
|
Arg::new("option")
|
2018-07-23 19:09:42 +00:00
|
|
|
.short('o')
|
2022-05-04 20:38:06 +00:00
|
|
|
.long("option")
|
2022-07-26 00:17:01 +00:00
|
|
|
.action(ArgAction::Set)
|
2022-05-24 01:16:02 +00:00
|
|
|
.value_parser(["test123"]),
|
2017-11-28 12:30:06 +00:00
|
|
|
)
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec!["myprog", "--option", "notest"]);
|
2016-01-13 16:52:42 +00:00
|
|
|
|
|
|
|
assert!(m.is_err());
|
2022-01-25 22:19:28 +00:00
|
|
|
assert_eq!(m.unwrap_err().kind(), ErrorKind::InvalidValue);
|
2016-01-13 16:52:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn possible_values_of_option_multiple() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let m = Command::new("possible_values")
|
2017-11-28 12:30:06 +00:00
|
|
|
.arg(
|
2020-05-14 20:50:56 +00:00
|
|
|
Arg::new("option")
|
2018-07-23 19:09:42 +00:00
|
|
|
.short('o')
|
2022-05-04 20:38:06 +00:00
|
|
|
.long("option")
|
2022-07-26 00:17:01 +00:00
|
|
|
.action(ArgAction::Set)
|
2022-05-24 01:16:02 +00:00
|
|
|
.value_parser(["test123", "test321"])
|
2022-06-07 18:48:48 +00:00
|
|
|
.action(ArgAction::Append),
|
2017-11-28 12:30:06 +00:00
|
|
|
)
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec!["", "--option", "test123", "--option", "test321"]);
|
2016-01-13 16:52:42 +00:00
|
|
|
|
2021-12-27 19:57:38 +00:00
|
|
|
assert!(m.is_ok(), "{}", m.unwrap_err());
|
2016-01-13 16:52:42 +00:00
|
|
|
let m = m.unwrap();
|
|
|
|
|
2022-06-10 01:03:28 +00:00
|
|
|
assert!(m.contains_id("option"));
|
2017-11-28 12:30:06 +00:00
|
|
|
assert_eq!(
|
2022-05-24 15:16:50 +00:00
|
|
|
m.get_many::<String>("option")
|
|
|
|
.unwrap()
|
|
|
|
.map(|v| v.as_str())
|
|
|
|
.collect::<Vec<_>>(),
|
2017-11-28 12:30:06 +00:00
|
|
|
vec!["test123", "test321"]
|
|
|
|
);
|
2016-01-13 16:52:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn possible_values_of_option_multiple_fail() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let m = Command::new("possible_values")
|
2017-11-28 12:30:06 +00:00
|
|
|
.arg(
|
2020-05-14 20:50:56 +00:00
|
|
|
Arg::new("option")
|
2018-07-23 19:09:42 +00:00
|
|
|
.short('o')
|
2022-05-04 20:38:06 +00:00
|
|
|
.long("option")
|
2022-07-26 00:17:01 +00:00
|
|
|
.action(ArgAction::Set)
|
2022-05-24 01:16:02 +00:00
|
|
|
.value_parser(["test123", "test321"])
|
2022-06-07 18:48:48 +00:00
|
|
|
.action(ArgAction::Append),
|
2017-11-28 12:30:06 +00:00
|
|
|
)
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec!["", "--option", "test123", "--option", "notest"]);
|
2016-01-13 16:52:42 +00:00
|
|
|
|
|
|
|
assert!(m.is_err());
|
2022-01-25 22:19:28 +00:00
|
|
|
assert_eq!(m.unwrap_err().kind(), ErrorKind::InvalidValue);
|
2016-01-13 16:52:42 +00:00
|
|
|
}
|
2016-05-09 03:20:50 +00:00
|
|
|
|
|
|
|
#[test]
|
2022-09-19 14:59:04 +00:00
|
|
|
#[cfg(feature = "error-context")]
|
2016-05-09 03:20:50 +00:00
|
|
|
fn possible_values_output() {
|
2022-09-19 14:59:04 +00:00
|
|
|
#[cfg(feature = "suggestions")]
|
|
|
|
static PV_ERROR: &str = "\
|
2023-01-06 23:01:34 +00:00
|
|
|
error: invalid value 'slo' for '-O <option>'
|
2022-09-19 14:59:04 +00:00
|
|
|
[possible values: slow, fast, \"ludicrous speed\"]
|
|
|
|
|
2023-03-28 01:18:18 +00:00
|
|
|
tip: value 'slow' exists
|
2022-09-19 14:59:04 +00:00
|
|
|
|
2023-01-03 19:22:35 +00:00
|
|
|
For more information, try '--help'.
|
2022-09-19 14:59:04 +00:00
|
|
|
";
|
|
|
|
|
|
|
|
#[cfg(not(feature = "suggestions"))]
|
|
|
|
static PV_ERROR: &str = "\
|
2023-01-06 23:01:34 +00:00
|
|
|
error: invalid value 'slo' for '-O <option>'
|
2022-09-19 14:59:04 +00:00
|
|
|
[possible values: slow, fast, \"ludicrous speed\"]
|
|
|
|
|
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(
|
2022-05-24 01:16:02 +00:00
|
|
|
Command::new("test").arg(
|
|
|
|
Arg::new("option")
|
|
|
|
.short('O')
|
2022-07-26 00:17:01 +00:00
|
|
|
.action(ArgAction::Set)
|
2022-05-24 01:16:02 +00:00
|
|
|
.value_parser(["slow", "fast", "ludicrous speed"]),
|
|
|
|
),
|
2017-11-28 12:30:06 +00:00
|
|
|
"clap-test -O slo",
|
|
|
|
PV_ERROR,
|
2022-04-29 20:32:25 +00:00
|
|
|
true,
|
|
|
|
);
|
2016-05-09 03:20:50 +00:00
|
|
|
}
|
2017-11-28 11:44:40 +00:00
|
|
|
|
2021-09-26 20:46:23 +00:00
|
|
|
#[test]
|
2022-09-19 14:59:04 +00:00
|
|
|
#[cfg(feature = "error-context")]
|
2021-09-26 20:46:23 +00:00
|
|
|
fn possible_values_alias_output() {
|
2022-09-19 14:59:04 +00:00
|
|
|
#[cfg(feature = "suggestions")]
|
|
|
|
static PV_ERROR: &str = "\
|
2023-01-06 23:01:34 +00:00
|
|
|
error: invalid value 'slo' for '-O <option>'
|
2022-09-19 14:59:04 +00:00
|
|
|
[possible values: slow, fast, \"ludicrous speed\"]
|
|
|
|
|
2023-03-28 01:18:18 +00:00
|
|
|
tip: value 'slow' exists
|
2022-09-19 14:59:04 +00:00
|
|
|
|
2023-01-03 19:22:35 +00:00
|
|
|
For more information, try '--help'.
|
2022-09-19 14:59:04 +00:00
|
|
|
";
|
|
|
|
|
|
|
|
#[cfg(not(feature = "suggestions"))]
|
|
|
|
static PV_ERROR: &str = "\
|
2023-01-06 23:01:34 +00:00
|
|
|
error: invalid value 'slo' for '-O <option>'
|
2022-09-19 14:59:04 +00:00
|
|
|
[possible values: slow, fast, \"ludicrous speed\"]
|
|
|
|
|
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(
|
2022-02-12 03:48:29 +00:00
|
|
|
Command::new("test").arg(
|
2021-09-26 20:46:23 +00:00
|
|
|
Arg::new("option")
|
|
|
|
.short('O')
|
2022-07-26 00:17:01 +00:00
|
|
|
.action(ArgAction::Set)
|
2022-05-24 01:16:02 +00:00
|
|
|
.value_parser([
|
|
|
|
"slow".into(),
|
|
|
|
PossibleValue::new("fast").alias("fost"),
|
|
|
|
PossibleValue::new("ludicrous speed").aliases(["ls", "lcs"]),
|
|
|
|
]),
|
2021-09-26 20:46:23 +00:00
|
|
|
),
|
|
|
|
"clap-test -O slo",
|
|
|
|
PV_ERROR,
|
2022-04-29 20:32:25 +00:00
|
|
|
true,
|
|
|
|
);
|
2021-09-26 20:46:23 +00:00
|
|
|
}
|
|
|
|
|
2021-09-19 10:29:09 +00:00
|
|
|
#[test]
|
2022-09-19 14:59:04 +00:00
|
|
|
#[cfg(feature = "error-context")]
|
2021-09-19 10:29:09 +00:00
|
|
|
fn possible_values_hidden_output() {
|
2022-09-19 14:59:04 +00:00
|
|
|
#[cfg(feature = "suggestions")]
|
|
|
|
static PV_ERROR: &str = "\
|
2023-01-06 23:01:34 +00:00
|
|
|
error: invalid value 'slo' for '-O <option>'
|
2022-09-19 14:59:04 +00:00
|
|
|
[possible values: slow, fast, \"ludicrous speed\"]
|
|
|
|
|
2023-03-28 01:18:18 +00:00
|
|
|
tip: value 'slow' exists
|
2022-09-19 14:59:04 +00:00
|
|
|
|
2023-01-03 19:22:35 +00:00
|
|
|
For more information, try '--help'.
|
2022-09-19 14:59:04 +00:00
|
|
|
";
|
|
|
|
|
|
|
|
#[cfg(not(feature = "suggestions"))]
|
|
|
|
static PV_ERROR: &str = "\
|
2023-01-06 23:01:34 +00:00
|
|
|
error: invalid value 'slo' for '-O <option>'
|
2022-09-19 14:59:04 +00:00
|
|
|
[possible values: slow, fast, \"ludicrous speed\"]
|
|
|
|
|
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(
|
2022-02-12 03:48:29 +00:00
|
|
|
Command::new("test").arg(
|
2021-09-19 10:29:09 +00:00
|
|
|
Arg::new("option")
|
|
|
|
.short('O')
|
2022-07-26 00:17:01 +00:00
|
|
|
.action(ArgAction::Set)
|
2022-05-24 01:16:02 +00:00
|
|
|
.value_parser([
|
|
|
|
"slow".into(),
|
|
|
|
"fast".into(),
|
|
|
|
PossibleValue::new("ludicrous speed"),
|
|
|
|
PossibleValue::new("forbidden speed").hide(true),
|
|
|
|
]),
|
2021-09-19 10:29:09 +00:00
|
|
|
),
|
|
|
|
"clap-test -O slo",
|
|
|
|
PV_ERROR,
|
2022-04-29 20:32:25 +00:00
|
|
|
true,
|
|
|
|
);
|
2021-09-19 10:29:09 +00:00
|
|
|
}
|
|
|
|
|
2020-09-24 14:02:10 +00:00
|
|
|
#[test]
|
2022-09-19 14:59:04 +00:00
|
|
|
#[cfg(feature = "error-context")]
|
2020-09-24 14:02:10 +00:00
|
|
|
fn escaped_possible_values_output() {
|
2022-09-19 14:59:04 +00:00
|
|
|
#[cfg(feature = "suggestions")]
|
|
|
|
static PV_ERROR_ESCAPED: &str = "\
|
2023-01-06 23:01:34 +00:00
|
|
|
error: invalid value 'ludicrous' for '-O <option>'
|
2022-09-19 14:59:04 +00:00
|
|
|
[possible values: slow, fast, \"ludicrous speed\"]
|
|
|
|
|
2023-03-28 01:18:18 +00:00
|
|
|
tip: value 'ludicrous speed' exists
|
2022-09-19 14:59:04 +00:00
|
|
|
|
2023-01-03 19:22:35 +00:00
|
|
|
For more information, try '--help'.
|
2022-09-19 14:59:04 +00:00
|
|
|
";
|
|
|
|
|
|
|
|
#[cfg(not(feature = "suggestions"))]
|
|
|
|
static PV_ERROR_ESCAPED: &str = "\
|
2023-01-09 17:59:20 +00:00
|
|
|
error: invalid value 'ludicrous' for '-O <option>'
|
2022-09-19 14:59:04 +00:00
|
|
|
[possible values: slow, fast, \"ludicrous speed\"]
|
|
|
|
|
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(
|
2022-05-24 01:16:02 +00:00
|
|
|
Command::new("test").arg(
|
|
|
|
Arg::new("option")
|
|
|
|
.short('O')
|
2022-07-26 00:17:01 +00:00
|
|
|
.action(ArgAction::Set)
|
2022-05-24 01:16:02 +00:00
|
|
|
.value_parser(["slow", "fast", "ludicrous speed"]),
|
|
|
|
),
|
2020-09-24 14:02:10 +00:00
|
|
|
"clap-test -O ludicrous",
|
|
|
|
PV_ERROR_ESCAPED,
|
2022-04-29 20:32:25 +00:00
|
|
|
true,
|
|
|
|
);
|
2020-09-24 14:02:10 +00:00
|
|
|
}
|
|
|
|
|
2022-02-02 20:28:41 +00:00
|
|
|
#[test]
|
2022-09-19 14:59:04 +00:00
|
|
|
#[cfg(feature = "error-context")]
|
2022-02-02 20:28:41 +00:00
|
|
|
fn missing_possible_value_error() {
|
2022-09-19 14:59:04 +00:00
|
|
|
static MISSING_PV_ERROR: &str = "\
|
2023-01-06 23:01:34 +00:00
|
|
|
error: a value is required for '-O <option>' but none was supplied
|
2022-09-19 14:59:04 +00:00
|
|
|
[possible values: slow, fast, \"ludicrous speed\"]
|
|
|
|
|
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(
|
2022-02-12 03:48:29 +00:00
|
|
|
Command::new("test").arg(
|
2022-02-02 20:28:41 +00:00
|
|
|
Arg::new("option")
|
|
|
|
.short('O')
|
2022-07-26 00:17:01 +00:00
|
|
|
.action(ArgAction::Set)
|
2022-05-24 01:16:02 +00:00
|
|
|
.value_parser([
|
|
|
|
"slow".into(),
|
|
|
|
PossibleValue::new("fast").alias("fost"),
|
|
|
|
PossibleValue::new("ludicrous speed"),
|
|
|
|
PossibleValue::new("forbidden speed").hide(true),
|
|
|
|
]),
|
2022-02-02 20:28:41 +00:00
|
|
|
),
|
|
|
|
"clap-test -O",
|
|
|
|
MISSING_PV_ERROR,
|
2022-04-29 20:32:25 +00:00
|
|
|
true,
|
|
|
|
);
|
2022-02-02 20:28:41 +00:00
|
|
|
}
|
|
|
|
|
2021-09-26 20:46:23 +00:00
|
|
|
#[test]
|
|
|
|
fn alias() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let m = Command::new("pv")
|
2021-09-26 20:46:23 +00:00
|
|
|
.arg(
|
|
|
|
Arg::new("option")
|
|
|
|
.short('o')
|
2022-05-04 20:38:06 +00:00
|
|
|
.long("option")
|
2022-07-26 00:17:01 +00:00
|
|
|
.action(ArgAction::Set)
|
2022-05-24 01:16:02 +00:00
|
|
|
.value_parser([PossibleValue::new("test123").alias("123"), "test321".into()])
|
2021-11-29 16:34:42 +00:00
|
|
|
.ignore_case(true),
|
2021-09-26 20:46:23 +00:00
|
|
|
)
|
|
|
|
.try_get_matches_from(vec!["pv", "--option", "123"]);
|
|
|
|
|
2021-12-27 19:57:38 +00:00
|
|
|
assert!(m.is_ok(), "{}", m.unwrap_err());
|
2022-05-24 15:16:50 +00:00
|
|
|
assert!(m
|
|
|
|
.unwrap()
|
|
|
|
.get_one::<String>("option")
|
|
|
|
.map(|v| v.as_str())
|
|
|
|
.unwrap()
|
|
|
|
.eq("123"));
|
2021-09-26 20:46:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn aliases() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let m = Command::new("pv")
|
2021-09-26 20:46:23 +00:00
|
|
|
.arg(
|
|
|
|
Arg::new("option")
|
|
|
|
.short('o')
|
2022-05-04 20:38:06 +00:00
|
|
|
.long("option")
|
2022-07-26 00:17:01 +00:00
|
|
|
.action(ArgAction::Set)
|
2022-05-24 01:16:02 +00:00
|
|
|
.value_parser([
|
|
|
|
PossibleValue::new("test123").aliases(["1", "2", "3"]),
|
|
|
|
"test321".into(),
|
|
|
|
])
|
2021-11-29 16:34:42 +00:00
|
|
|
.ignore_case(true),
|
2021-09-26 20:46:23 +00:00
|
|
|
)
|
|
|
|
.try_get_matches_from(vec!["pv", "--option", "2"]);
|
|
|
|
|
2021-12-27 19:57:38 +00:00
|
|
|
assert!(m.is_ok(), "{}", m.unwrap_err());
|
2022-05-24 15:16:50 +00:00
|
|
|
assert!(m
|
|
|
|
.unwrap()
|
|
|
|
.get_one::<String>("option")
|
|
|
|
.map(|v| v.as_str())
|
|
|
|
.unwrap()
|
|
|
|
.eq("2"));
|
2021-09-26 20:46:23 +00:00
|
|
|
}
|
|
|
|
|
2017-11-28 11:44:40 +00:00
|
|
|
#[test]
|
2021-11-29 16:34:42 +00:00
|
|
|
fn ignore_case() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let m = Command::new("pv")
|
2017-11-28 12:30:06 +00:00
|
|
|
.arg(
|
2020-05-14 20:50:56 +00:00
|
|
|
Arg::new("option")
|
2018-07-23 19:09:42 +00:00
|
|
|
.short('o')
|
2022-05-04 20:38:06 +00:00
|
|
|
.long("option")
|
2022-07-26 00:17:01 +00:00
|
|
|
.action(ArgAction::Set)
|
2022-05-24 01:16:02 +00:00
|
|
|
.value_parser(["test123", "test321"])
|
2021-11-29 16:34:42 +00:00
|
|
|
.ignore_case(true),
|
2017-11-28 12:30:06 +00:00
|
|
|
)
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec!["pv", "--option", "TeSt123"]);
|
2017-11-28 11:44:40 +00:00
|
|
|
|
2021-12-27 19:57:38 +00:00
|
|
|
assert!(m.is_ok(), "{}", m.unwrap_err());
|
2018-11-14 17:05:06 +00:00
|
|
|
assert!(m
|
|
|
|
.unwrap()
|
2022-05-24 15:16:50 +00:00
|
|
|
.get_one::<String>("option")
|
|
|
|
.map(|v| v.as_str())
|
2018-11-14 17:05:06 +00:00
|
|
|
.unwrap()
|
|
|
|
.eq_ignore_ascii_case("test123"));
|
2017-11-28 11:44:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2021-11-29 16:34:42 +00:00
|
|
|
fn ignore_case_fail() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let m = Command::new("pv")
|
2017-11-28 12:30:06 +00:00
|
|
|
.arg(
|
2020-05-14 20:50:56 +00:00
|
|
|
Arg::new("option")
|
2018-07-23 19:09:42 +00:00
|
|
|
.short('o')
|
2022-05-04 20:38:06 +00:00
|
|
|
.long("option")
|
2022-07-26 00:17:01 +00:00
|
|
|
.action(ArgAction::Set)
|
2022-05-24 01:16:02 +00:00
|
|
|
.value_parser(["test123", "test321"]),
|
2017-11-28 12:30:06 +00:00
|
|
|
)
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec!["pv", "--option", "TeSt123"]);
|
2017-11-28 11:44:40 +00:00
|
|
|
|
|
|
|
assert!(m.is_err());
|
2022-01-25 22:19:28 +00:00
|
|
|
assert_eq!(m.unwrap_err().kind(), ErrorKind::InvalidValue);
|
2017-11-28 11:44:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2021-11-29 16:34:42 +00:00
|
|
|
fn ignore_case_multiple() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let m = Command::new("pv")
|
2017-11-28 12:30:06 +00:00
|
|
|
.arg(
|
2020-05-14 20:50:56 +00:00
|
|
|
Arg::new("option")
|
2018-07-23 19:09:42 +00:00
|
|
|
.short('o')
|
2022-05-04 20:38:06 +00:00
|
|
|
.long("option")
|
2022-07-26 00:17:01 +00:00
|
|
|
.action(ArgAction::Set)
|
2022-05-24 01:16:02 +00:00
|
|
|
.value_parser(["test123", "test321"])
|
2022-08-03 16:20:07 +00:00
|
|
|
.num_args(1..)
|
2021-11-29 16:34:42 +00:00
|
|
|
.ignore_case(true),
|
2017-11-28 12:30:06 +00:00
|
|
|
)
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec!["pv", "--option", "TeSt123", "teST123", "tESt321"]);
|
2017-11-28 11:44:40 +00:00
|
|
|
|
2021-12-27 19:57:38 +00:00
|
|
|
assert!(m.is_ok(), "{}", m.unwrap_err());
|
2017-11-28 12:30:06 +00:00
|
|
|
assert_eq!(
|
2022-05-24 15:16:50 +00:00
|
|
|
m.unwrap()
|
|
|
|
.get_many::<String>("option")
|
|
|
|
.unwrap()
|
|
|
|
.map(|v| v.as_str())
|
|
|
|
.collect::<Vec<_>>(),
|
2022-11-24 13:54:25 +00:00
|
|
|
["TeSt123", "teST123", "tESt321"]
|
2017-11-28 12:30:06 +00:00
|
|
|
);
|
2017-11-28 11:44:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2021-11-29 16:34:42 +00:00
|
|
|
fn ignore_case_multiple_fail() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let m = Command::new("pv")
|
2017-11-28 12:30:06 +00:00
|
|
|
.arg(
|
2020-05-14 20:50:56 +00:00
|
|
|
Arg::new("option")
|
2018-07-23 19:09:42 +00:00
|
|
|
.short('o')
|
2022-05-04 20:38:06 +00:00
|
|
|
.long("option")
|
2022-07-26 00:17:01 +00:00
|
|
|
.action(ArgAction::Set)
|
2022-05-24 01:16:02 +00:00
|
|
|
.value_parser(["test123", "test321"])
|
2022-08-03 16:20:07 +00:00
|
|
|
.num_args(1..),
|
2017-11-28 12:30:06 +00:00
|
|
|
)
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec!["pv", "--option", "test123", "teST123", "test321"]);
|
2017-11-28 11:44:40 +00:00
|
|
|
|
|
|
|
assert!(m.is_err());
|
2022-01-25 22:19:28 +00:00
|
|
|
assert_eq!(m.unwrap_err().kind(), ErrorKind::InvalidValue);
|
2017-11-28 12:30:06 +00:00
|
|
|
}
|