2022-02-12 03:48:29 +00:00
|
|
|
use clap::{arg, Arg, ArgMatches, Command};
|
2020-05-21 18:06:45 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn opt_missing() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let r = Command::new("df")
|
2020-05-21 18:06:45 +00:00
|
|
|
.arg(
|
|
|
|
Arg::new("color")
|
|
|
|
.long("color")
|
|
|
|
.default_value("auto")
|
|
|
|
.min_values(0)
|
|
|
|
.require_equals(true)
|
|
|
|
.default_missing_value("always"),
|
|
|
|
)
|
|
|
|
.try_get_matches_from(vec![""]);
|
2021-12-27 19:57:38 +00:00
|
|
|
assert!(r.is_ok(), "{}", r.unwrap_err());
|
2020-05-21 18:06:45 +00:00
|
|
|
let m = r.unwrap();
|
|
|
|
assert!(m.is_present("color"));
|
|
|
|
assert_eq!(m.value_of("color").unwrap(), "auto");
|
|
|
|
assert_eq!(m.occurrences_of("color"), 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn opt_present_with_missing_value() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let r = Command::new("df")
|
2020-05-21 18:06:45 +00:00
|
|
|
.arg(
|
|
|
|
Arg::new("color")
|
|
|
|
.long("color")
|
|
|
|
.default_value("auto")
|
|
|
|
.min_values(0)
|
|
|
|
.require_equals(true)
|
|
|
|
.default_missing_value("always"),
|
|
|
|
)
|
|
|
|
.try_get_matches_from(vec!["", "--color"]);
|
2021-12-27 19:57:38 +00:00
|
|
|
assert!(r.is_ok(), "{}", r.unwrap_err());
|
2020-05-21 18:06:45 +00:00
|
|
|
let m = r.unwrap();
|
|
|
|
assert!(m.is_present("color"));
|
|
|
|
assert_eq!(m.value_of("color").unwrap(), "always");
|
|
|
|
assert_eq!(m.occurrences_of("color"), 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn opt_present_with_value() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let r = Command::new("df")
|
2020-05-21 18:06:45 +00:00
|
|
|
.arg(
|
|
|
|
Arg::new("color")
|
|
|
|
.long("color")
|
|
|
|
.default_value("auto")
|
|
|
|
.min_values(0)
|
|
|
|
.require_equals(true)
|
|
|
|
.default_missing_value("always"),
|
|
|
|
)
|
|
|
|
.try_get_matches_from(vec!["", "--color=never"]);
|
2021-12-27 19:57:38 +00:00
|
|
|
assert!(r.is_ok(), "{}", r.unwrap_err());
|
2020-05-21 18:06:45 +00:00
|
|
|
let m = r.unwrap();
|
|
|
|
assert!(m.is_present("color"));
|
|
|
|
assert_eq!(m.value_of("color").unwrap(), "never");
|
|
|
|
assert_eq!(m.occurrences_of("color"), 1);
|
|
|
|
}
|
|
|
|
|
2021-10-25 17:36:01 +00:00
|
|
|
#[test]
|
|
|
|
fn opt_present_with_empty_value() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let r = Command::new("df")
|
2021-10-25 17:36:01 +00:00
|
|
|
.arg(
|
|
|
|
Arg::new("color")
|
|
|
|
.long("color")
|
|
|
|
.default_value("auto")
|
|
|
|
.require_equals(true)
|
|
|
|
.default_missing_value("always"),
|
|
|
|
)
|
|
|
|
.try_get_matches_from(vec!["", "--color="]);
|
2021-12-27 19:57:38 +00:00
|
|
|
assert!(r.is_ok(), "{}", r.unwrap_err());
|
2021-10-25 17:36:01 +00:00
|
|
|
let m = r.unwrap();
|
|
|
|
assert!(m.is_present("color"));
|
|
|
|
assert_eq!(m.value_of("color").unwrap(), "");
|
|
|
|
assert_eq!(m.occurrences_of("color"), 1);
|
|
|
|
}
|
2020-05-21 18:06:45 +00:00
|
|
|
|
|
|
|
//## `default_value`/`default_missing_value` non-interaction checks
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn opt_default() {
|
|
|
|
// assert no change to usual argument handling when adding default_missing_value()
|
2022-02-14 21:47:20 +00:00
|
|
|
let r = Command::new("cmd")
|
2020-05-21 18:06:45 +00:00
|
|
|
.arg(
|
2021-11-19 20:33:11 +00:00
|
|
|
arg!(o: -o [opt] "some opt")
|
2020-05-21 18:06:45 +00:00
|
|
|
.default_value("default")
|
|
|
|
.default_missing_value("default_missing"),
|
|
|
|
)
|
|
|
|
.try_get_matches_from(vec![""]);
|
2021-12-27 19:57:38 +00:00
|
|
|
assert!(r.is_ok(), "{}", r.unwrap_err());
|
2020-05-21 18:06:45 +00:00
|
|
|
let m = r.unwrap();
|
|
|
|
assert!(m.is_present("o"));
|
|
|
|
assert_eq!(m.value_of("o").unwrap(), "default");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn opt_default_user_override() {
|
|
|
|
// assert no change to usual argument handling when adding default_missing_value()
|
2022-02-14 21:47:20 +00:00
|
|
|
let r = Command::new("cmd")
|
2020-05-21 18:06:45 +00:00
|
|
|
.arg(
|
2021-11-19 20:33:11 +00:00
|
|
|
arg!(o: -o [opt] "some opt")
|
2020-05-21 18:06:45 +00:00
|
|
|
.default_value("default")
|
|
|
|
.default_missing_value("default_missing"),
|
|
|
|
)
|
|
|
|
.try_get_matches_from(vec!["", "-o=value"]);
|
2021-12-27 19:57:38 +00:00
|
|
|
assert!(r.is_ok(), "{}", r.unwrap_err());
|
2020-05-21 18:06:45 +00:00
|
|
|
let m = r.unwrap();
|
|
|
|
assert!(m.is_present("o"));
|
|
|
|
assert_eq!(m.value_of("o").unwrap(), "value");
|
|
|
|
}
|
2021-07-31 08:55:54 +00:00
|
|
|
|
|
|
|
#[test]
|
2021-08-02 21:10:51 +00:00
|
|
|
#[allow(clippy::bool_assert_comparison)]
|
2021-07-31 08:55:54 +00:00
|
|
|
fn default_missing_value_flag_value() {
|
2022-02-14 21:47:20 +00:00
|
|
|
let cmd = Command::new("test").arg(
|
2021-07-31 08:55:54 +00:00
|
|
|
Arg::new("flag")
|
|
|
|
.long("flag")
|
|
|
|
.takes_value(true)
|
|
|
|
.default_missing_value("true"),
|
|
|
|
);
|
|
|
|
|
|
|
|
fn flag_value(m: ArgMatches) -> bool {
|
|
|
|
match m.value_of("flag") {
|
|
|
|
None => false,
|
|
|
|
Some(x) => x.parse().expect("non boolean value"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
assert_eq!(
|
2022-02-14 21:47:20 +00:00
|
|
|
flag_value(cmd.clone().try_get_matches_from(&["test"]).unwrap()),
|
2021-12-27 18:56:12 +00:00
|
|
|
false
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
flag_value(
|
2022-02-14 21:47:20 +00:00
|
|
|
cmd.clone()
|
2021-12-27 18:56:12 +00:00
|
|
|
.try_get_matches_from(&["test", "--flag"])
|
|
|
|
.unwrap()
|
|
|
|
),
|
2021-07-31 08:55:54 +00:00
|
|
|
true
|
|
|
|
);
|
|
|
|
assert_eq!(
|
2021-12-27 18:56:12 +00:00
|
|
|
flag_value(
|
2022-02-14 21:47:20 +00:00
|
|
|
cmd.clone()
|
2021-12-27 18:56:12 +00:00
|
|
|
.try_get_matches_from(&["test", "--flag=true"])
|
|
|
|
.unwrap()
|
|
|
|
),
|
2021-07-31 08:55:54 +00:00
|
|
|
true
|
|
|
|
);
|
|
|
|
assert_eq!(
|
2021-12-27 18:56:12 +00:00
|
|
|
flag_value(
|
2022-02-14 21:47:20 +00:00
|
|
|
cmd.clone()
|
2021-12-27 18:56:12 +00:00
|
|
|
.try_get_matches_from(&["test", "--flag=false"])
|
|
|
|
.unwrap()
|
|
|
|
),
|
2021-07-31 08:55:54 +00:00
|
|
|
false
|
|
|
|
);
|
|
|
|
}
|
2022-02-08 18:59:30 +00:00
|
|
|
|
|
|
|
#[cfg(debug_assertions)]
|
|
|
|
#[test]
|
|
|
|
#[should_panic = "Argument `arg`'s default_missing_value=value doesn't match possible values"]
|
|
|
|
fn default_missing_values_are_possible_values() {
|
2022-02-12 03:48:29 +00:00
|
|
|
use clap::{Arg, Command};
|
2022-02-08 18:59:30 +00:00
|
|
|
|
2022-02-12 03:48:29 +00:00
|
|
|
let _ = Command::new("test")
|
2022-02-08 18:59:30 +00:00
|
|
|
.arg(
|
|
|
|
Arg::new("arg")
|
|
|
|
.possible_values(["one", "two"])
|
|
|
|
.default_missing_value("value"),
|
|
|
|
)
|
|
|
|
.try_get_matches();
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(debug_assertions)]
|
|
|
|
#[test]
|
|
|
|
#[should_panic = "Argument `arg`'s default_missing_value=value failed validation: invalid digit found in string"]
|
|
|
|
fn default_missing_values_are_valid() {
|
2022-02-12 03:48:29 +00:00
|
|
|
use clap::{Arg, Command};
|
2022-02-08 18:59:30 +00:00
|
|
|
|
2022-02-12 03:48:29 +00:00
|
|
|
let _ = Command::new("test")
|
2022-02-08 18:59:30 +00:00
|
|
|
.arg(
|
|
|
|
Arg::new("arg")
|
|
|
|
.validator(|val| val.parse::<u32>().map_err(|e| e.to_string()))
|
|
|
|
.default_missing_value("value"),
|
|
|
|
)
|
|
|
|
.try_get_matches();
|
|
|
|
}
|