2020-02-04 08:10:53 +00:00
|
|
|
mod utils;
|
2016-01-13 16:52:42 +00:00
|
|
|
|
2021-10-18 13:35:52 +00:00
|
|
|
use clap::{App, Arg, ErrorKind, PossibleValue};
|
2016-01-13 16:52:42 +00:00
|
|
|
|
2017-11-28 12:30:06 +00:00
|
|
|
#[cfg(feature = "suggestions")]
|
2020-09-24 14:02:10 +00:00
|
|
|
static PV_ERROR: &str = "error: \"slo\" isn't a valid value for '-O <option>'
|
|
|
|
\t[possible values: \"ludicrous speed\", fast, slow]
|
2016-05-09 03:20:50 +00:00
|
|
|
|
2020-09-24 14:02:10 +00:00
|
|
|
\tDid you mean \"slow\"?
|
2016-05-09 03:20:50 +00:00
|
|
|
|
2016-10-05 18:06:32 +00:00
|
|
|
USAGE:
|
2020-09-24 14:02:10 +00:00
|
|
|
clap-test -O <option>
|
2016-10-05 18:06:32 +00:00
|
|
|
|
2021-09-24 15:58:39 +00:00
|
|
|
For more information try --help
|
|
|
|
";
|
2017-01-03 04:05:23 +00:00
|
|
|
|
2017-11-28 12:30:06 +00:00
|
|
|
#[cfg(not(feature = "suggestions"))]
|
2020-09-24 14:02:10 +00:00
|
|
|
static PV_ERROR: &'static str = "error: \"slo\" isn't a valid value for '-O <option>'
|
|
|
|
\t[possible values: \"ludicrous speed\", fast, slow]
|
2016-10-05 18:06:32 +00:00
|
|
|
|
2016-05-09 03:20:50 +00:00
|
|
|
USAGE:
|
2020-09-24 14:02:10 +00:00
|
|
|
clap-test -O <option>
|
|
|
|
|
2021-09-24 15:58:39 +00:00
|
|
|
For more information try --help
|
|
|
|
";
|
2020-09-24 14:02:10 +00:00
|
|
|
|
|
|
|
#[cfg(feature = "suggestions")]
|
|
|
|
static PV_ERROR_ESCAPED: &str = "error: \"ludicrous\" isn't a valid value for '-O <option>'
|
|
|
|
\t[possible values: \"ludicrous speed\", fast, slow]
|
|
|
|
|
|
|
|
\tDid you mean \"ludicrous speed\"?
|
|
|
|
|
|
|
|
USAGE:
|
|
|
|
clap-test -O <option>
|
|
|
|
|
2021-09-24 15:58:39 +00:00
|
|
|
For more information try --help
|
|
|
|
";
|
2020-09-24 14:02:10 +00:00
|
|
|
|
|
|
|
#[cfg(not(feature = "suggestions"))]
|
|
|
|
static PV_ERROR_ESCAPED: &'static str = "error: \"ludicrous\" isn't a valid value for '-O <option>'
|
|
|
|
\t[possible values: \"ludicrous speed\", fast, slow]
|
|
|
|
|
|
|
|
USAGE:
|
|
|
|
clap-test -O <option>
|
2016-05-09 03:20:50 +00:00
|
|
|
|
2021-09-24 15:58:39 +00:00
|
|
|
For more information try --help
|
|
|
|
";
|
2016-05-09 03:20:50 +00:00
|
|
|
|
2016-01-13 16:52:42 +00:00
|
|
|
#[test]
|
|
|
|
fn possible_values_of_positional() {
|
|
|
|
let m = App::new("possible_values")
|
2020-05-14 20:50:56 +00:00
|
|
|
.arg(Arg::new("positional").index(1).possible_value("test123"))
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec!["myprog", "test123"]);
|
2016-01-13 16:52:42 +00:00
|
|
|
|
|
|
|
assert!(m.is_ok());
|
|
|
|
let m = m.unwrap();
|
|
|
|
|
|
|
|
assert!(m.is_present("positional"));
|
|
|
|
assert_eq!(m.value_of("positional"), Some("test123"));
|
|
|
|
}
|
|
|
|
|
2021-09-19 10:29:09 +00:00
|
|
|
#[test]
|
|
|
|
fn possible_value_arg_value() {
|
|
|
|
let m = App::new("possible_values")
|
|
|
|
.arg(
|
|
|
|
Arg::new("arg_value").index(1).possible_value(
|
2021-10-18 13:35:52 +00:00
|
|
|
PossibleValue::new("test123")
|
2021-09-19 10:29:09 +00:00
|
|
|
.hidden(false)
|
|
|
|
.about("It's just a test"),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
.try_get_matches_from(vec!["myprog", "test123"]);
|
|
|
|
|
|
|
|
assert!(m.is_ok());
|
|
|
|
let m = m.unwrap();
|
|
|
|
|
|
|
|
assert!(m.is_present("arg_value"));
|
|
|
|
assert_eq!(m.value_of("arg_value"), Some("test123"));
|
|
|
|
}
|
|
|
|
|
2016-01-13 16:52:42 +00:00
|
|
|
#[test]
|
|
|
|
fn possible_values_of_positional_fail() {
|
|
|
|
let m = App::new("possible_values")
|
2020-05-14 20:50:56 +00:00
|
|
|
.arg(Arg::new("positional").index(1).possible_value("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());
|
2016-01-21 05:18:53 +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() {
|
|
|
|
let m = App::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)
|
2021-02-24 15:07:57 +00:00
|
|
|
.takes_value(true)
|
2017-11-28 12:30:06 +00:00
|
|
|
.possible_value("test123")
|
|
|
|
.possible_value("test321")
|
2021-06-16 05:28:25 +00:00
|
|
|
.multiple_values(true),
|
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
|
|
|
|
|
|
|
assert!(m.is_ok());
|
|
|
|
let m = m.unwrap();
|
|
|
|
|
|
|
|
assert!(m.is_present("positional"));
|
2017-11-28 12:30:06 +00:00
|
|
|
assert_eq!(
|
|
|
|
m.values_of("positional").unwrap().collect::<Vec<_>>(),
|
|
|
|
vec!["test123", "test321"]
|
|
|
|
);
|
2016-01-13 16:52:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn possible_values_of_positional_multiple_fail() {
|
|
|
|
let m = App::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)
|
2021-02-24 15:07:57 +00:00
|
|
|
.takes_value(true)
|
2017-11-28 12:30:06 +00:00
|
|
|
.possible_value("test123")
|
|
|
|
.possible_value("test321")
|
2021-06-16 05:28:25 +00:00
|
|
|
.multiple_values(true),
|
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());
|
2016-01-21 05:18:53 +00:00
|
|
|
assert_eq!(m.unwrap_err().kind, ErrorKind::InvalidValue);
|
2016-01-13 16:52:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn possible_values_of_option() {
|
|
|
|
let m = App::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')
|
2017-11-28 12:30:06 +00:00
|
|
|
.long("--option")
|
|
|
|
.takes_value(true)
|
|
|
|
.possible_value("test123"),
|
|
|
|
)
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec!["myprog", "--option", "test123"]);
|
2016-01-13 16:52:42 +00:00
|
|
|
|
|
|
|
assert!(m.is_ok());
|
|
|
|
let m = m.unwrap();
|
|
|
|
|
|
|
|
assert!(m.is_present("option"));
|
|
|
|
assert_eq!(m.value_of("option"), Some("test123"));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn possible_values_of_option_fail() {
|
|
|
|
let m = App::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')
|
2017-11-28 12:30:06 +00:00
|
|
|
.long("--option")
|
|
|
|
.takes_value(true)
|
|
|
|
.possible_value("test123"),
|
|
|
|
)
|
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());
|
2016-01-21 05:18:53 +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() {
|
|
|
|
let m = App::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')
|
2017-11-28 12:30:06 +00:00
|
|
|
.long("--option")
|
|
|
|
.takes_value(true)
|
|
|
|
.possible_value("test123")
|
|
|
|
.possible_value("test321")
|
2021-06-16 05:28:25 +00:00
|
|
|
.multiple_occurrences(true),
|
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
|
|
|
|
|
|
|
assert!(m.is_ok());
|
|
|
|
let m = m.unwrap();
|
|
|
|
|
|
|
|
assert!(m.is_present("option"));
|
2017-11-28 12:30:06 +00:00
|
|
|
assert_eq!(
|
|
|
|
m.values_of("option").unwrap().collect::<Vec<_>>(),
|
|
|
|
vec!["test123", "test321"]
|
|
|
|
);
|
2016-01-13 16:52:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn possible_values_of_option_multiple_fail() {
|
|
|
|
let m = App::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')
|
2017-11-28 12:30:06 +00:00
|
|
|
.long("--option")
|
|
|
|
.takes_value(true)
|
|
|
|
.possible_value("test123")
|
|
|
|
.possible_value("test321")
|
2021-06-16 05:28:25 +00:00
|
|
|
.multiple_occurrences(true),
|
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());
|
2016-01-21 05:18:53 +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]
|
|
|
|
fn possible_values_output() {
|
2020-02-04 08:10:53 +00:00
|
|
|
assert!(utils::compare_output(
|
2021-09-19 10:29:09 +00:00
|
|
|
App::new("test").arg(Arg::new("option").short('O').possible_values([
|
2020-09-25 08:38:35 +00:00
|
|
|
"slow",
|
|
|
|
"fast",
|
|
|
|
"ludicrous speed"
|
|
|
|
])),
|
2017-11-28 12:30:06 +00:00
|
|
|
"clap-test -O slo",
|
|
|
|
PV_ERROR,
|
|
|
|
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]
|
|
|
|
fn possible_values_alias_output() {
|
|
|
|
assert!(utils::compare_output(
|
|
|
|
App::new("test").arg(
|
|
|
|
Arg::new("option")
|
|
|
|
.short('O')
|
|
|
|
.possible_value("slow")
|
2021-10-18 13:35:52 +00:00
|
|
|
.possible_value(PossibleValue::new("fast").alias("fost"))
|
|
|
|
.possible_value(PossibleValue::new("ludicrous speed").aliases(["ls", "lcs"]))
|
2021-09-26 20:46:23 +00:00
|
|
|
),
|
|
|
|
"clap-test -O slo",
|
|
|
|
PV_ERROR,
|
|
|
|
true
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
2021-09-19 10:29:09 +00:00
|
|
|
#[test]
|
|
|
|
fn possible_values_hidden_output() {
|
|
|
|
assert!(utils::compare_output(
|
|
|
|
App::new("test").arg(
|
|
|
|
Arg::new("option")
|
|
|
|
.short('O')
|
|
|
|
.possible_values(["slow", "fast"])
|
2021-10-18 13:35:52 +00:00
|
|
|
.possible_value(PossibleValue::new("ludicrous speed"))
|
|
|
|
.possible_value(PossibleValue::new("forbidden speed").hidden(true))
|
2021-09-19 10:29:09 +00:00
|
|
|
),
|
|
|
|
"clap-test -O slo",
|
|
|
|
PV_ERROR,
|
|
|
|
true
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
2020-09-24 14:02:10 +00:00
|
|
|
#[test]
|
|
|
|
fn escaped_possible_values_output() {
|
|
|
|
assert!(utils::compare_output(
|
2021-09-19 10:29:09 +00:00
|
|
|
App::new("test").arg(Arg::new("option").short('O').possible_values([
|
2020-09-25 08:38:35 +00:00
|
|
|
"slow",
|
|
|
|
"fast",
|
|
|
|
"ludicrous speed"
|
|
|
|
])),
|
2020-09-24 14:02:10 +00:00
|
|
|
"clap-test -O ludicrous",
|
|
|
|
PV_ERROR_ESCAPED,
|
|
|
|
true
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
2021-09-26 20:46:23 +00:00
|
|
|
#[test]
|
|
|
|
fn alias() {
|
|
|
|
let m = App::new("pv")
|
|
|
|
.arg(
|
|
|
|
Arg::new("option")
|
|
|
|
.short('o')
|
|
|
|
.long("--option")
|
|
|
|
.takes_value(true)
|
2021-10-18 13:35:52 +00:00
|
|
|
.possible_value(PossibleValue::new("test123").alias("123"))
|
2021-09-26 20:46:23 +00:00
|
|
|
.possible_value("test321")
|
|
|
|
.case_insensitive(true),
|
|
|
|
)
|
|
|
|
.try_get_matches_from(vec!["pv", "--option", "123"]);
|
|
|
|
|
|
|
|
assert!(m.is_ok());
|
|
|
|
assert!(m.unwrap().value_of("option").unwrap().eq("123"));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn aliases() {
|
|
|
|
let m = App::new("pv")
|
|
|
|
.arg(
|
|
|
|
Arg::new("option")
|
|
|
|
.short('o')
|
|
|
|
.long("--option")
|
|
|
|
.takes_value(true)
|
2021-10-18 13:35:52 +00:00
|
|
|
.possible_value(PossibleValue::new("test123").aliases(["1", "2", "3"]))
|
2021-09-26 20:46:23 +00:00
|
|
|
.possible_value("test321")
|
|
|
|
.case_insensitive(true),
|
|
|
|
)
|
|
|
|
.try_get_matches_from(vec!["pv", "--option", "2"]);
|
|
|
|
|
|
|
|
assert!(m.is_ok());
|
|
|
|
assert!(m.unwrap().value_of("option").unwrap().eq("2"));
|
|
|
|
}
|
|
|
|
|
2017-11-28 11:44:40 +00:00
|
|
|
#[test]
|
|
|
|
fn case_insensitive() {
|
|
|
|
let m = App::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')
|
2017-11-28 12:30:06 +00:00
|
|
|
.long("--option")
|
|
|
|
.takes_value(true)
|
|
|
|
.possible_value("test123")
|
|
|
|
.possible_value("test321")
|
|
|
|
.case_insensitive(true),
|
|
|
|
)
|
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_ok());
|
2018-11-14 17:05:06 +00:00
|
|
|
assert!(m
|
|
|
|
.unwrap()
|
|
|
|
.value_of("option")
|
|
|
|
.unwrap()
|
|
|
|
.eq_ignore_ascii_case("test123"));
|
2017-11-28 11:44:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2021-09-26 20:46:23 +00:00
|
|
|
fn case_insensitive_fail() {
|
2017-11-28 11:44:40 +00:00
|
|
|
let m = App::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')
|
2017-11-28 12:30:06 +00:00
|
|
|
.long("--option")
|
|
|
|
.takes_value(true)
|
|
|
|
.possible_value("test123")
|
|
|
|
.possible_value("test321"),
|
|
|
|
)
|
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());
|
|
|
|
assert_eq!(m.unwrap_err().kind, ErrorKind::InvalidValue);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn case_insensitive_multiple() {
|
|
|
|
let m = App::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')
|
2017-11-28 12:30:06 +00:00
|
|
|
.long("--option")
|
|
|
|
.takes_value(true)
|
|
|
|
.possible_value("test123")
|
|
|
|
.possible_value("test321")
|
2021-06-16 05:28:25 +00:00
|
|
|
.multiple_values(true)
|
2017-11-28 12:30:06 +00:00
|
|
|
.case_insensitive(true),
|
|
|
|
)
|
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_ok());
|
2017-11-28 12:30:06 +00:00
|
|
|
assert_eq!(
|
|
|
|
m.unwrap().values_of("option").unwrap().collect::<Vec<_>>(),
|
|
|
|
&["TeSt123", "teST123", "tESt321"]
|
|
|
|
);
|
2017-11-28 11:44:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn case_insensitive_multiple_fail() {
|
|
|
|
let m = App::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')
|
2017-11-28 12:30:06 +00:00
|
|
|
.long("--option")
|
|
|
|
.takes_value(true)
|
|
|
|
.possible_value("test123")
|
|
|
|
.possible_value("test321")
|
2021-06-16 05:28:25 +00:00
|
|
|
.multiple_values(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
|
|
|
|
|
|
|
assert!(m.is_err());
|
|
|
|
assert_eq!(m.unwrap_err().kind, ErrorKind::InvalidValue);
|
2017-11-28 12:30:06 +00:00
|
|
|
}
|