2021-11-30 15:31:19 +00:00
|
|
|
use crate::utils;
|
2021-11-19 20:33:11 +00:00
|
|
|
use clap::{arg, App, Arg, ErrorKind};
|
2016-12-09 23:29:53 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn opts() {
|
|
|
|
let r = App::new("df")
|
2021-11-19 20:33:11 +00:00
|
|
|
.arg(
|
|
|
|
arg!(o: -o <opt> "some opt")
|
|
|
|
.required(false)
|
|
|
|
.default_value("default"),
|
|
|
|
)
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec![""]);
|
2016-12-09 23:29:53 +00:00
|
|
|
assert!(r.is_ok());
|
|
|
|
let m = r.unwrap();
|
|
|
|
assert!(m.is_present("o"));
|
|
|
|
assert_eq!(m.value_of("o").unwrap(), "default");
|
|
|
|
}
|
|
|
|
|
2020-10-13 21:21:01 +00:00
|
|
|
#[test]
|
|
|
|
fn opt_without_value_fail() {
|
|
|
|
let r = App::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")
|
|
|
|
.required(false)
|
2021-03-10 17:39:50 +00:00
|
|
|
.default_value("default")
|
|
|
|
.forbid_empty_values(true),
|
|
|
|
)
|
2020-10-13 21:21:01 +00:00
|
|
|
.try_get_matches_from(vec!["", "-o"]);
|
|
|
|
assert!(r.is_err());
|
|
|
|
let err = r.unwrap_err();
|
|
|
|
assert_eq!(err.kind, ErrorKind::EmptyValue);
|
|
|
|
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() {
|
|
|
|
let r = App::new("df")
|
2021-11-19 20:33:11 +00:00
|
|
|
.arg(
|
|
|
|
arg!(--opt <FILE> "some arg")
|
|
|
|
.required(false)
|
|
|
|
.default_value("default"),
|
|
|
|
)
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec!["", "--opt", "value"]);
|
2016-12-09 23:29:53 +00:00
|
|
|
assert!(r.is_ok());
|
|
|
|
let m = r.unwrap();
|
|
|
|
assert!(m.is_present("opt"));
|
|
|
|
assert_eq!(m.value_of("opt").unwrap(), "value");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn positionals() {
|
|
|
|
let r = App::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![""]);
|
2016-12-09 23:29:53 +00:00
|
|
|
assert!(r.is_ok());
|
|
|
|
let m = r.unwrap();
|
|
|
|
assert!(m.is_present("arg"));
|
|
|
|
assert_eq!(m.value_of("arg").unwrap(), "default");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn positional_user_override() {
|
|
|
|
let r = App::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"]);
|
2016-12-09 23:29:53 +00:00
|
|
|
assert!(r.is_ok());
|
|
|
|
let m = r.unwrap();
|
|
|
|
assert!(m.is_present("arg"));
|
|
|
|
assert_eq!(m.value_of("arg").unwrap(), "value");
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2017-04-24 19:49:59 +00:00
|
|
|
let r = App::new("df")
|
2021-11-19 20:33:11 +00:00
|
|
|
.arg(
|
|
|
|
arg!(o: -o <opt> "some opt")
|
|
|
|
.required(false)
|
|
|
|
.default_value_os(expected),
|
|
|
|
)
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec![""]);
|
2017-04-24 19:49:59 +00:00
|
|
|
assert!(r.is_ok());
|
|
|
|
let m = r.unwrap();
|
|
|
|
assert!(m.is_present("o"));
|
|
|
|
assert_eq!(m.value_of("o").unwrap(), expected);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn osstr_opt_user_override() {
|
|
|
|
use std::ffi::OsStr;
|
|
|
|
let default = OsStr::new("default");
|
|
|
|
|
|
|
|
let r = App::new("df")
|
2021-11-19 20:33:11 +00:00
|
|
|
.arg(
|
|
|
|
arg!(--opt <FILE> "some arg")
|
|
|
|
.required(false)
|
|
|
|
.default_value_os(default),
|
|
|
|
)
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec!["", "--opt", "value"]);
|
2017-04-24 19:49:59 +00:00
|
|
|
assert!(r.is_ok());
|
|
|
|
let m = r.unwrap();
|
|
|
|
assert!(m.is_present("opt"));
|
|
|
|
assert_eq!(m.value_of("opt").unwrap(), "value");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn osstr_positionals() {
|
|
|
|
use std::ffi::OsStr;
|
|
|
|
let expected = OsStr::new("default");
|
|
|
|
|
|
|
|
let r = App::new("df")
|
2021-11-19 20:33:11 +00:00
|
|
|
.arg(arg!([arg] "some opt").default_value_os(expected))
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec![""]);
|
2017-04-24 19:49:59 +00:00
|
|
|
assert!(r.is_ok());
|
|
|
|
let m = r.unwrap();
|
|
|
|
assert!(m.is_present("arg"));
|
|
|
|
assert_eq!(m.value_of("arg").unwrap(), expected);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn osstr_positional_user_override() {
|
|
|
|
use std::ffi::OsStr;
|
|
|
|
let default = OsStr::new("default");
|
|
|
|
|
|
|
|
let r = App::new("df")
|
2021-11-19 20:33:11 +00:00
|
|
|
.arg(arg!([arg] "some arg").default_value_os(default))
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec!["", "value"]);
|
2017-04-24 19:49:59 +00:00
|
|
|
assert!(r.is_ok());
|
|
|
|
let m = r.unwrap();
|
|
|
|
assert!(m.is_present("arg"));
|
|
|
|
assert_eq!(m.value_of("arg").unwrap(), "value");
|
|
|
|
}
|
|
|
|
|
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() {
|
|
|
|
let r = App::new("df")
|
2021-11-19 20:33:11 +00:00
|
|
|
.arg(arg!(--opt <FILE> "some arg").required(true))
|
|
|
|
.arg(arg!([arg] "some arg").default_value_if("opt", None, Some("default")))
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec!["", "--opt", "some"]);
|
2016-12-09 23:29:53 +00:00
|
|
|
assert!(r.is_ok());
|
|
|
|
let m = r.unwrap();
|
|
|
|
assert!(m.is_present("arg"));
|
|
|
|
assert_eq!(m.value_of("arg").unwrap(), "default");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn default_if_arg_present_no_default_user_override() {
|
|
|
|
let r = App::new("df")
|
2021-11-19 20:33:11 +00:00
|
|
|
.arg(arg!(--opt <FILE> "some arg").required(false))
|
|
|
|
.arg(arg!([arg] "some arg").default_value_if("opt", None, Some("default")))
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec!["", "--opt", "some", "other"]);
|
2016-12-09 23:29:53 +00:00
|
|
|
assert!(r.is_ok());
|
|
|
|
let m = r.unwrap();
|
|
|
|
assert!(m.is_present("arg"));
|
|
|
|
assert_eq!(m.value_of("arg").unwrap(), "other");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn default_if_arg_present_no_arg_with_default() {
|
|
|
|
let r = App::new("df")
|
2021-11-19 20:33:11 +00:00
|
|
|
.arg(arg!(--opt <FILE> "some arg").required(false))
|
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")
|
2021-03-09 18:26:38 +00:00
|
|
|
.default_value_if("opt", None, Some("default")),
|
2017-09-29 18:56:53 +00:00
|
|
|
)
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec![""]);
|
2016-12-09 23:29:53 +00:00
|
|
|
assert!(r.is_ok());
|
|
|
|
let m = r.unwrap();
|
|
|
|
assert!(m.is_present("arg"));
|
|
|
|
assert_eq!(m.value_of("arg").unwrap(), "first");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn default_if_arg_present_with_default() {
|
|
|
|
let r = App::new("df")
|
2021-11-19 20:33:11 +00:00
|
|
|
.arg(arg!(--opt <FILE> "some arg").required(false))
|
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")
|
2021-03-09 18:26:38 +00:00
|
|
|
.default_value_if("opt", None, 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"]);
|
2016-12-09 23:29:53 +00:00
|
|
|
assert!(r.is_ok());
|
|
|
|
let m = r.unwrap();
|
|
|
|
assert!(m.is_present("arg"));
|
|
|
|
assert_eq!(m.value_of("arg").unwrap(), "default");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn default_if_arg_present_with_default_user_override() {
|
|
|
|
let r = App::new("df")
|
2021-11-19 20:33:11 +00:00
|
|
|
.arg(arg!(--opt <FILE> "some arg").required(false))
|
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")
|
2021-03-09 18:26:38 +00:00
|
|
|
.default_value_if("opt", None, 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"]);
|
2016-12-09 23:29:53 +00:00
|
|
|
assert!(r.is_ok());
|
|
|
|
let m = r.unwrap();
|
|
|
|
assert!(m.is_present("arg"));
|
|
|
|
assert_eq!(m.value_of("arg").unwrap(), "other");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn default_if_arg_present_no_arg_with_default_user_override() {
|
|
|
|
let r = App::new("df")
|
2021-11-19 20:33:11 +00:00
|
|
|
.arg(arg!(--opt <FILE> "some arg").required(false))
|
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")
|
2021-03-09 18:26:38 +00:00
|
|
|
.default_value_if("opt", None, Some("default")),
|
2017-09-29 18:56:53 +00:00
|
|
|
)
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec!["", "other"]);
|
2016-12-09 23:29:53 +00:00
|
|
|
assert!(r.is_ok());
|
|
|
|
let m = r.unwrap();
|
|
|
|
assert!(m.is_present("arg"));
|
|
|
|
assert_eq!(m.value_of("arg").unwrap(), "other");
|
|
|
|
}
|
|
|
|
|
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() {
|
|
|
|
let r = App::new("df")
|
2021-11-19 20:33:11 +00:00
|
|
|
.arg(arg!(--opt <FILE> "some arg").required(false))
|
|
|
|
.arg(arg!([arg] "some arg").default_value_if("opt", Some("value"), Some("default")))
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec!["", "--opt", "value"]);
|
2016-12-09 23:29:53 +00:00
|
|
|
assert!(r.is_ok());
|
|
|
|
let m = r.unwrap();
|
|
|
|
assert!(m.is_present("arg"));
|
|
|
|
assert_eq!(m.value_of("arg").unwrap(), "default");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn default_if_arg_present_with_value_no_default_fail() {
|
|
|
|
let r = App::new("df")
|
2021-11-19 20:33:11 +00:00
|
|
|
.arg(arg!(--opt <FILE> "some arg").required(false))
|
|
|
|
.arg(arg!([arg] "some arg").default_value_if("opt", Some("value"), Some("default")))
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec!["", "--opt", "other"]);
|
2016-12-09 23:29:53 +00:00
|
|
|
assert!(r.is_ok());
|
|
|
|
let m = r.unwrap();
|
|
|
|
assert!(!m.is_present("arg"));
|
2021-10-25 17:36:01 +00:00
|
|
|
assert!(m.value_of("arg").is_none());
|
2016-12-09 23:29:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn default_if_arg_present_with_value_no_default_user_override() {
|
|
|
|
let r = App::new("df")
|
2021-11-19 20:33:11 +00:00
|
|
|
.arg(arg!(--opt <FILE> "some arg").required(false))
|
|
|
|
.arg(arg!([arg] "some arg").default_value_if("opt", Some("some"), Some("default")))
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec!["", "--opt", "some", "other"]);
|
2016-12-09 23:29:53 +00:00
|
|
|
assert!(r.is_ok());
|
|
|
|
let m = r.unwrap();
|
|
|
|
assert!(m.is_present("arg"));
|
|
|
|
assert_eq!(m.value_of("arg").unwrap(), "other");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn default_if_arg_present_with_value_no_arg_with_default() {
|
|
|
|
let r = App::new("df")
|
2021-11-19 20:33:11 +00:00
|
|
|
.arg(arg!(--opt <FILE> "some arg").required(false))
|
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")
|
2021-03-09 18:26:38 +00:00
|
|
|
.default_value_if("opt", Some("some"), Some("default")),
|
2017-09-29 18:56:53 +00:00
|
|
|
)
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec![""]);
|
2016-12-09 23:29:53 +00:00
|
|
|
assert!(r.is_ok());
|
|
|
|
let m = r.unwrap();
|
|
|
|
assert!(m.is_present("arg"));
|
|
|
|
assert_eq!(m.value_of("arg").unwrap(), "first");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn default_if_arg_present_with_value_no_arg_with_default_fail() {
|
|
|
|
let r = App::new("df")
|
2021-11-19 20:33:11 +00:00
|
|
|
.arg(arg!(--opt <FILE> "some arg").required(false))
|
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")
|
2021-03-09 18:26:38 +00:00
|
|
|
.default_value_if("opt", Some("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"]);
|
2016-12-09 23:29:53 +00:00
|
|
|
assert!(r.is_ok());
|
|
|
|
let m = r.unwrap();
|
|
|
|
assert!(m.is_present("arg"));
|
|
|
|
assert_eq!(m.value_of("arg").unwrap(), "first");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn default_if_arg_present_with_value_with_default() {
|
|
|
|
let r = App::new("df")
|
2021-11-19 20:33:11 +00:00
|
|
|
.arg(arg!(--opt <FILE> "some arg").required(false))
|
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")
|
2021-03-09 18:26:38 +00:00
|
|
|
.default_value_if("opt", Some("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"]);
|
2016-12-09 23:29:53 +00:00
|
|
|
assert!(r.is_ok());
|
|
|
|
let m = r.unwrap();
|
|
|
|
assert!(m.is_present("arg"));
|
|
|
|
assert_eq!(m.value_of("arg").unwrap(), "default");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn default_if_arg_present_with_value_with_default_user_override() {
|
|
|
|
let r = App::new("df")
|
2021-11-19 20:33:11 +00:00
|
|
|
.arg(arg!(--opt <FILE> "some arg").required(false))
|
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")
|
2021-03-09 18:26:38 +00:00
|
|
|
.default_value_if("opt", Some("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"]);
|
2016-12-09 23:29:53 +00:00
|
|
|
assert!(r.is_ok());
|
|
|
|
let m = r.unwrap();
|
|
|
|
assert!(m.is_present("arg"));
|
|
|
|
assert_eq!(m.value_of("arg").unwrap(), "other");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn default_if_arg_present_no_arg_with_value_with_default_user_override() {
|
|
|
|
let r = App::new("df")
|
2021-11-19 20:33:11 +00:00
|
|
|
.arg(arg!(--opt <FILE> "some arg").required(false))
|
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")
|
2021-03-09 18:26:38 +00:00
|
|
|
.default_value_if("opt", Some("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"]);
|
2016-12-09 23:29:53 +00:00
|
|
|
assert!(r.is_ok());
|
|
|
|
let m = r.unwrap();
|
|
|
|
assert!(m.is_present("arg"));
|
|
|
|
assert_eq!(m.value_of("arg").unwrap(), "other");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn default_if_arg_present_no_arg_with_value_with_default_user_override_fail() {
|
|
|
|
let r = App::new("df")
|
2021-11-19 20:33:11 +00:00
|
|
|
.arg(arg!(--opt <FILE> "some arg").required(false))
|
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")
|
2021-03-09 18:26:38 +00:00
|
|
|
.default_value_if("opt", Some("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"]);
|
2016-12-09 23:29:53 +00:00
|
|
|
assert!(r.is_ok());
|
|
|
|
let m = r.unwrap();
|
|
|
|
assert!(m.is_present("arg"));
|
|
|
|
assert_eq!(m.value_of("arg").unwrap(), "other");
|
|
|
|
}
|
|
|
|
|
2021-01-17 07:28:04 +00:00
|
|
|
// Unsetting the default
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn no_default_if_arg_present_with_value_no_default() {
|
|
|
|
let r = App::new("df")
|
2021-11-19 20:33:11 +00:00
|
|
|
.arg(arg!(--opt <FILE> "some arg").required(false))
|
|
|
|
.arg(arg!([arg] "some arg").default_value_if("opt", Some("value"), None))
|
2021-01-17 07:28:04 +00:00
|
|
|
.try_get_matches_from(vec!["", "--opt", "value"]);
|
|
|
|
assert!(r.is_ok());
|
|
|
|
let m = r.unwrap();
|
|
|
|
assert!(!m.is_present("arg"));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn no_default_if_arg_present_with_value_with_default() {
|
|
|
|
let r = App::new("df")
|
2021-11-19 20:33:11 +00:00
|
|
|
.arg(arg!(--opt <FILE> "some arg").required(false))
|
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")
|
|
|
|
.default_value_if("opt", Some("value"), None),
|
|
|
|
)
|
|
|
|
.try_get_matches_from(vec!["", "--opt", "value"]);
|
|
|
|
assert!(r.is_ok());
|
|
|
|
let m = r.unwrap();
|
|
|
|
assert!(!m.is_present("arg"));
|
2021-10-25 17:36:01 +00:00
|
|
|
assert!(m.value_of("arg").is_none());
|
2021-01-17 07:28:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn no_default_if_arg_present_with_value_with_default_user_override() {
|
|
|
|
let r = App::new("df")
|
2021-11-19 20:33:11 +00:00
|
|
|
.arg(arg!(--opt <FILE> "some arg").required(false))
|
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")
|
|
|
|
.default_value_if("opt", Some("value"), None),
|
|
|
|
)
|
|
|
|
.try_get_matches_from(vec!["", "--opt", "value", "other"]);
|
|
|
|
assert!(r.is_ok());
|
|
|
|
let m = r.unwrap();
|
|
|
|
assert!(m.is_present("arg"));
|
|
|
|
assert_eq!(m.value_of("arg").unwrap(), "other");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn no_default_if_arg_present_no_arg_with_value_with_default() {
|
|
|
|
let r = App::new("df")
|
2021-11-19 20:33:11 +00:00
|
|
|
.arg(arg!(--opt <FILE> "some arg").required(false))
|
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")
|
|
|
|
.default_value_if("opt", Some("value"), None),
|
|
|
|
)
|
|
|
|
.try_get_matches_from(vec!["", "--opt", "other"]);
|
|
|
|
assert!(r.is_ok());
|
|
|
|
let m = r.unwrap();
|
|
|
|
assert!(m.is_present("arg"));
|
|
|
|
assert_eq!(m.value_of("arg").unwrap(), "default");
|
|
|
|
}
|
|
|
|
|
2016-12-09 23:29:53 +00:00
|
|
|
// Multiple conditions
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn default_ifs_arg_present() {
|
|
|
|
let r = App::new("df")
|
2021-11-19 20:33:11 +00:00
|
|
|
.arg(arg!(--opt <FILE> "some arg").required(false))
|
|
|
|
.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")
|
2021-03-09 18:26:38 +00:00
|
|
|
.default_value_ifs(&[
|
|
|
|
("opt", Some("some"), Some("default")),
|
|
|
|
("flag", None, Some("flg")),
|
|
|
|
]),
|
2017-09-29 18:56:53 +00:00
|
|
|
)
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec!["", "--flag"]);
|
2016-12-09 23:29:53 +00:00
|
|
|
assert!(r.is_ok());
|
|
|
|
let m = r.unwrap();
|
|
|
|
assert!(m.is_present("arg"));
|
|
|
|
assert_eq!(m.value_of("arg").unwrap(), "flg");
|
|
|
|
}
|
|
|
|
|
2021-01-17 07:28:04 +00:00
|
|
|
#[test]
|
|
|
|
fn no_default_ifs_arg_present() {
|
|
|
|
let r = App::new("df")
|
2021-11-19 20:33:11 +00:00
|
|
|
.arg(arg!(--opt <FILE> "some arg").required(false))
|
|
|
|
.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")
|
|
|
|
.default_value_ifs(&[("opt", Some("some"), Some("default")), ("flag", None, None)]),
|
|
|
|
)
|
|
|
|
.try_get_matches_from(vec!["", "--flag"]);
|
|
|
|
assert!(r.is_ok());
|
|
|
|
let m = r.unwrap();
|
|
|
|
assert!(!m.is_present("arg"));
|
2021-10-25 17:36:01 +00:00
|
|
|
assert!(m.value_of("arg").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() {
|
|
|
|
let r = App::new("df")
|
2021-11-19 20:33:11 +00:00
|
|
|
.arg(arg!(--opt <FILE> "some arg").required(false))
|
|
|
|
.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")
|
2021-03-09 18:26:38 +00:00
|
|
|
.default_value_ifs(&[
|
|
|
|
("opt", Some("some"), Some("default")),
|
|
|
|
("flag", None, Some("flg")),
|
|
|
|
]),
|
2017-09-29 18:56:53 +00:00
|
|
|
)
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec!["", "--flag", "value"]);
|
2016-12-09 23:29:53 +00:00
|
|
|
assert!(r.is_ok());
|
|
|
|
let m = r.unwrap();
|
|
|
|
assert!(m.is_present("arg"));
|
|
|
|
assert_eq!(m.value_of("arg").unwrap(), "value");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn default_ifs_arg_present_order() {
|
|
|
|
let r = App::new("df")
|
2021-11-19 20:33:11 +00:00
|
|
|
.arg(arg!(--opt <FILE> "some arg").required(false))
|
|
|
|
.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")
|
2021-03-09 18:26:38 +00:00
|
|
|
.default_value_ifs(&[
|
|
|
|
("opt", Some("some"), Some("default")),
|
|
|
|
("flag", None, Some("flg")),
|
|
|
|
]),
|
2017-09-29 18:56:53 +00:00
|
|
|
)
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec!["", "--opt=some", "--flag"]);
|
2016-12-09 23:29:53 +00:00
|
|
|
assert!(r.is_ok());
|
|
|
|
let m = r.unwrap();
|
|
|
|
assert!(m.is_present("arg"));
|
|
|
|
assert_eq!(m.value_of("arg").unwrap(), "default");
|
2017-02-03 22:43:49 +00:00
|
|
|
}
|
|
|
|
|
2021-10-25 17:36:01 +00:00
|
|
|
// Interaction with requires
|
2017-02-03 22:43:49 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn conditional_reqs_pass() {
|
|
|
|
let m = App::new("Test app")
|
2017-09-29 18:56:53 +00:00
|
|
|
.arg(
|
2020-05-14 20:50:56 +00:00
|
|
|
Arg::new("target")
|
2017-09-29 18:56:53 +00:00
|
|
|
.takes_value(true)
|
|
|
|
.default_value("file")
|
|
|
|
.long("target"),
|
|
|
|
)
|
|
|
|
.arg(
|
2020-05-14 20:50:56 +00:00
|
|
|
Arg::new("input")
|
2017-09-29 18:56:53 +00:00
|
|
|
.takes_value(true)
|
|
|
|
.required(true)
|
|
|
|
.long("input"),
|
|
|
|
)
|
|
|
|
.arg(
|
2020-05-14 20:50:56 +00:00
|
|
|
Arg::new("output")
|
2017-09-29 18:56:53 +00:00
|
|
|
.takes_value(true)
|
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
|
|
|
|
|
|
|
assert!(m.is_ok());
|
|
|
|
let m = m.unwrap();
|
|
|
|
assert_eq!(m.value_of("output"), Some("other"));
|
|
|
|
assert_eq!(m.value_of("input"), 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() {
|
|
|
|
let r = App::new("diff")
|
|
|
|
.arg(
|
2020-05-14 20:50:56 +00:00
|
|
|
Arg::new("files")
|
2019-10-10 16:48:22 +00:00
|
|
|
.long("files")
|
|
|
|
.number_of_values(2)
|
2021-11-29 19:16:03 +00:00
|
|
|
.allow_invalid_utf8(true)
|
2019-10-10 16:48:22 +00:00
|
|
|
.default_values(&["old", "new"]),
|
|
|
|
)
|
|
|
|
.try_get_matches_from(vec![""]);
|
|
|
|
assert!(r.is_ok());
|
|
|
|
let m = r.unwrap();
|
|
|
|
assert!(m.is_present("files"));
|
|
|
|
assert_eq!(m.values_of_lossy("files").unwrap(), vec!["old", "new"]);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn multiple_defaults_override() {
|
|
|
|
let r = App::new("diff")
|
|
|
|
.arg(
|
2020-05-14 20:50:56 +00:00
|
|
|
Arg::new("files")
|
2019-10-10 16:48:22 +00:00
|
|
|
.long("files")
|
|
|
|
.number_of_values(2)
|
2021-11-29 19:16:03 +00:00
|
|
|
.allow_invalid_utf8(true)
|
2019-10-10 16:48:22 +00:00
|
|
|
.default_values(&["old", "new"]),
|
|
|
|
)
|
|
|
|
.try_get_matches_from(vec!["", "--files", "other", "mine"]);
|
|
|
|
assert!(r.is_ok());
|
|
|
|
let m = r.unwrap();
|
|
|
|
assert!(m.is_present("files"));
|
|
|
|
assert_eq!(m.values_of_lossy("files").unwrap(), vec!["other", "mine"]);
|
|
|
|
}
|
|
|
|
|
2021-07-20 18:50:41 +00:00
|
|
|
#[test]
|
|
|
|
fn default_vals_donnot_show_in_smart_usage() {
|
|
|
|
let app = App::new("bug")
|
|
|
|
.arg(
|
|
|
|
Arg::new("foo")
|
|
|
|
.long("config")
|
|
|
|
.takes_value(true)
|
|
|
|
.default_value("bar"),
|
|
|
|
)
|
|
|
|
.arg(Arg::new("input").required(true));
|
2021-10-25 17:36:01 +00:00
|
|
|
|
2021-07-20 18:50:41 +00:00
|
|
|
assert!(utils::compare_output(
|
|
|
|
app,
|
|
|
|
"bug",
|
|
|
|
"error: The following required arguments were not provided:
|
|
|
|
<input>
|
|
|
|
|
|
|
|
USAGE:
|
|
|
|
bug [OPTIONS] <input>
|
|
|
|
|
2021-09-24 15:58:39 +00:00
|
|
|
For more information try --help
|
|
|
|
",
|
2021-07-20 18:50:41 +00:00
|
|
|
true,
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
2017-10-24 19:57:04 +00:00
|
|
|
#[test]
|
|
|
|
fn issue_1050_num_vals_and_defaults() {
|
|
|
|
let res = App::new("hello")
|
|
|
|
.arg(
|
2020-05-14 20:50:56 +00:00
|
|
|
Arg::new("exit-code")
|
2017-10-24 19:57:04 +00:00
|
|
|
.long("exit-code")
|
|
|
|
.takes_value(true)
|
|
|
|
.number_of_values(1)
|
|
|
|
.default_value("0"),
|
|
|
|
)
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec!["hello", "--exit-code=1"]);
|
2017-10-24 19:57:04 +00:00
|
|
|
assert!(res.is_ok());
|
|
|
|
let m = res.unwrap();
|
|
|
|
assert_eq!(m.value_of("exit-code"), Some("1"));
|
2018-01-25 04:05:05 +00:00
|
|
|
}
|
2020-10-23 10:55:08 +00:00
|
|
|
|
|
|
|
#[cfg(debug_assertions)]
|
|
|
|
#[test]
|
2021-10-25 17:48:03 +00:00
|
|
|
#[should_panic = "Argument group 'group' is required but all of it's arguments have a default value."]
|
2020-10-23 10:55:08 +00:00
|
|
|
fn required_groups_with_default_values() {
|
|
|
|
use clap::{App, Arg, ArgGroup};
|
|
|
|
|
|
|
|
let _ = App::new("test")
|
|
|
|
.arg(Arg::new("arg").default_value("value"))
|
|
|
|
.group(ArgGroup::new("group").args(&["arg"]).required(true))
|
|
|
|
.try_get_matches();
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(debug_assertions)]
|
|
|
|
#[test]
|
|
|
|
#[should_panic = "Argument 'arg' is required and can't have a default value"]
|
|
|
|
fn required_args_with_default_values() {
|
|
|
|
use clap::{App, Arg};
|
|
|
|
|
|
|
|
let _ = App::new("test")
|
|
|
|
.arg(Arg::new("arg").required(true).default_value("value"))
|
|
|
|
.try_get_matches();
|
|
|
|
}
|
2021-09-18 22:15:25 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn with_value_delimiter() {
|
|
|
|
let app = App::new("multiple_values").arg(
|
|
|
|
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"),
|
|
|
|
);
|
|
|
|
|
|
|
|
let matches = app.get_matches_from(vec![""]);
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
matches.values_of("option").unwrap().collect::<Vec<_>>(),
|
|
|
|
["first", "second"]
|
|
|
|
);
|
|
|
|
}
|
2021-09-18 22:47:03 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn missing_with_value_delimiter() {
|
|
|
|
let app = App::new("program").arg(
|
|
|
|
Arg::new("option")
|
|
|
|
.long("option")
|
|
|
|
.value_delimiter(';')
|
|
|
|
.default_missing_values(&["value1;value2;value3", "value4;value5"]),
|
|
|
|
);
|
|
|
|
|
|
|
|
let matches = app.get_matches_from(vec!["program", "--option"]);
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
matches.values_of("option").unwrap().collect::<Vec<_>>(),
|
|
|
|
["value1", "value2", "value3", "value4", "value5"]
|
|
|
|
);
|
|
|
|
}
|