2016-01-13 16:52:42 +00:00
|
|
|
extern crate clap;
|
2016-05-10 02:46:09 +00:00
|
|
|
extern crate regex;
|
|
|
|
|
|
|
|
include!("../clap-test.rs");
|
2016-01-13 16:52:42 +00:00
|
|
|
|
2018-01-09 15:24:24 +00:00
|
|
|
#[allow(unused_imports)]
|
api: Adds Arg::case_insensitive(bool) which allows matching Arg::possible_values without worrying about ASCII case
When used with `Arg::possible_values` it allows the argument value to pass validation even if
the case differs from that of the specified `possible_value`.
```rust
let m = App::new("pv")
.arg(Arg::with_name("option")
.long("--option")
.takes_value(true)
.possible_value("test123")
.case_insensitive(true))
.get_matches_from(vec![
"pv", "--option", "TeSt123",
]);
assert!(m.value_of("option").unwrap().eq_ignore_ascii_case("test123"));
```
This setting also works when multiple values can be defined:
```rust
let m = App::new("pv")
.arg(Arg::with_name("option")
.short("-o")
.long("--option")
.takes_value(true)
.possible_value("test123")
.possible_value("test321")
.multiple(true)
.case_insensitive(true))
.get_matches_from(vec![
"pv", "--option", "TeSt123", "teST123", "tESt321"
]);
let matched_vals = m.values_of("option").unwrap().collect::<Vec<_>>();
assert_eq!(&*matched_vals, &["TeSt123", "teST123", "tESt321"]);
```
Closes #1118
2017-11-28 11:41:43 +00:00
|
|
|
use std::ascii::AsciiExt;
|
|
|
|
|
2016-01-21 05:18:53 +00:00
|
|
|
use clap::{App, Arg, ErrorKind};
|
2016-01-13 16:52:42 +00:00
|
|
|
|
2017-11-28 12:30:06 +00:00
|
|
|
#[cfg(feature = "suggestions")]
|
2016-05-09 03:20:50 +00:00
|
|
|
static PV_ERROR: &'static str = "error: 'slo' isn't a valid value for '--Option <option3>'
|
2018-02-06 03:56:28 +00:00
|
|
|
\t[possible values: fast, slow]
|
2016-05-09 03:20:50 +00:00
|
|
|
|
2016-07-05 20:01:04 +00:00
|
|
|
\tDid you mean 'slow'?
|
2016-05-09 03:20:50 +00:00
|
|
|
|
2016-10-05 18:06:32 +00:00
|
|
|
USAGE:
|
|
|
|
clap-test --Option <option3>
|
|
|
|
|
|
|
|
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"))]
|
2016-10-05 18:06:32 +00:00
|
|
|
static PV_ERROR: &'static str = "error: 'slo' isn't a valid value for '--Option <option3>'
|
2018-02-06 03:56:28 +00:00
|
|
|
\t[possible values: fast, slow]
|
2016-10-05 18:06:32 +00:00
|
|
|
|
|
|
|
|
2016-05-09 03:20:50 +00:00
|
|
|
USAGE:
|
|
|
|
clap-test --Option <option3>
|
|
|
|
|
|
|
|
For more information try --help";
|
|
|
|
|
2016-01-13 16:52:42 +00:00
|
|
|
#[test]
|
|
|
|
fn possible_values_of_positional() {
|
|
|
|
let m = App::new("possible_values")
|
2017-11-28 12:30:06 +00:00
|
|
|
.arg(
|
|
|
|
Arg::with_name("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"));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn possible_values_of_positional_fail() {
|
|
|
|
let m = App::new("possible_values")
|
2017-11-28 12:30:06 +00:00
|
|
|
.arg(
|
|
|
|
Arg::with_name("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(
|
|
|
|
Arg::with_name("positional")
|
|
|
|
.index(1)
|
|
|
|
.possible_value("test123")
|
|
|
|
.possible_value("test321")
|
|
|
|
.multiple(true),
|
|
|
|
)
|
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(
|
|
|
|
Arg::with_name("positional")
|
|
|
|
.index(1)
|
|
|
|
.possible_value("test123")
|
|
|
|
.possible_value("test321")
|
|
|
|
.multiple(true),
|
|
|
|
)
|
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(
|
|
|
|
Arg::with_name("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(
|
|
|
|
Arg::with_name("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(
|
|
|
|
Arg::with_name("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")
|
|
|
|
.multiple(true),
|
|
|
|
)
|
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(
|
|
|
|
Arg::with_name("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")
|
|
|
|
.multiple(true),
|
|
|
|
)
|
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() {
|
2017-11-28 12:30:06 +00:00
|
|
|
assert!(test::compare_output(
|
|
|
|
test::complex_app(),
|
|
|
|
"clap-test -O slo",
|
|
|
|
PV_ERROR,
|
|
|
|
true
|
|
|
|
));
|
2016-05-09 03:20:50 +00:00
|
|
|
}
|
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(
|
|
|
|
Arg::with_name("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());
|
2017-11-28 12:30:06 +00:00
|
|
|
assert!(
|
|
|
|
m.unwrap()
|
|
|
|
.value_of("option")
|
|
|
|
.unwrap()
|
|
|
|
.eq_ignore_ascii_case("test123")
|
|
|
|
);
|
2017-11-28 11:44:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn case_insensitive_faili() {
|
|
|
|
let m = App::new("pv")
|
2017-11-28 12:30:06 +00:00
|
|
|
.arg(
|
|
|
|
Arg::with_name("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(
|
|
|
|
Arg::with_name("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")
|
|
|
|
.multiple(true)
|
|
|
|
.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(
|
|
|
|
Arg::with_name("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")
|
|
|
|
.multiple(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_err());
|
|
|
|
assert_eq!(m.unwrap_err().kind, ErrorKind::InvalidValue);
|
2017-11-28 12:30:06 +00:00
|
|
|
}
|