2022-06-13 12:48:06 +00:00
|
|
|
use std::ffi::OsStr;
|
|
|
|
use std::ffi::OsString;
|
|
|
|
|
2022-06-07 21:21:12 +00:00
|
|
|
use super::utils;
|
2022-08-16 16:52:10 +00:00
|
|
|
use clap::builder::ArgPredicate;
|
2022-07-26 00:17:01 +00:00
|
|
|
use clap::{arg, error::ErrorKind, value_parser, Arg, ArgAction, Command};
|
2016-12-09 23:29:53 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn opts() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let r = Command::new("df")
|
2022-09-12 21:59:57 +00:00
|
|
|
.arg(arg!(o: -o <opt> "some opt").default_value("default"))
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec![""]);
|
2021-12-27 19:57:38 +00:00
|
|
|
assert!(r.is_ok(), "{}", r.unwrap_err());
|
2016-12-09 23:29:53 +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"
|
|
|
|
);
|
2016-12-09 23:29:53 +00:00
|
|
|
}
|
|
|
|
|
2022-06-04 13:27:39 +00:00
|
|
|
#[test]
|
|
|
|
fn default_has_index() {
|
|
|
|
let r = Command::new("df")
|
2022-09-12 21:59:57 +00:00
|
|
|
.arg(arg!(o: -o <opt> "some opt").default_value("default"))
|
2022-06-04 13:27:39 +00:00
|
|
|
.try_get_matches_from(vec![""]);
|
|
|
|
assert!(r.is_ok(), "{}", r.unwrap_err());
|
|
|
|
let m = r.unwrap();
|
|
|
|
assert_eq!(m.index_of("o"), Some(1));
|
|
|
|
}
|
|
|
|
|
2020-10-13 21:21:01 +00:00
|
|
|
#[test]
|
|
|
|
fn opt_without_value_fail() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let r = Command::new("df")
|
2021-03-10 17:39:50 +00:00
|
|
|
.arg(
|
2021-11-19 20:33:11 +00:00
|
|
|
arg!(o: -o <opt> "some opt")
|
2021-03-10 17:39:50 +00:00
|
|
|
.default_value("default")
|
2022-05-23 21:58:07 +00:00
|
|
|
.value_parser(clap::builder::NonEmptyStringValueParser::new()),
|
2021-03-10 17:39:50 +00:00
|
|
|
)
|
2020-10-13 21:21:01 +00:00
|
|
|
.try_get_matches_from(vec!["", "-o"]);
|
|
|
|
assert!(r.is_err());
|
|
|
|
let err = r.unwrap_err();
|
2022-07-22 14:03:28 +00:00
|
|
|
assert_eq!(err.kind(), ErrorKind::InvalidValue);
|
2020-10-13 21:21:01 +00:00
|
|
|
assert!(err
|
|
|
|
.to_string()
|
|
|
|
.contains("The argument '-o <opt>' requires a value but none was supplied"));
|
|
|
|
}
|
|
|
|
|
2016-12-09 23:29:53 +00:00
|
|
|
#[test]
|
|
|
|
fn opt_user_override() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let r = Command::new("df")
|
2022-09-12 21:59:57 +00:00
|
|
|
.arg(arg!(--opt <FILE> "some arg").default_value("default"))
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec!["", "--opt", "value"]);
|
2021-12-27 19:57:38 +00:00
|
|
|
assert!(r.is_ok(), "{}", r.unwrap_err());
|
2016-12-09 23:29:53 +00:00
|
|
|
let m = r.unwrap();
|
2022-06-10 01:03:28 +00:00
|
|
|
assert!(m.contains_id("opt"));
|
2022-05-24 15:16:50 +00:00
|
|
|
assert_eq!(
|
|
|
|
m.get_one::<String>("opt").map(|v| v.as_str()).unwrap(),
|
|
|
|
"value"
|
|
|
|
);
|
2016-12-09 23:29:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn positionals() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let r = Command::new("df")
|
2021-11-19 20:33:11 +00:00
|
|
|
.arg(arg!([arg] "some opt").default_value("default"))
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec![""]);
|
2021-12-27 19:57:38 +00:00
|
|
|
assert!(r.is_ok(), "{}", r.unwrap_err());
|
2016-12-09 23:29:53 +00:00
|
|
|
let m = r.unwrap();
|
2022-06-10 01:03:28 +00:00
|
|
|
assert!(m.contains_id("arg"));
|
2022-05-24 15:16:50 +00:00
|
|
|
assert_eq!(
|
|
|
|
m.get_one::<String>("arg").map(|v| v.as_str()).unwrap(),
|
|
|
|
"default"
|
|
|
|
);
|
2016-12-09 23:29:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn positional_user_override() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let r = Command::new("df")
|
2021-11-19 20:33:11 +00:00
|
|
|
.arg(arg!([arg] "some arg").default_value("default"))
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec!["", "value"]);
|
2021-12-27 19:57:38 +00:00
|
|
|
assert!(r.is_ok(), "{}", r.unwrap_err());
|
2016-12-09 23:29:53 +00:00
|
|
|
let m = r.unwrap();
|
2022-06-10 01:03:28 +00:00
|
|
|
assert!(m.contains_id("arg"));
|
2022-05-24 15:16:50 +00:00
|
|
|
assert_eq!(
|
|
|
|
m.get_one::<String>("arg").map(|v| v.as_str()).unwrap(),
|
|
|
|
"value"
|
|
|
|
);
|
2016-12-09 23:29:53 +00:00
|
|
|
}
|
|
|
|
|
2017-04-24 19:49:59 +00:00
|
|
|
// OsStr Default Values
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn osstr_opts() {
|
|
|
|
use std::ffi::OsStr;
|
|
|
|
let expected = OsStr::new("default");
|
2017-09-29 18:56:53 +00:00
|
|
|
|
2022-02-12 03:48:29 +00:00
|
|
|
let r = Command::new("df")
|
2022-09-12 21:59:57 +00:00
|
|
|
.arg(arg!(o: -o <opt> "some opt").default_value(expected))
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec![""]);
|
2021-12-27 19:57:38 +00:00
|
|
|
assert!(r.is_ok(), "{}", r.unwrap_err());
|
2017-04-24 19:49:59 +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(),
|
|
|
|
expected
|
|
|
|
);
|
2017-04-24 19:49:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn osstr_opt_user_override() {
|
|
|
|
use std::ffi::OsStr;
|
|
|
|
let default = OsStr::new("default");
|
|
|
|
|
2022-02-12 03:48:29 +00:00
|
|
|
let r = Command::new("df")
|
2022-09-12 21:59:57 +00:00
|
|
|
.arg(arg!(--opt <FILE> "some arg").default_value(default))
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec!["", "--opt", "value"]);
|
2021-12-27 19:57:38 +00:00
|
|
|
assert!(r.is_ok(), "{}", r.unwrap_err());
|
2017-04-24 19:49:59 +00:00
|
|
|
let m = r.unwrap();
|
2022-06-10 01:03:28 +00:00
|
|
|
assert!(m.contains_id("opt"));
|
2022-05-24 15:16:50 +00:00
|
|
|
assert_eq!(
|
|
|
|
m.get_one::<String>("opt").map(|v| v.as_str()).unwrap(),
|
|
|
|
"value"
|
|
|
|
);
|
2017-04-24 19:49:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn osstr_positionals() {
|
|
|
|
use std::ffi::OsStr;
|
|
|
|
let expected = OsStr::new("default");
|
|
|
|
|
2022-02-12 03:48:29 +00:00
|
|
|
let r = Command::new("df")
|
2022-08-29 17:42:30 +00:00
|
|
|
.arg(arg!([arg] "some opt").default_value(expected))
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec![""]);
|
2021-12-27 19:57:38 +00:00
|
|
|
assert!(r.is_ok(), "{}", r.unwrap_err());
|
2017-04-24 19:49:59 +00:00
|
|
|
let m = r.unwrap();
|
2022-06-10 01:03:28 +00:00
|
|
|
assert!(m.contains_id("arg"));
|
2022-05-24 15:16:50 +00:00
|
|
|
assert_eq!(
|
|
|
|
m.get_one::<String>("arg").map(|v| v.as_str()).unwrap(),
|
|
|
|
expected
|
|
|
|
);
|
2017-04-24 19:49:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn osstr_positional_user_override() {
|
|
|
|
use std::ffi::OsStr;
|
|
|
|
let default = OsStr::new("default");
|
|
|
|
|
2022-02-12 03:48:29 +00:00
|
|
|
let r = Command::new("df")
|
2022-08-29 17:42:30 +00:00
|
|
|
.arg(arg!([arg] "some arg").default_value(default))
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec!["", "value"]);
|
2021-12-27 19:57:38 +00:00
|
|
|
assert!(r.is_ok(), "{}", r.unwrap_err());
|
2017-04-24 19:49:59 +00:00
|
|
|
let m = r.unwrap();
|
2022-06-10 01:03:28 +00:00
|
|
|
assert!(m.contains_id("arg"));
|
2022-05-24 15:16:50 +00:00
|
|
|
assert_eq!(
|
|
|
|
m.get_one::<String>("arg").map(|v| v.as_str()).unwrap(),
|
|
|
|
"value"
|
|
|
|
);
|
2017-04-24 19:49:59 +00:00
|
|
|
}
|
|
|
|
|
2017-09-29 18:56:53 +00:00
|
|
|
// --- Default if arg is present
|
2016-12-09 23:29:53 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn default_if_arg_present_no_default() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let r = Command::new("df")
|
2021-11-19 20:33:11 +00:00
|
|
|
.arg(arg!(--opt <FILE> "some arg").required(true))
|
2022-08-16 16:52:10 +00:00
|
|
|
.arg(arg!([arg] "some arg").default_value_if(
|
|
|
|
"opt",
|
|
|
|
ArgPredicate::IsPresent,
|
|
|
|
Some("default"),
|
|
|
|
))
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec!["", "--opt", "some"]);
|
2021-12-27 19:57:38 +00:00
|
|
|
assert!(r.is_ok(), "{}", r.unwrap_err());
|
2016-12-09 23:29:53 +00:00
|
|
|
let m = r.unwrap();
|
2022-06-10 01:03:28 +00:00
|
|
|
assert!(m.contains_id("arg"));
|
2022-05-24 15:16:50 +00:00
|
|
|
assert_eq!(
|
|
|
|
m.get_one::<String>("arg").map(|v| v.as_str()).unwrap(),
|
|
|
|
"default"
|
|
|
|
);
|
2016-12-09 23:29:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn default_if_arg_present_no_default_user_override() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let r = Command::new("df")
|
2022-09-12 21:59:57 +00:00
|
|
|
.arg(arg!(--opt <FILE> "some arg"))
|
2022-08-16 16:52:10 +00:00
|
|
|
.arg(arg!([arg] "some arg").default_value_if(
|
|
|
|
"opt",
|
|
|
|
ArgPredicate::IsPresent,
|
|
|
|
Some("default"),
|
|
|
|
))
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec!["", "--opt", "some", "other"]);
|
2021-12-27 19:57:38 +00:00
|
|
|
assert!(r.is_ok(), "{}", r.unwrap_err());
|
2016-12-09 23:29:53 +00:00
|
|
|
let m = r.unwrap();
|
2022-06-10 01:03:28 +00:00
|
|
|
assert!(m.contains_id("arg"));
|
2022-05-24 15:16:50 +00:00
|
|
|
assert_eq!(
|
|
|
|
m.get_one::<String>("arg").map(|v| v.as_str()).unwrap(),
|
|
|
|
"other"
|
|
|
|
);
|
2016-12-09 23:29:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn default_if_arg_present_no_arg_with_default() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let r = Command::new("df")
|
2022-09-12 21:59:57 +00:00
|
|
|
.arg(arg!(--opt <FILE> "some arg"))
|
2017-09-29 18:56:53 +00:00
|
|
|
.arg(
|
2021-11-19 20:33:11 +00:00
|
|
|
arg!([arg] "some arg")
|
2017-09-29 18:56:53 +00:00
|
|
|
.default_value("first")
|
2022-08-16 16:52:10 +00:00
|
|
|
.default_value_if("opt", ArgPredicate::IsPresent, Some("default")),
|
2017-09-29 18:56:53 +00:00
|
|
|
)
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec![""]);
|
2021-12-27 19:57:38 +00:00
|
|
|
assert!(r.is_ok(), "{}", r.unwrap_err());
|
2016-12-09 23:29:53 +00:00
|
|
|
let m = r.unwrap();
|
2022-06-10 01:03:28 +00:00
|
|
|
assert!(m.contains_id("arg"));
|
2022-05-24 15:16:50 +00:00
|
|
|
assert_eq!(
|
|
|
|
m.get_one::<String>("arg").map(|v| v.as_str()).unwrap(),
|
|
|
|
"first"
|
|
|
|
);
|
2016-12-09 23:29:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn default_if_arg_present_with_default() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let r = Command::new("df")
|
2022-09-12 21:59:57 +00:00
|
|
|
.arg(arg!(--opt <FILE> "some arg"))
|
2017-09-29 18:56:53 +00:00
|
|
|
.arg(
|
2021-11-19 20:33:11 +00:00
|
|
|
arg!([arg] "some arg")
|
2017-09-29 18:56:53 +00:00
|
|
|
.default_value("first")
|
2022-08-16 16:52:10 +00:00
|
|
|
.default_value_if("opt", ArgPredicate::IsPresent, Some("default")),
|
2017-09-29 18:56:53 +00:00
|
|
|
)
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec!["", "--opt", "some"]);
|
2021-12-27 19:57:38 +00:00
|
|
|
assert!(r.is_ok(), "{}", r.unwrap_err());
|
2016-12-09 23:29:53 +00:00
|
|
|
let m = r.unwrap();
|
2022-06-10 01:03:28 +00:00
|
|
|
assert!(m.contains_id("arg"));
|
2022-05-24 15:16:50 +00:00
|
|
|
assert_eq!(
|
|
|
|
m.get_one::<String>("arg").map(|v| v.as_str()).unwrap(),
|
|
|
|
"default"
|
|
|
|
);
|
2016-12-09 23:29:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn default_if_arg_present_with_default_user_override() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let r = Command::new("df")
|
2022-09-12 21:59:57 +00:00
|
|
|
.arg(arg!(--opt <FILE> "some arg"))
|
2017-09-29 18:56:53 +00:00
|
|
|
.arg(
|
2021-11-19 20:33:11 +00:00
|
|
|
arg!([arg] "some arg")
|
2017-09-29 18:56:53 +00:00
|
|
|
.default_value("first")
|
2022-08-16 16:52:10 +00:00
|
|
|
.default_value_if("opt", ArgPredicate::IsPresent, Some("default")),
|
2017-09-29 18:56:53 +00:00
|
|
|
)
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec!["", "--opt", "some", "other"]);
|
2021-12-27 19:57:38 +00:00
|
|
|
assert!(r.is_ok(), "{}", r.unwrap_err());
|
2016-12-09 23:29:53 +00:00
|
|
|
let m = r.unwrap();
|
2022-06-10 01:03:28 +00:00
|
|
|
assert!(m.contains_id("arg"));
|
2022-05-24 15:16:50 +00:00
|
|
|
assert_eq!(
|
|
|
|
m.get_one::<String>("arg").map(|v| v.as_str()).unwrap(),
|
|
|
|
"other"
|
|
|
|
);
|
2016-12-09 23:29:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn default_if_arg_present_no_arg_with_default_user_override() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let r = Command::new("df")
|
2022-09-12 21:59:57 +00:00
|
|
|
.arg(arg!(--opt <FILE> "some arg"))
|
2017-09-29 18:56:53 +00:00
|
|
|
.arg(
|
2021-11-19 20:33:11 +00:00
|
|
|
arg!([arg] "some arg")
|
2017-09-29 18:56:53 +00:00
|
|
|
.default_value("first")
|
2022-08-16 16:52:10 +00:00
|
|
|
.default_value_if("opt", ArgPredicate::IsPresent, Some("default")),
|
2017-09-29 18:56:53 +00:00
|
|
|
)
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec!["", "other"]);
|
2021-12-27 19:57:38 +00:00
|
|
|
assert!(r.is_ok(), "{}", r.unwrap_err());
|
2016-12-09 23:29:53 +00:00
|
|
|
let m = r.unwrap();
|
2022-06-10 01:03:28 +00:00
|
|
|
assert!(m.contains_id("arg"));
|
2022-05-24 15:16:50 +00:00
|
|
|
assert_eq!(
|
|
|
|
m.get_one::<String>("arg").map(|v| v.as_str()).unwrap(),
|
|
|
|
"other"
|
|
|
|
);
|
2016-12-09 23:29:53 +00:00
|
|
|
}
|
|
|
|
|
2016-12-29 01:35:34 +00:00
|
|
|
// Conditional Default Values
|
2016-12-09 23:29:53 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn default_if_arg_present_with_value_no_default() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let r = Command::new("df")
|
2022-09-12 21:59:57 +00:00
|
|
|
.arg(arg!(--opt <FILE> "some arg"))
|
2022-08-16 16:52:10 +00:00
|
|
|
.arg(arg!([arg] "some arg").default_value_if("opt", "value", Some("default")))
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec!["", "--opt", "value"]);
|
2021-12-27 19:57:38 +00:00
|
|
|
assert!(r.is_ok(), "{}", r.unwrap_err());
|
2016-12-09 23:29:53 +00:00
|
|
|
let m = r.unwrap();
|
2022-06-10 01:03:28 +00:00
|
|
|
assert!(m.contains_id("arg"));
|
2022-05-24 15:16:50 +00:00
|
|
|
assert_eq!(
|
|
|
|
m.get_one::<String>("arg").map(|v| v.as_str()).unwrap(),
|
|
|
|
"default"
|
|
|
|
);
|
2016-12-09 23:29:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn default_if_arg_present_with_value_no_default_fail() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let r = Command::new("df")
|
2022-09-12 21:59:57 +00:00
|
|
|
.arg(arg!(--opt <FILE> "some arg"))
|
2022-08-16 16:52:10 +00:00
|
|
|
.arg(arg!([arg] "some arg").default_value_if("opt", "value", Some("default")))
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec!["", "--opt", "other"]);
|
2021-12-27 19:57:38 +00:00
|
|
|
assert!(r.is_ok(), "{}", r.unwrap_err());
|
2016-12-09 23:29:53 +00:00
|
|
|
let m = r.unwrap();
|
2022-06-10 01:03:28 +00:00
|
|
|
assert!(!m.contains_id("arg"));
|
2022-05-24 15:16:50 +00:00
|
|
|
assert!(m.get_one::<String>("arg").map(|v| v.as_str()).is_none());
|
2016-12-09 23:29:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn default_if_arg_present_with_value_no_default_user_override() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let r = Command::new("df")
|
2022-09-12 21:59:57 +00:00
|
|
|
.arg(arg!(--opt <FILE> "some arg"))
|
2022-08-16 16:52:10 +00:00
|
|
|
.arg(arg!([arg] "some arg").default_value_if("opt", "some", Some("default")))
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec!["", "--opt", "some", "other"]);
|
2021-12-27 19:57:38 +00:00
|
|
|
assert!(r.is_ok(), "{}", r.unwrap_err());
|
2016-12-09 23:29:53 +00:00
|
|
|
let m = r.unwrap();
|
2022-06-10 01:03:28 +00:00
|
|
|
assert!(m.contains_id("arg"));
|
2022-05-24 15:16:50 +00:00
|
|
|
assert_eq!(
|
|
|
|
m.get_one::<String>("arg").map(|v| v.as_str()).unwrap(),
|
|
|
|
"other"
|
|
|
|
);
|
2016-12-09 23:29:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn default_if_arg_present_with_value_no_arg_with_default() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let r = Command::new("df")
|
2022-09-12 21:59:57 +00:00
|
|
|
.arg(arg!(--opt <FILE> "some arg"))
|
2017-09-29 18:56:53 +00:00
|
|
|
.arg(
|
2021-11-19 20:33:11 +00:00
|
|
|
arg!([arg] "some arg")
|
2017-09-29 18:56:53 +00:00
|
|
|
.default_value("first")
|
2022-08-16 16:52:10 +00:00
|
|
|
.default_value_if("opt", "some", Some("default")),
|
2017-09-29 18:56:53 +00:00
|
|
|
)
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec![""]);
|
2021-12-27 19:57:38 +00:00
|
|
|
assert!(r.is_ok(), "{}", r.unwrap_err());
|
2016-12-09 23:29:53 +00:00
|
|
|
let m = r.unwrap();
|
2022-06-10 01:03:28 +00:00
|
|
|
assert!(m.contains_id("arg"));
|
2022-05-24 15:16:50 +00:00
|
|
|
assert_eq!(
|
|
|
|
m.get_one::<String>("arg").map(|v| v.as_str()).unwrap(),
|
|
|
|
"first"
|
|
|
|
);
|
2016-12-09 23:29:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn default_if_arg_present_with_value_no_arg_with_default_fail() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let r = Command::new("df")
|
2022-09-12 21:59:57 +00:00
|
|
|
.arg(arg!(--opt <FILE> "some arg"))
|
2017-09-29 18:56:53 +00:00
|
|
|
.arg(
|
2021-11-19 20:33:11 +00:00
|
|
|
arg!([arg] "some arg")
|
2017-09-29 18:56:53 +00:00
|
|
|
.default_value("first")
|
2022-08-16 16:52:10 +00:00
|
|
|
.default_value_if("opt", "some", Some("default")),
|
2017-09-29 18:56:53 +00:00
|
|
|
)
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec!["", "--opt", "other"]);
|
2021-12-27 19:57:38 +00:00
|
|
|
assert!(r.is_ok(), "{}", r.unwrap_err());
|
2016-12-09 23:29:53 +00:00
|
|
|
let m = r.unwrap();
|
2022-06-10 01:03:28 +00:00
|
|
|
assert!(m.contains_id("arg"));
|
2022-05-24 15:16:50 +00:00
|
|
|
assert_eq!(
|
|
|
|
m.get_one::<String>("arg").map(|v| v.as_str()).unwrap(),
|
|
|
|
"first"
|
|
|
|
);
|
2016-12-09 23:29:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn default_if_arg_present_with_value_with_default() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let r = Command::new("df")
|
2022-09-12 21:59:57 +00:00
|
|
|
.arg(arg!(--opt <FILE> "some arg"))
|
2017-09-29 18:56:53 +00:00
|
|
|
.arg(
|
2021-11-19 20:33:11 +00:00
|
|
|
arg!([arg] "some arg")
|
2017-09-29 18:56:53 +00:00
|
|
|
.default_value("first")
|
2022-08-16 16:52:10 +00:00
|
|
|
.default_value_if("opt", "some", Some("default")),
|
2017-09-29 18:56:53 +00:00
|
|
|
)
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec!["", "--opt", "some"]);
|
2021-12-27 19:57:38 +00:00
|
|
|
assert!(r.is_ok(), "{}", r.unwrap_err());
|
2016-12-09 23:29:53 +00:00
|
|
|
let m = r.unwrap();
|
2022-06-10 01:03:28 +00:00
|
|
|
assert!(m.contains_id("arg"));
|
2022-05-24 15:16:50 +00:00
|
|
|
assert_eq!(
|
|
|
|
m.get_one::<String>("arg").map(|v| v.as_str()).unwrap(),
|
|
|
|
"default"
|
|
|
|
);
|
2016-12-09 23:29:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn default_if_arg_present_with_value_with_default_user_override() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let r = Command::new("df")
|
2022-09-12 21:59:57 +00:00
|
|
|
.arg(arg!(--opt <FILE> "some arg"))
|
2017-09-29 18:56:53 +00:00
|
|
|
.arg(
|
2021-11-19 20:33:11 +00:00
|
|
|
arg!([arg] "some arg")
|
2017-09-29 18:56:53 +00:00
|
|
|
.default_value("first")
|
2022-08-16 16:52:10 +00:00
|
|
|
.default_value_if("opt", "some", Some("default")),
|
2017-09-29 18:56:53 +00:00
|
|
|
)
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec!["", "--opt", "some", "other"]);
|
2021-12-27 19:57:38 +00:00
|
|
|
assert!(r.is_ok(), "{}", r.unwrap_err());
|
2016-12-09 23:29:53 +00:00
|
|
|
let m = r.unwrap();
|
2022-06-10 01:03:28 +00:00
|
|
|
assert!(m.contains_id("arg"));
|
2022-05-24 15:16:50 +00:00
|
|
|
assert_eq!(
|
|
|
|
m.get_one::<String>("arg").map(|v| v.as_str()).unwrap(),
|
|
|
|
"other"
|
|
|
|
);
|
2016-12-09 23:29:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn default_if_arg_present_no_arg_with_value_with_default_user_override() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let r = Command::new("df")
|
2022-09-12 21:59:57 +00:00
|
|
|
.arg(arg!(--opt <FILE> "some arg"))
|
2017-09-29 18:56:53 +00:00
|
|
|
.arg(
|
2021-11-19 20:33:11 +00:00
|
|
|
arg!([arg] "some arg")
|
2017-09-29 18:56:53 +00:00
|
|
|
.default_value("first")
|
2022-08-16 16:52:10 +00:00
|
|
|
.default_value_if("opt", "some", Some("default")),
|
2017-09-29 18:56:53 +00:00
|
|
|
)
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec!["", "other"]);
|
2021-12-27 19:57:38 +00:00
|
|
|
assert!(r.is_ok(), "{}", r.unwrap_err());
|
2016-12-09 23:29:53 +00:00
|
|
|
let m = r.unwrap();
|
2022-06-10 01:03:28 +00:00
|
|
|
assert!(m.contains_id("arg"));
|
2022-05-24 15:16:50 +00:00
|
|
|
assert_eq!(
|
|
|
|
m.get_one::<String>("arg").map(|v| v.as_str()).unwrap(),
|
|
|
|
"other"
|
|
|
|
);
|
2016-12-09 23:29:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn default_if_arg_present_no_arg_with_value_with_default_user_override_fail() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let r = Command::new("df")
|
2022-09-12 21:59:57 +00:00
|
|
|
.arg(arg!(--opt <FILE> "some arg"))
|
2017-09-29 18:56:53 +00:00
|
|
|
.arg(
|
2021-11-19 20:33:11 +00:00
|
|
|
arg!([arg] "some arg")
|
2017-09-29 18:56:53 +00:00
|
|
|
.default_value("first")
|
2022-08-16 16:52:10 +00:00
|
|
|
.default_value_if("opt", "some", Some("default")),
|
2017-09-29 18:56:53 +00:00
|
|
|
)
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec!["", "--opt", "value", "other"]);
|
2021-12-27 19:57:38 +00:00
|
|
|
assert!(r.is_ok(), "{}", r.unwrap_err());
|
2016-12-09 23:29:53 +00:00
|
|
|
let m = r.unwrap();
|
2022-06-10 01:03:28 +00:00
|
|
|
assert!(m.contains_id("arg"));
|
2022-05-24 15:16:50 +00:00
|
|
|
assert_eq!(
|
|
|
|
m.get_one::<String>("arg").map(|v| v.as_str()).unwrap(),
|
|
|
|
"other"
|
|
|
|
);
|
2016-12-09 23:29:53 +00:00
|
|
|
}
|
|
|
|
|
2021-01-17 07:28:04 +00:00
|
|
|
// Unsetting the default
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn no_default_if_arg_present_with_value_no_default() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let r = Command::new("df")
|
2022-09-12 21:59:57 +00:00
|
|
|
.arg(arg!(--opt <FILE> "some arg"))
|
2022-08-16 16:52:10 +00:00
|
|
|
.arg(arg!([arg] "some arg").default_value_if("opt", "value", None))
|
2021-01-17 07:28:04 +00:00
|
|
|
.try_get_matches_from(vec!["", "--opt", "value"]);
|
2021-12-27 19:57:38 +00:00
|
|
|
assert!(r.is_ok(), "{}", r.unwrap_err());
|
2021-01-17 07:28:04 +00:00
|
|
|
let m = r.unwrap();
|
2022-06-10 01:03:28 +00:00
|
|
|
assert!(!m.contains_id("arg"));
|
2021-01-17 07:28:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn no_default_if_arg_present_with_value_with_default() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let r = Command::new("df")
|
2022-09-12 21:59:57 +00:00
|
|
|
.arg(arg!(--opt <FILE> "some arg"))
|
2021-01-17 07:28:04 +00:00
|
|
|
.arg(
|
2021-11-19 20:33:11 +00:00
|
|
|
arg!([arg] "some arg")
|
2021-01-17 07:28:04 +00:00
|
|
|
.default_value("default")
|
2022-08-16 16:52:10 +00:00
|
|
|
.default_value_if("opt", "value", None),
|
2021-01-17 07:28:04 +00:00
|
|
|
)
|
|
|
|
.try_get_matches_from(vec!["", "--opt", "value"]);
|
2021-12-27 19:57:38 +00:00
|
|
|
assert!(r.is_ok(), "{}", r.unwrap_err());
|
2021-01-17 07:28:04 +00:00
|
|
|
let m = r.unwrap();
|
2022-06-10 01:03:28 +00:00
|
|
|
assert!(!m.contains_id("arg"));
|
2022-05-24 15:16:50 +00:00
|
|
|
assert!(m.get_one::<String>("arg").map(|v| v.as_str()).is_none());
|
2021-01-17 07:28:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn no_default_if_arg_present_with_value_with_default_user_override() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let r = Command::new("df")
|
2022-09-12 21:59:57 +00:00
|
|
|
.arg(arg!(--opt <FILE> "some arg"))
|
2021-01-17 07:28:04 +00:00
|
|
|
.arg(
|
2021-11-19 20:33:11 +00:00
|
|
|
arg!([arg] "some arg")
|
2021-01-17 07:28:04 +00:00
|
|
|
.default_value("default")
|
2022-08-16 16:52:10 +00:00
|
|
|
.default_value_if("opt", "value", None),
|
2021-01-17 07:28:04 +00:00
|
|
|
)
|
|
|
|
.try_get_matches_from(vec!["", "--opt", "value", "other"]);
|
2021-12-27 19:57:38 +00:00
|
|
|
assert!(r.is_ok(), "{}", r.unwrap_err());
|
2021-01-17 07:28:04 +00:00
|
|
|
let m = r.unwrap();
|
2022-06-10 01:03:28 +00:00
|
|
|
assert!(m.contains_id("arg"));
|
2022-05-24 15:16:50 +00:00
|
|
|
assert_eq!(
|
|
|
|
m.get_one::<String>("arg").map(|v| v.as_str()).unwrap(),
|
|
|
|
"other"
|
|
|
|
);
|
2021-01-17 07:28:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn no_default_if_arg_present_no_arg_with_value_with_default() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let r = Command::new("df")
|
2022-09-12 21:59:57 +00:00
|
|
|
.arg(arg!(--opt <FILE> "some arg"))
|
2021-01-17 07:28:04 +00:00
|
|
|
.arg(
|
2021-11-19 20:33:11 +00:00
|
|
|
arg!([arg] "some arg")
|
2021-01-17 07:28:04 +00:00
|
|
|
.default_value("default")
|
2022-08-16 16:52:10 +00:00
|
|
|
.default_value_if("opt", "value", None),
|
2021-01-17 07:28:04 +00:00
|
|
|
)
|
|
|
|
.try_get_matches_from(vec!["", "--opt", "other"]);
|
2021-12-27 19:57:38 +00:00
|
|
|
assert!(r.is_ok(), "{}", r.unwrap_err());
|
2021-01-17 07:28:04 +00:00
|
|
|
let m = r.unwrap();
|
2022-06-10 01:03:28 +00:00
|
|
|
assert!(m.contains_id("arg"));
|
2022-05-24 15:16:50 +00:00
|
|
|
assert_eq!(
|
|
|
|
m.get_one::<String>("arg").map(|v| v.as_str()).unwrap(),
|
|
|
|
"default"
|
|
|
|
);
|
2021-01-17 07:28:04 +00:00
|
|
|
}
|
|
|
|
|
2016-12-09 23:29:53 +00:00
|
|
|
// Multiple conditions
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn default_ifs_arg_present() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let r = Command::new("df")
|
2022-09-12 21:59:57 +00:00
|
|
|
.arg(arg!(--opt <FILE> "some arg"))
|
2021-11-19 20:33:11 +00:00
|
|
|
.arg(arg!(--flag "some arg"))
|
2017-09-29 18:56:53 +00:00
|
|
|
.arg(
|
2021-11-19 20:33:11 +00:00
|
|
|
arg!([arg] "some arg")
|
2017-09-29 18:56:53 +00:00
|
|
|
.default_value("first")
|
2022-08-12 17:40:58 +00:00
|
|
|
.default_value_ifs([
|
2022-08-16 16:52:10 +00:00
|
|
|
("opt", ArgPredicate::from("some"), Some("default")),
|
|
|
|
("flag", ArgPredicate::IsPresent, Some("flg")),
|
2021-03-09 18:26:38 +00:00
|
|
|
]),
|
2017-09-29 18:56:53 +00:00
|
|
|
)
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec!["", "--flag"]);
|
2021-12-27 19:57:38 +00:00
|
|
|
assert!(r.is_ok(), "{}", r.unwrap_err());
|
2016-12-09 23:29:53 +00:00
|
|
|
let m = r.unwrap();
|
2022-06-10 01:03:28 +00:00
|
|
|
assert!(m.contains_id("arg"));
|
2022-05-24 15:16:50 +00:00
|
|
|
assert_eq!(
|
|
|
|
m.get_one::<String>("arg").map(|v| v.as_str()).unwrap(),
|
|
|
|
"flg"
|
|
|
|
);
|
2016-12-09 23:29:53 +00:00
|
|
|
}
|
|
|
|
|
2021-01-17 07:28:04 +00:00
|
|
|
#[test]
|
|
|
|
fn no_default_ifs_arg_present() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let r = Command::new("df")
|
2022-09-12 21:59:57 +00:00
|
|
|
.arg(arg!(--opt <FILE> "some arg"))
|
2021-11-19 20:33:11 +00:00
|
|
|
.arg(arg!(--flag "some arg"))
|
2021-01-17 07:28:04 +00:00
|
|
|
.arg(
|
2021-11-19 20:33:11 +00:00
|
|
|
arg!([arg] "some arg")
|
2021-01-17 07:28:04 +00:00
|
|
|
.default_value("first")
|
2022-08-16 16:52:10 +00:00
|
|
|
.default_value_ifs([
|
|
|
|
("opt", ArgPredicate::from("some"), Some("default")),
|
|
|
|
("flag", ArgPredicate::IsPresent, None),
|
|
|
|
]),
|
2021-01-17 07:28:04 +00:00
|
|
|
)
|
|
|
|
.try_get_matches_from(vec!["", "--flag"]);
|
2021-12-27 19:57:38 +00:00
|
|
|
assert!(r.is_ok(), "{}", r.unwrap_err());
|
2021-01-17 07:28:04 +00:00
|
|
|
let m = r.unwrap();
|
2022-06-10 01:03:28 +00:00
|
|
|
assert!(!m.contains_id("arg"));
|
2022-05-24 15:16:50 +00:00
|
|
|
assert!(m.get_one::<String>("arg").map(|v| v.as_str()).is_none());
|
2021-01-17 07:28:04 +00:00
|
|
|
}
|
|
|
|
|
2016-12-09 23:29:53 +00:00
|
|
|
#[test]
|
|
|
|
fn default_ifs_arg_present_user_override() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let r = Command::new("df")
|
2022-09-12 21:59:57 +00:00
|
|
|
.arg(arg!(--opt <FILE> "some arg"))
|
2021-11-19 20:33:11 +00:00
|
|
|
.arg(arg!(--flag "some arg"))
|
2017-09-29 18:56:53 +00:00
|
|
|
.arg(
|
2021-11-19 20:33:11 +00:00
|
|
|
arg!([arg] "some arg")
|
2017-09-29 18:56:53 +00:00
|
|
|
.default_value("first")
|
2022-08-12 17:40:58 +00:00
|
|
|
.default_value_ifs([
|
2022-08-16 16:52:10 +00:00
|
|
|
("opt", ArgPredicate::from("some"), Some("default")),
|
|
|
|
("flag", ArgPredicate::IsPresent, Some("flg")),
|
2021-03-09 18:26:38 +00:00
|
|
|
]),
|
2017-09-29 18:56:53 +00:00
|
|
|
)
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec!["", "--flag", "value"]);
|
2021-12-27 19:57:38 +00:00
|
|
|
assert!(r.is_ok(), "{}", r.unwrap_err());
|
2016-12-09 23:29:53 +00:00
|
|
|
let m = r.unwrap();
|
2022-06-10 01:03:28 +00:00
|
|
|
assert!(m.contains_id("arg"));
|
2022-05-24 15:16:50 +00:00
|
|
|
assert_eq!(
|
|
|
|
m.get_one::<String>("arg").map(|v| v.as_str()).unwrap(),
|
|
|
|
"value"
|
|
|
|
);
|
2016-12-09 23:29:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn default_ifs_arg_present_order() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let r = Command::new("df")
|
2022-09-12 21:59:57 +00:00
|
|
|
.arg(arg!(--opt <FILE> "some arg"))
|
2021-11-19 20:33:11 +00:00
|
|
|
.arg(arg!(--flag "some arg"))
|
2017-09-29 18:56:53 +00:00
|
|
|
.arg(
|
2021-11-19 20:33:11 +00:00
|
|
|
arg!([arg] "some arg")
|
2017-09-29 18:56:53 +00:00
|
|
|
.default_value("first")
|
2022-08-12 17:40:58 +00:00
|
|
|
.default_value_ifs([
|
2022-08-16 16:52:10 +00:00
|
|
|
("opt", ArgPredicate::from("some"), Some("default")),
|
|
|
|
("flag", ArgPredicate::IsPresent, Some("flg")),
|
2021-03-09 18:26:38 +00:00
|
|
|
]),
|
2017-09-29 18:56:53 +00:00
|
|
|
)
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec!["", "--opt=some", "--flag"]);
|
2021-12-27 19:57:38 +00:00
|
|
|
assert!(r.is_ok(), "{}", r.unwrap_err());
|
2016-12-09 23:29:53 +00:00
|
|
|
let m = r.unwrap();
|
2022-06-10 01:03:28 +00:00
|
|
|
assert!(m.contains_id("arg"));
|
2022-05-24 15:16:50 +00:00
|
|
|
assert_eq!(
|
|
|
|
m.get_one::<String>("arg").map(|v| v.as_str()).unwrap(),
|
|
|
|
"default"
|
|
|
|
);
|
2017-02-03 22:43:49 +00:00
|
|
|
}
|
|
|
|
|
2022-06-13 12:48:06 +00:00
|
|
|
#[test]
|
|
|
|
fn default_value_ifs_os() {
|
|
|
|
let cmd = Command::new("my_cargo")
|
|
|
|
.arg(
|
|
|
|
Arg::new("flag")
|
|
|
|
.long("flag")
|
|
|
|
.value_parser(value_parser!(OsString))
|
2022-07-26 00:17:01 +00:00
|
|
|
.action(ArgAction::Set),
|
2022-06-13 12:48:06 +00:00
|
|
|
)
|
|
|
|
.arg(
|
|
|
|
Arg::new("other")
|
|
|
|
.long("other")
|
|
|
|
.value_parser(value_parser!(OsString))
|
2022-08-29 17:42:30 +00:00
|
|
|
.default_value_ifs([("flag", "标记2", OsStr::new("flag=标记2"))]),
|
2022-06-13 12:48:06 +00:00
|
|
|
);
|
|
|
|
let result = cmd.try_get_matches_from(["my_cargo", "--flag", "标记2"]);
|
|
|
|
assert!(result.is_ok(), "{}", result.unwrap_err());
|
|
|
|
let m = result.unwrap();
|
|
|
|
assert_eq!(
|
|
|
|
m.get_one::<OsString>("flag").map(OsString::as_os_str),
|
|
|
|
Some(OsStr::new("标记2"))
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
m.get_one::<OsString>("other").map(OsString::as_os_str),
|
|
|
|
Some(OsStr::new("flag=标记2")),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-10-25 17:36:01 +00:00
|
|
|
// Interaction with requires
|
2017-02-03 22:43:49 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn conditional_reqs_pass() {
|
2022-02-14 21:47:20 +00:00
|
|
|
let m = Command::new("Test cmd")
|
2017-09-29 18:56:53 +00:00
|
|
|
.arg(
|
2020-05-14 20:50:56 +00:00
|
|
|
Arg::new("target")
|
2022-07-26 00:17:01 +00:00
|
|
|
.action(ArgAction::Set)
|
2017-09-29 18:56:53 +00:00
|
|
|
.default_value("file")
|
|
|
|
.long("target"),
|
|
|
|
)
|
|
|
|
.arg(
|
2020-05-14 20:50:56 +00:00
|
|
|
Arg::new("input")
|
2022-07-26 00:17:01 +00:00
|
|
|
.action(ArgAction::Set)
|
2017-09-29 18:56:53 +00:00
|
|
|
.required(true)
|
|
|
|
.long("input"),
|
|
|
|
)
|
|
|
|
.arg(
|
2020-05-14 20:50:56 +00:00
|
|
|
Arg::new("output")
|
2022-07-26 00:17:01 +00:00
|
|
|
.action(ArgAction::Set)
|
2020-05-23 14:02:03 +00:00
|
|
|
.required_if_eq("target", "file")
|
2017-09-29 18:56:53 +00:00
|
|
|
.long("output"),
|
|
|
|
)
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec!["test", "--input", "some", "--output", "other"]);
|
2017-02-03 22:43:49 +00:00
|
|
|
|
2021-12-27 19:57:38 +00:00
|
|
|
assert!(m.is_ok(), "{}", m.unwrap_err());
|
2017-02-03 22:43:49 +00:00
|
|
|
let m = m.unwrap();
|
2022-05-24 15:16:50 +00:00
|
|
|
assert_eq!(
|
|
|
|
m.get_one::<String>("output").map(|v| v.as_str()),
|
|
|
|
Some("other")
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
m.get_one::<String>("input").map(|v| v.as_str()),
|
|
|
|
Some("some")
|
|
|
|
);
|
2017-04-24 19:49:59 +00:00
|
|
|
}
|
2017-10-24 19:57:04 +00:00
|
|
|
|
2019-10-10 16:48:22 +00:00
|
|
|
#[test]
|
|
|
|
fn multiple_defaults() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let r = Command::new("diff")
|
2019-10-10 16:48:22 +00:00
|
|
|
.arg(
|
2020-05-14 20:50:56 +00:00
|
|
|
Arg::new("files")
|
2019-10-10 16:48:22 +00:00
|
|
|
.long("files")
|
2022-08-03 16:20:07 +00:00
|
|
|
.num_args(2)
|
2022-08-15 18:21:05 +00:00
|
|
|
.default_values(["old", "new"]),
|
2019-10-10 16:48:22 +00:00
|
|
|
)
|
|
|
|
.try_get_matches_from(vec![""]);
|
2021-12-27 19:57:38 +00:00
|
|
|
assert!(r.is_ok(), "{}", r.unwrap_err());
|
2019-10-10 16:48:22 +00:00
|
|
|
let m = r.unwrap();
|
2022-06-10 01:03:28 +00:00
|
|
|
assert!(m.contains_id("files"));
|
2022-05-23 12:23:16 +00:00
|
|
|
assert_eq!(
|
|
|
|
m.get_many::<String>("files").unwrap().collect::<Vec<_>>(),
|
|
|
|
vec!["old", "new"]
|
|
|
|
);
|
2019-10-10 16:48:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn multiple_defaults_override() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let r = Command::new("diff")
|
2019-10-10 16:48:22 +00:00
|
|
|
.arg(
|
2020-05-14 20:50:56 +00:00
|
|
|
Arg::new("files")
|
2019-10-10 16:48:22 +00:00
|
|
|
.long("files")
|
2022-08-03 16:20:07 +00:00
|
|
|
.num_args(2)
|
2022-08-15 18:21:05 +00:00
|
|
|
.default_values(["old", "new"]),
|
2019-10-10 16:48:22 +00:00
|
|
|
)
|
|
|
|
.try_get_matches_from(vec!["", "--files", "other", "mine"]);
|
2021-12-27 19:57:38 +00:00
|
|
|
assert!(r.is_ok(), "{}", r.unwrap_err());
|
2019-10-10 16:48:22 +00:00
|
|
|
let m = r.unwrap();
|
2022-06-10 01:03:28 +00:00
|
|
|
assert!(m.contains_id("files"));
|
2022-05-23 12:23:16 +00:00
|
|
|
assert_eq!(
|
|
|
|
m.get_many::<String>("files").unwrap().collect::<Vec<_>>(),
|
|
|
|
vec!["other", "mine"]
|
|
|
|
);
|
2019-10-10 16:48:22 +00:00
|
|
|
}
|
|
|
|
|
2021-07-20 18:50:41 +00:00
|
|
|
#[test]
|
|
|
|
fn default_vals_donnot_show_in_smart_usage() {
|
2022-02-14 21:47:20 +00:00
|
|
|
let cmd = Command::new("bug")
|
2021-07-20 18:50:41 +00:00
|
|
|
.arg(
|
|
|
|
Arg::new("foo")
|
|
|
|
.long("config")
|
2022-07-26 00:17:01 +00:00
|
|
|
.action(ArgAction::Set)
|
2021-07-20 18:50:41 +00:00
|
|
|
.default_value("bar"),
|
|
|
|
)
|
|
|
|
.arg(Arg::new("input").required(true));
|
2021-10-25 17:36:01 +00:00
|
|
|
|
2022-04-29 20:32:25 +00:00
|
|
|
utils::assert_output(
|
2022-02-14 21:47:20 +00:00
|
|
|
cmd,
|
2021-07-20 18:50:41 +00:00
|
|
|
"bug",
|
2022-09-07 20:29:15 +00:00
|
|
|
"\
|
|
|
|
error: The following required arguments were not provided:
|
|
|
|
<input>
|
2021-07-20 18:50:41 +00:00
|
|
|
|
2022-09-07 16:03:55 +00:00
|
|
|
Usage: bug <input>
|
2021-07-20 18:50:41 +00:00
|
|
|
|
2021-09-24 15:58:39 +00:00
|
|
|
For more information try --help
|
|
|
|
",
|
2021-07-20 18:50:41 +00:00
|
|
|
true,
|
2022-04-29 20:32:25 +00:00
|
|
|
);
|
2021-07-20 18:50:41 +00:00
|
|
|
}
|
|
|
|
|
2017-10-24 19:57:04 +00:00
|
|
|
#[test]
|
|
|
|
fn issue_1050_num_vals_and_defaults() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let res = Command::new("hello")
|
2017-10-24 19:57:04 +00:00
|
|
|
.arg(
|
2020-05-14 20:50:56 +00:00
|
|
|
Arg::new("exit-code")
|
2017-10-24 19:57:04 +00:00
|
|
|
.long("exit-code")
|
2022-07-26 00:17:01 +00:00
|
|
|
.action(ArgAction::Set)
|
2022-08-03 16:20:07 +00:00
|
|
|
.num_args(1)
|
2017-10-24 19:57:04 +00:00
|
|
|
.default_value("0"),
|
|
|
|
)
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec!["hello", "--exit-code=1"]);
|
2021-12-27 19:57:38 +00:00
|
|
|
assert!(res.is_ok(), "{}", res.unwrap_err());
|
2017-10-24 19:57:04 +00:00
|
|
|
let m = res.unwrap();
|
2022-05-24 15:16:50 +00:00
|
|
|
assert_eq!(
|
|
|
|
m.get_one::<String>("exit-code").map(|v| v.as_str()),
|
|
|
|
Some("1")
|
|
|
|
);
|
2018-01-25 04:05:05 +00:00
|
|
|
}
|
2020-10-23 10:55:08 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn required_groups_with_default_values() {
|
2022-02-12 03:48:29 +00:00
|
|
|
use clap::{Arg, ArgGroup, Command};
|
2020-10-23 10:55:08 +00:00
|
|
|
|
2022-06-10 15:16:06 +00:00
|
|
|
let cmd = Command::new("test")
|
2020-10-23 10:55:08 +00:00
|
|
|
.arg(Arg::new("arg").default_value("value"))
|
2022-08-12 17:40:58 +00:00
|
|
|
.group(ArgGroup::new("group").args(["arg"]).required(true));
|
2022-06-10 15:16:06 +00:00
|
|
|
|
|
|
|
let result = cmd.clone().try_get_matches_from(&["test"]);
|
|
|
|
assert!(result.is_err());
|
|
|
|
|
|
|
|
let result = cmd.clone().try_get_matches_from(&["test", "value"]);
|
|
|
|
assert!(result.is_ok(), "{}", result.unwrap_err());
|
|
|
|
let m = result.unwrap();
|
|
|
|
assert!(m.contains_id("arg"));
|
|
|
|
assert!(m.contains_id("group"));
|
2020-10-23 10:55:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn required_args_with_default_values() {
|
2022-02-12 03:48:29 +00:00
|
|
|
use clap::{Arg, Command};
|
2020-10-23 10:55:08 +00:00
|
|
|
|
2022-06-10 15:16:06 +00:00
|
|
|
let cmd = Command::new("test").arg(Arg::new("arg").required(true).default_value("value"));
|
|
|
|
|
|
|
|
let result = cmd.clone().try_get_matches_from(&["test"]);
|
|
|
|
assert!(result.is_err());
|
|
|
|
|
|
|
|
let result = cmd.clone().try_get_matches_from(&["test", "value"]);
|
|
|
|
assert!(result.is_ok(), "{}", result.unwrap_err());
|
|
|
|
let m = result.unwrap();
|
|
|
|
assert!(m.contains_id("arg"));
|
2020-10-23 10:55:08 +00:00
|
|
|
}
|
2021-09-18 22:15:25 +00:00
|
|
|
|
2022-02-08 18:59:30 +00:00
|
|
|
#[cfg(debug_assertions)]
|
|
|
|
#[test]
|
2022-08-29 20:09:06 +00:00
|
|
|
#[should_panic = "Argument `arg`'s default_value=\"value\" failed validation: error: \"value\" isn't a valid value for '[arg]'"]
|
2022-02-08 18:59:30 +00:00
|
|
|
fn default_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_value("value"),
|
|
|
|
)
|
|
|
|
.try_get_matches();
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(debug_assertions)]
|
|
|
|
#[test]
|
2022-08-29 20:09:06 +00:00
|
|
|
#[should_panic = "Argument `arg`'s default_value=\"one\" failed validation: error: Invalid value \"one\" for '[arg]"]
|
2022-02-28 15:21:39 +00:00
|
|
|
fn invalid_default_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-23 20:42:34 +00:00
|
|
|
.value_parser(clap::value_parser!(u32))
|
2022-02-28 15:21:39 +00:00
|
|
|
.default_value("one"),
|
|
|
|
)
|
|
|
|
.try_get_matches();
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn valid_delimited_default_values() {
|
|
|
|
use clap::{Arg, Command};
|
|
|
|
|
2022-08-03 19:48:50 +00:00
|
|
|
Command::new("test")
|
2022-02-28 15:21:39 +00:00
|
|
|
.arg(
|
|
|
|
Arg::new("arg")
|
2022-05-23 20:42:34 +00:00
|
|
|
.value_parser(clap::value_parser!(u32))
|
2022-08-03 15:59:42 +00:00
|
|
|
.value_delimiter(',')
|
2022-02-28 15:21:39 +00:00
|
|
|
.default_value("1,2,3"),
|
|
|
|
)
|
2022-08-03 19:48:50 +00:00
|
|
|
.debug_assert();
|
2022-02-28 15:21:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(debug_assertions)]
|
|
|
|
#[test]
|
2022-08-29 20:09:06 +00:00
|
|
|
#[should_panic = "Argument `arg`'s default_value=\"one\" failed validation: error: Invalid value \"one\" for '[arg]"]
|
2022-02-28 15:21:39 +00:00
|
|
|
fn invalid_delimited_default_values() {
|
|
|
|
use clap::{Arg, Command};
|
|
|
|
|
2022-08-03 19:48:50 +00:00
|
|
|
Command::new("test")
|
2022-02-28 15:21:39 +00:00
|
|
|
.arg(
|
|
|
|
Arg::new("arg")
|
2022-05-23 20:42:34 +00:00
|
|
|
.value_parser(clap::value_parser!(u32))
|
2022-08-03 15:59:42 +00:00
|
|
|
.value_delimiter(',')
|
2022-02-28 15:21:39 +00:00
|
|
|
.default_value("one,two"),
|
2022-02-08 18:59:30 +00:00
|
|
|
)
|
2022-08-03 19:48:50 +00:00
|
|
|
.debug_assert();
|
2022-02-08 18:59:30 +00:00
|
|
|
}
|
|
|
|
|
2021-09-18 22:15:25 +00:00
|
|
|
#[test]
|
|
|
|
fn with_value_delimiter() {
|
2022-02-14 21:47:20 +00:00
|
|
|
let cmd = Command::new("multiple_values").arg(
|
2021-09-18 22:15:25 +00:00
|
|
|
Arg::new("option")
|
|
|
|
.long("option")
|
2021-11-18 16:17:15 +00:00
|
|
|
.help("multiple options")
|
2021-09-18 22:15:25 +00:00
|
|
|
.value_delimiter(';')
|
|
|
|
.default_value("first;second"),
|
|
|
|
);
|
|
|
|
|
2022-02-14 21:47:20 +00:00
|
|
|
let matches = cmd.try_get_matches_from(vec![""]).unwrap();
|
2021-09-18 22:15:25 +00:00
|
|
|
|
|
|
|
assert_eq!(
|
2022-05-24 15:16:50 +00:00
|
|
|
matches
|
|
|
|
.get_many::<String>("option")
|
|
|
|
.unwrap()
|
|
|
|
.map(|v| v.as_str())
|
|
|
|
.collect::<Vec<_>>(),
|
2021-09-18 22:15:25 +00:00
|
|
|
["first", "second"]
|
|
|
|
);
|
|
|
|
}
|
2021-09-18 22:47:03 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn missing_with_value_delimiter() {
|
2022-02-14 21:47:20 +00:00
|
|
|
let cmd = Command::new("program").arg(
|
2021-09-18 22:47:03 +00:00
|
|
|
Arg::new("option")
|
|
|
|
.long("option")
|
|
|
|
.value_delimiter(';')
|
2022-08-03 19:51:38 +00:00
|
|
|
.num_args(0..=1)
|
2022-08-15 18:21:05 +00:00
|
|
|
.default_missing_values(["value1;value2;value3", "value4;value5"]),
|
2021-09-18 22:47:03 +00:00
|
|
|
);
|
|
|
|
|
2022-02-14 21:47:20 +00:00
|
|
|
let matches = cmd
|
2021-12-27 18:56:12 +00:00
|
|
|
.try_get_matches_from(vec!["program", "--option"])
|
|
|
|
.unwrap();
|
2021-09-18 22:47:03 +00:00
|
|
|
|
|
|
|
assert_eq!(
|
2022-05-24 15:16:50 +00:00
|
|
|
matches
|
|
|
|
.get_many::<String>("option")
|
|
|
|
.unwrap()
|
|
|
|
.map(|v| v.as_str())
|
|
|
|
.collect::<Vec<_>>(),
|
2021-09-18 22:47:03 +00:00
|
|
|
["value1", "value2", "value3", "value4", "value5"]
|
|
|
|
);
|
|
|
|
}
|
2022-05-27 01:50:41 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn default_independent_of_trailing() {
|
|
|
|
let cmd = Command::new("test")
|
|
|
|
.dont_delimit_trailing_values(true)
|
|
|
|
.arg(Arg::new("pos").required(true))
|
|
|
|
.arg(
|
|
|
|
Arg::new("flag")
|
|
|
|
.long("flag")
|
|
|
|
.default_value("one,two")
|
|
|
|
.value_delimiter(','),
|
|
|
|
);
|
|
|
|
|
|
|
|
// Baseline behavior
|
|
|
|
let m = cmd
|
|
|
|
.clone()
|
|
|
|
.try_get_matches_from(vec!["program", "here"])
|
|
|
|
.unwrap();
|
|
|
|
assert_eq!(
|
|
|
|
m.get_one::<String>("pos").map(|v| v.as_str()).unwrap(),
|
|
|
|
"here"
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
m.get_many::<String>("flag")
|
|
|
|
.unwrap()
|
|
|
|
.map(|v| v.as_str())
|
|
|
|
.collect::<Vec<_>>(),
|
|
|
|
["one", "two"]
|
|
|
|
);
|
|
|
|
|
|
|
|
// Trailing-values behavior should match the baseline
|
|
|
|
let m = cmd
|
|
|
|
.try_get_matches_from(vec!["program", "--", "here"])
|
|
|
|
.unwrap();
|
|
|
|
assert_eq!(
|
|
|
|
m.get_one::<String>("pos").map(|v| v.as_str()).unwrap(),
|
|
|
|
"here"
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
m.get_many::<String>("flag")
|
|
|
|
.unwrap()
|
|
|
|
.map(|v| v.as_str())
|
|
|
|
.collect::<Vec<_>>(),
|
|
|
|
["one", "two"]
|
|
|
|
);
|
|
|
|
}
|