2022-07-26 00:17:01 +00:00
|
|
|
use clap::{arg, Arg, ArgAction, 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")
|
2022-08-03 16:20:07 +00:00
|
|
|
.num_args(0..=1)
|
2020-05-21 18:06:45 +00:00
|
|
|
.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();
|
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(),
|
|
|
|
"auto"
|
|
|
|
);
|
2022-05-27 12:04:21 +00:00
|
|
|
assert_eq!(
|
|
|
|
m.value_source("color").unwrap(),
|
2022-08-01 14:02:52 +00:00
|
|
|
clap::parser::ValueSource::DefaultValue
|
2022-05-27 12:04:21 +00:00
|
|
|
);
|
2022-06-04 13:27:39 +00:00
|
|
|
assert_eq!(m.index_of("color"), Some(1));
|
2020-05-21 18:06:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[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")
|
2022-08-03 16:20:07 +00:00
|
|
|
.num_args(0..=1)
|
2020-05-21 18:06:45 +00:00
|
|
|
.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();
|
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(),
|
|
|
|
"always"
|
|
|
|
);
|
2022-05-27 12:04:21 +00:00
|
|
|
assert_eq!(
|
|
|
|
m.value_source("color").unwrap(),
|
2022-08-01 14:02:52 +00:00
|
|
|
clap::parser::ValueSource::CommandLine
|
2022-05-27 12:04:21 +00:00
|
|
|
);
|
2022-06-04 13:27:39 +00:00
|
|
|
assert_eq!(m.index_of("color"), Some(2));
|
2020-05-21 18:06:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[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")
|
2022-08-03 16:20:07 +00:00
|
|
|
.num_args(0..=1)
|
2020-05-21 18:06:45 +00:00
|
|
|
.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();
|
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(),
|
|
|
|
"never"
|
|
|
|
);
|
2022-05-27 12:04:21 +00:00
|
|
|
assert_eq!(
|
|
|
|
m.value_source("color").unwrap(),
|
2022-08-01 14:02:52 +00:00
|
|
|
clap::parser::ValueSource::CommandLine
|
2022-05-27 12:04:21 +00:00
|
|
|
);
|
2022-06-04 13:27:39 +00:00
|
|
|
assert_eq!(m.index_of("color"), Some(2));
|
2020-05-21 18:06:45 +00:00
|
|
|
}
|
|
|
|
|
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();
|
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(),
|
|
|
|
""
|
|
|
|
);
|
2022-05-27 12:04:21 +00:00
|
|
|
assert_eq!(
|
|
|
|
m.value_source("color").unwrap(),
|
2022-08-01 14:02:52 +00:00
|
|
|
clap::parser::ValueSource::CommandLine
|
2022-05-27 12:04:21 +00:00
|
|
|
);
|
2022-06-04 13:27:39 +00:00
|
|
|
assert_eq!(m.index_of("color"), Some(2));
|
2021-10-25 17:36:01 +00:00
|
|
|
}
|
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();
|
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(),
|
|
|
|
"default"
|
|
|
|
);
|
2020-05-21 18:06:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[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();
|
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"
|
|
|
|
);
|
2020-05-21 18:06:45 +00:00
|
|
|
}
|
2021-07-31 08:55:54 +00:00
|
|
|
|
2022-07-28 01:23:11 +00:00
|
|
|
#[test]
|
|
|
|
fn default_missing_value_per_occurrence() {
|
|
|
|
// assert no change to usual argument handling when adding default_missing_value()
|
|
|
|
let r = Command::new("cmd")
|
|
|
|
.arg(
|
2022-07-28 02:00:31 +00:00
|
|
|
arg!(o: -o [opt] ... "some opt")
|
2022-07-28 01:23:11 +00:00
|
|
|
.default_value("default")
|
|
|
|
.default_missing_value("default_missing"),
|
|
|
|
)
|
|
|
|
.try_get_matches_from(vec!["", "-o", "-o=value", "-o"]);
|
|
|
|
assert!(r.is_ok(), "{}", r.unwrap_err());
|
|
|
|
let m = r.unwrap();
|
|
|
|
assert_eq!(
|
|
|
|
m.get_many::<String>("o")
|
|
|
|
.unwrap()
|
|
|
|
.map(|v| v.as_str())
|
|
|
|
.collect::<Vec<_>>(),
|
|
|
|
vec!["default_missing", "value", "default_missing"]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
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")
|
2022-07-26 00:17:01 +00:00
|
|
|
.action(ArgAction::Set)
|
2022-08-03 19:51:38 +00:00
|
|
|
.num_args(0..=1)
|
2022-05-27 12:04:21 +00:00
|
|
|
.default_value("false")
|
2021-07-31 08:55:54 +00:00
|
|
|
.default_missing_value("true"),
|
|
|
|
);
|
|
|
|
|
2022-11-24 13:54:25 +00:00
|
|
|
let m = cmd.clone().try_get_matches_from(["test"]).unwrap();
|
2022-06-10 01:03:28 +00:00
|
|
|
assert!(m.contains_id("flag"));
|
2022-05-27 12:04:21 +00:00
|
|
|
assert_eq!(
|
|
|
|
m.get_one::<String>("flag").map(|v| v.as_str()),
|
|
|
|
Some("false")
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
m.value_source("flag").unwrap(),
|
2022-08-01 14:02:52 +00:00
|
|
|
clap::parser::ValueSource::DefaultValue
|
2022-05-27 12:04:21 +00:00
|
|
|
);
|
2021-07-31 08:55:54 +00:00
|
|
|
|
2022-05-27 12:04:21 +00:00
|
|
|
let m = cmd
|
|
|
|
.clone()
|
2022-11-24 13:54:25 +00:00
|
|
|
.try_get_matches_from(["test", "--flag"])
|
2022-05-27 12:04:21 +00:00
|
|
|
.unwrap();
|
2022-06-10 01:03:28 +00:00
|
|
|
assert!(m.contains_id("flag"));
|
2021-07-31 08:55:54 +00:00
|
|
|
assert_eq!(
|
2022-05-27 12:04:21 +00:00
|
|
|
m.get_one::<String>("flag").map(|v| v.as_str()),
|
|
|
|
Some("true")
|
2021-12-27 18:56:12 +00:00
|
|
|
);
|
|
|
|
assert_eq!(
|
2022-05-27 12:04:21 +00:00
|
|
|
m.value_source("flag").unwrap(),
|
2022-08-01 14:02:52 +00:00
|
|
|
clap::parser::ValueSource::CommandLine
|
2021-07-31 08:55:54 +00:00
|
|
|
);
|
2022-05-27 12:04:21 +00:00
|
|
|
|
|
|
|
let m = cmd
|
|
|
|
.clone()
|
2022-11-24 13:54:25 +00:00
|
|
|
.try_get_matches_from(["test", "--flag=true"])
|
2022-05-27 12:04:21 +00:00
|
|
|
.unwrap();
|
2022-06-10 01:03:28 +00:00
|
|
|
assert!(m.contains_id("flag"));
|
2022-05-27 12:04:21 +00:00
|
|
|
assert_eq!(
|
|
|
|
m.get_one::<String>("flag").map(|v| v.as_str()),
|
|
|
|
Some("true")
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
m.value_source("flag").unwrap(),
|
2022-08-01 14:02:52 +00:00
|
|
|
clap::parser::ValueSource::CommandLine
|
2022-05-27 12:04:21 +00:00
|
|
|
);
|
|
|
|
|
2022-11-24 13:54:25 +00:00
|
|
|
let m = cmd.try_get_matches_from(["test", "--flag=false"]).unwrap();
|
2022-06-10 01:03:28 +00:00
|
|
|
assert!(m.contains_id("flag"));
|
2021-07-31 08:55:54 +00:00
|
|
|
assert_eq!(
|
2022-05-27 12:04:21 +00:00
|
|
|
m.get_one::<String>("flag").map(|v| v.as_str()),
|
|
|
|
Some("false")
|
2021-07-31 08:55:54 +00:00
|
|
|
);
|
|
|
|
assert_eq!(
|
2022-05-27 12:04:21 +00:00
|
|
|
m.value_source("flag").unwrap(),
|
2022-08-01 14:02:52 +00:00
|
|
|
clap::parser::ValueSource::CommandLine
|
2021-07-31 08:55:54 +00:00
|
|
|
);
|
|
|
|
}
|
2022-02-08 18:59:30 +00:00
|
|
|
|
2022-05-27 01:36:49 +00:00
|
|
|
#[test]
|
|
|
|
fn delimited_missing_value() {
|
|
|
|
let cmd = Command::new("test").arg(
|
|
|
|
Arg::new("flag")
|
|
|
|
.long("flag")
|
|
|
|
.default_value("one,two")
|
|
|
|
.default_missing_value("three,four")
|
2022-08-03 16:20:07 +00:00
|
|
|
.num_args(0..)
|
2022-05-27 01:36:49 +00:00
|
|
|
.value_delimiter(',')
|
|
|
|
.require_equals(true),
|
|
|
|
);
|
|
|
|
|
|
|
|
let m = cmd.clone().try_get_matches_from(["test"]).unwrap();
|
|
|
|
assert_eq!(
|
|
|
|
m.get_many::<String>("flag")
|
|
|
|
.unwrap()
|
|
|
|
.map(|s| s.as_str())
|
|
|
|
.collect::<Vec<_>>(),
|
|
|
|
vec!["one", "two"]
|
|
|
|
);
|
|
|
|
|
|
|
|
let m = cmd.try_get_matches_from(["test", "--flag"]).unwrap();
|
|
|
|
assert_eq!(
|
|
|
|
m.get_many::<String>("flag")
|
|
|
|
.unwrap()
|
|
|
|
.map(|s| s.as_str())
|
|
|
|
.collect::<Vec<_>>(),
|
|
|
|
vec!["three", "four"]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-02-08 18:59:30 +00:00
|
|
|
#[cfg(debug_assertions)]
|
|
|
|
#[test]
|
2022-09-19 14:59:04 +00:00
|
|
|
#[cfg(feature = "error-context")]
|
2022-10-13 15:45:38 +00:00
|
|
|
#[should_panic = "Argument `arg`'s default_missing_value=\"value\" failed validation: error: 'value' isn't a valid value for '[arg]'"]
|
2022-02-08 18:59:30 +00:00
|
|
|
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")
|
2022-05-24 01:16:02 +00:00
|
|
|
.value_parser(["one", "two"])
|
2022-02-08 18:59:30 +00:00
|
|
|
.default_missing_value("value"),
|
|
|
|
)
|
|
|
|
.try_get_matches();
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(debug_assertions)]
|
|
|
|
#[test]
|
2022-09-19 14:59:04 +00:00
|
|
|
#[cfg(feature = "error-context")]
|
2022-10-13 15:45:38 +00:00
|
|
|
#[should_panic = "Argument `arg`'s default_missing_value=\"value\" failed validation: error: Invalid value 'value' for '[arg]"]
|
2022-02-08 18:59:30 +00:00
|
|
|
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")
|
2022-05-23 20:42:34 +00:00
|
|
|
.value_parser(clap::value_parser!(u32))
|
2022-02-08 18:59:30 +00:00
|
|
|
.default_missing_value("value"),
|
|
|
|
)
|
|
|
|
.try_get_matches();
|
|
|
|
}
|
2022-06-04 13:27:39 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn valid_index() {
|
|
|
|
let m = Command::new("df")
|
|
|
|
.arg(
|
|
|
|
Arg::new("color")
|
|
|
|
.long("color")
|
|
|
|
.default_value("auto")
|
2022-08-03 16:20:07 +00:00
|
|
|
.num_args(0..=1)
|
2022-06-04 13:27:39 +00:00
|
|
|
.require_equals(true)
|
|
|
|
.default_missing_value("always"),
|
|
|
|
)
|
2022-07-26 00:17:01 +00:00
|
|
|
.arg(Arg::new("sync").long("sync").action(ArgAction::SetTrue))
|
2022-06-04 13:27:39 +00:00
|
|
|
.try_get_matches_from(vec!["df", "--color", "--sync"])
|
|
|
|
.unwrap();
|
2022-06-10 01:03:28 +00:00
|
|
|
assert!(m.contains_id("color"));
|
2022-06-04 13:27:39 +00:00
|
|
|
assert_eq!(
|
|
|
|
m.get_one::<String>("color").map(|v| v.as_str()).unwrap(),
|
|
|
|
"always"
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
m.value_source("color").unwrap(),
|
2022-08-01 14:02:52 +00:00
|
|
|
clap::parser::ValueSource::CommandLine
|
2022-06-04 13:27:39 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
// Make sure the index reflects `--color`s position and not something else
|
|
|
|
assert_eq!(m.index_of("color"), Some(2));
|
|
|
|
}
|