2021-11-30 15:31:19 +00:00
|
|
|
use crate::utils;
|
2015-08-27 21:03:45 +00:00
|
|
|
|
2021-11-29 14:56:30 +00:00
|
|
|
use clap::{arg, App, AppSettings, Arg, ArgMatches, ErrorKind};
|
2015-08-27 21:03:45 +00:00
|
|
|
|
2017-01-03 04:05:23 +00:00
|
|
|
#[cfg(feature = "suggestions")]
|
2019-10-02 13:27:19 +00:00
|
|
|
static DYM: &str =
|
2018-01-25 04:05:05 +00:00
|
|
|
"error: Found argument '--optio' which wasn't expected, or isn't valid in this context
|
2020-04-12 01:39:13 +00:00
|
|
|
|
|
|
|
\tDid you mean '--option'?
|
|
|
|
|
2020-10-09 16:31:00 +00:00
|
|
|
\tIf you tried to supply `--optio` as a value rather than a flag, use `-- --optio`
|
2017-01-03 04:05:23 +00:00
|
|
|
|
|
|
|
USAGE:
|
|
|
|
clap-test --option <opt>...
|
|
|
|
|
2021-09-24 15:58:39 +00:00
|
|
|
For more information try --help
|
|
|
|
";
|
2017-01-03 04:05:23 +00:00
|
|
|
|
2020-04-12 15:44:48 +00:00
|
|
|
#[cfg(feature = "suggestions")]
|
|
|
|
static DYM_ISSUE_1073: &str =
|
|
|
|
"error: Found argument '--files-without-matches' which wasn't expected, or isn't valid in this context
|
2020-04-12 01:39:13 +00:00
|
|
|
|
|
|
|
\tDid you mean '--files-without-match'?
|
|
|
|
|
2020-10-09 16:31:00 +00:00
|
|
|
\tIf you tried to supply `--files-without-matches` as a value rather than a flag, use `-- --files-without-matches`
|
2020-04-12 15:44:48 +00:00
|
|
|
|
|
|
|
USAGE:
|
|
|
|
ripgrep-616 --files-without-match
|
|
|
|
|
2021-09-24 15:58:39 +00:00
|
|
|
For more information try --help
|
|
|
|
";
|
2020-04-12 15:44:48 +00:00
|
|
|
|
2017-02-18 02:56:34 +00:00
|
|
|
#[test]
|
|
|
|
fn require_equals_fail() {
|
|
|
|
let res = App::new("prog")
|
2018-01-25 04:05:05 +00:00
|
|
|
.arg(
|
2020-05-14 20:50:56 +00:00
|
|
|
Arg::new("cfg")
|
2021-11-29 14:56:30 +00:00
|
|
|
.require_equals(true)
|
|
|
|
.forbid_empty_values(true)
|
|
|
|
.takes_value(true)
|
2018-01-25 04:05:05 +00:00
|
|
|
.long("config"),
|
|
|
|
)
|
2018-01-31 20:15:01 +00:00
|
|
|
.try_get_matches_from(vec!["prog", "--config", "file.conf"]);
|
2017-02-18 02:56:34 +00:00
|
|
|
assert!(res.is_err());
|
2021-03-09 16:59:12 +00:00
|
|
|
assert_eq!(res.unwrap_err().kind, ErrorKind::NoEquals);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn require_equals_fail_message() {
|
|
|
|
static NO_EQUALS: &str =
|
|
|
|
"error: Equal sign is needed when assigning values to '--config=<cfg>'.
|
|
|
|
|
|
|
|
USAGE:
|
|
|
|
prog [OPTIONS]
|
|
|
|
|
2021-09-24 15:58:39 +00:00
|
|
|
For more information try --help
|
|
|
|
";
|
2021-03-09 16:59:12 +00:00
|
|
|
let app = App::new("prog").arg(
|
|
|
|
Arg::new("cfg")
|
2021-11-29 14:56:30 +00:00
|
|
|
.require_equals(true)
|
|
|
|
.takes_value(true)
|
2021-03-09 16:59:12 +00:00
|
|
|
.long("config"),
|
|
|
|
);
|
|
|
|
assert!(utils::compare_output(
|
|
|
|
app,
|
|
|
|
"prog --config file.conf",
|
|
|
|
NO_EQUALS,
|
|
|
|
true
|
|
|
|
));
|
2017-02-18 02:56:34 +00:00
|
|
|
}
|
|
|
|
|
2017-09-13 19:27:19 +00:00
|
|
|
#[test]
|
|
|
|
fn require_equals_min_values_zero() {
|
|
|
|
let res = App::new("prog")
|
2018-01-25 04:05:05 +00:00
|
|
|
.arg(
|
2020-05-14 20:50:56 +00:00
|
|
|
Arg::new("cfg")
|
2021-11-29 14:56:30 +00:00
|
|
|
.takes_value(true)
|
|
|
|
.require_equals(true)
|
2018-01-25 04:05:05 +00:00
|
|
|
.min_values(0)
|
|
|
|
.long("config"),
|
|
|
|
)
|
2020-05-14 20:50:56 +00:00
|
|
|
.arg(Arg::new("cmd"))
|
2018-01-31 20:15:01 +00:00
|
|
|
.try_get_matches_from(vec!["prog", "--config", "cmd"]);
|
2017-09-13 19:27:19 +00:00
|
|
|
assert!(res.is_ok());
|
|
|
|
let m = res.unwrap();
|
|
|
|
assert!(m.is_present("cfg"));
|
|
|
|
assert_eq!(m.value_of("cmd"), Some("cmd"));
|
|
|
|
}
|
|
|
|
|
2017-05-15 22:08:33 +00:00
|
|
|
#[test]
|
|
|
|
fn double_hyphen_as_value() {
|
|
|
|
let res = App::new("prog")
|
2018-01-25 04:05:05 +00:00
|
|
|
.arg(
|
2020-05-14 20:50:56 +00:00
|
|
|
Arg::new("cfg")
|
2021-11-29 14:56:30 +00:00
|
|
|
.takes_value(true)
|
|
|
|
.allow_hyphen_values(true)
|
2018-01-25 04:05:05 +00:00
|
|
|
.long("config"),
|
|
|
|
)
|
2018-01-31 20:15:01 +00:00
|
|
|
.try_get_matches_from(vec!["prog", "--config", "--"]);
|
2017-05-15 22:08:33 +00:00
|
|
|
assert!(res.is_ok(), "{:?}", res);
|
|
|
|
assert_eq!(res.unwrap().value_of("cfg"), Some("--"));
|
|
|
|
}
|
|
|
|
|
2017-02-18 02:56:34 +00:00
|
|
|
#[test]
|
|
|
|
fn require_equals_no_empty_values_fail() {
|
|
|
|
let res = App::new("prog")
|
2018-01-25 04:05:05 +00:00
|
|
|
.arg(
|
2020-05-14 20:50:56 +00:00
|
|
|
Arg::new("cfg")
|
2021-11-29 14:56:30 +00:00
|
|
|
.takes_value(true)
|
|
|
|
.require_equals(true)
|
|
|
|
.forbid_empty_values(true)
|
2018-01-25 04:05:05 +00:00
|
|
|
.long("config"),
|
|
|
|
)
|
2020-05-14 20:50:56 +00:00
|
|
|
.arg(Arg::new("some"))
|
2018-01-31 20:15:01 +00:00
|
|
|
.try_get_matches_from(vec!["prog", "--config=", "file.conf"]);
|
2017-02-18 02:56:34 +00:00
|
|
|
assert!(res.is_err());
|
|
|
|
assert_eq!(res.unwrap_err().kind, ErrorKind::EmptyValue);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn require_equals_empty_vals_pass() {
|
|
|
|
let res = App::new("prog")
|
2018-01-25 04:05:05 +00:00
|
|
|
.arg(
|
2020-05-14 20:50:56 +00:00
|
|
|
Arg::new("cfg")
|
2021-11-29 14:56:30 +00:00
|
|
|
.takes_value(true)
|
|
|
|
.require_equals(true)
|
2018-01-25 04:05:05 +00:00
|
|
|
.long("config"),
|
|
|
|
)
|
2018-01-31 20:15:01 +00:00
|
|
|
.try_get_matches_from(vec!["prog", "--config="]);
|
2017-02-18 02:56:34 +00:00
|
|
|
assert!(res.is_ok());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn require_equals_pass() {
|
|
|
|
let res = App::new("prog")
|
2018-01-25 04:05:05 +00:00
|
|
|
.arg(
|
2020-05-14 20:50:56 +00:00
|
|
|
Arg::new("cfg")
|
2021-11-29 14:56:30 +00:00
|
|
|
.takes_value(true)
|
|
|
|
.require_equals(true)
|
2018-01-25 04:05:05 +00:00
|
|
|
.long("config"),
|
|
|
|
)
|
2018-01-31 20:15:01 +00:00
|
|
|
.try_get_matches_from(vec!["prog", "--config=file.conf"]);
|
2017-02-18 02:56:34 +00:00
|
|
|
assert!(res.is_ok());
|
|
|
|
}
|
|
|
|
|
2016-02-04 06:34:46 +00:00
|
|
|
#[test]
|
|
|
|
fn stdin_char() {
|
|
|
|
let r = App::new("opts")
|
2021-11-19 20:33:11 +00:00
|
|
|
.arg(arg!(f: -f [flag] "some flag"))
|
2018-01-31 20:15:01 +00:00
|
|
|
.try_get_matches_from(vec!["", "-f", "-"]);
|
2016-02-04 06:34:46 +00:00
|
|
|
assert!(r.is_ok());
|
|
|
|
let m = r.unwrap();
|
|
|
|
assert!(m.is_present("f"));
|
|
|
|
assert_eq!(m.value_of("f").unwrap(), "-");
|
|
|
|
}
|
|
|
|
|
2015-08-27 21:03:45 +00:00
|
|
|
#[test]
|
|
|
|
fn opts_using_short() {
|
2016-01-26 07:05:32 +00:00
|
|
|
let r = App::new("opts")
|
2018-01-25 04:05:05 +00:00
|
|
|
.args(&[
|
2021-11-19 20:33:11 +00:00
|
|
|
arg!(f: -f [flag] "some flag"),
|
|
|
|
arg!(c: -c [color] "some other flag"),
|
2018-01-25 04:05:05 +00:00
|
|
|
])
|
2018-01-31 20:15:01 +00:00
|
|
|
.try_get_matches_from(vec!["", "-f", "some", "-c", "other"]);
|
2016-01-26 07:05:32 +00:00
|
|
|
assert!(r.is_ok());
|
|
|
|
let m = r.unwrap();
|
2016-01-25 20:56:37 +00:00
|
|
|
assert!(m.is_present("f"));
|
|
|
|
assert_eq!(m.value_of("f").unwrap(), "some");
|
|
|
|
assert!(m.is_present("c"));
|
|
|
|
assert_eq!(m.value_of("c").unwrap(), "other");
|
2015-08-27 21:03:45 +00:00
|
|
|
}
|
|
|
|
|
2016-02-02 12:43:53 +00:00
|
|
|
#[test]
|
|
|
|
fn lots_o_vals() {
|
|
|
|
let r = App::new("opts")
|
2021-11-19 20:33:11 +00:00
|
|
|
.arg(arg!(o: -o <opt> "some opt").multiple_values(true))
|
2018-01-31 20:15:01 +00:00
|
|
|
.try_get_matches_from(vec![
|
2018-01-25 04:05:05 +00:00
|
|
|
"", "-o", "some", "some", "some", "some", "some", "some", "some", "some", "some",
|
|
|
|
"some", "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
|
|
|
|
"some", "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
|
|
|
|
"some", "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
|
|
|
|
"some", "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
|
|
|
|
"some", "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
|
|
|
|
"some", "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
|
|
|
|
"some", "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
|
|
|
|
"some", "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
|
|
|
|
"some", "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
|
|
|
|
"some", "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
|
|
|
|
"some", "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
|
|
|
|
"some", "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
|
|
|
|
"some", "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
|
|
|
|
"some", "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
|
|
|
|
"some", "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
|
|
|
|
"some", "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
|
|
|
|
"some", "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
|
|
|
|
"some", "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
|
|
|
|
"some", "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
|
|
|
|
"some", "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
|
|
|
|
"some", "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
|
|
|
|
"some", "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
|
|
|
|
"some", "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
|
|
|
|
"some", "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
|
|
|
|
"some", "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
|
|
|
|
"some", "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
|
|
|
|
"some", "some",
|
|
|
|
]);
|
2016-02-02 12:43:53 +00:00
|
|
|
assert!(r.is_ok());
|
|
|
|
let m = r.unwrap();
|
|
|
|
assert!(m.is_present("o"));
|
2019-10-02 13:27:19 +00:00
|
|
|
assert_eq!(m.values_of("o").unwrap().count(), 297); // i.e. more than u8
|
2016-02-02 12:43:53 +00:00
|
|
|
}
|
|
|
|
|
2015-08-27 21:03:45 +00:00
|
|
|
#[test]
|
|
|
|
fn opts_using_long_space() {
|
2016-01-26 07:05:32 +00:00
|
|
|
let r = App::new("opts")
|
2018-01-25 04:05:05 +00:00
|
|
|
.args(&[
|
2021-11-19 20:33:11 +00:00
|
|
|
arg!(--flag [flag] "some flag"),
|
|
|
|
arg!(--color [color] "some other flag"),
|
2018-01-25 04:05:05 +00:00
|
|
|
])
|
2018-01-31 20:15:01 +00:00
|
|
|
.try_get_matches_from(vec!["", "--flag", "some", "--color", "other"]);
|
2016-01-26 07:05:32 +00:00
|
|
|
assert!(r.is_ok());
|
|
|
|
let m = r.unwrap();
|
2015-08-27 21:03:45 +00:00
|
|
|
assert!(m.is_present("flag"));
|
|
|
|
assert_eq!(m.value_of("flag").unwrap(), "some");
|
|
|
|
assert!(m.is_present("color"));
|
|
|
|
assert_eq!(m.value_of("color").unwrap(), "other");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn opts_using_long_equals() {
|
2016-01-26 07:05:32 +00:00
|
|
|
let r = App::new("opts")
|
2018-01-25 04:05:05 +00:00
|
|
|
.args(&[
|
2021-11-19 20:33:11 +00:00
|
|
|
arg!(--flag [flag] "some flag"),
|
|
|
|
arg!(--color [color] "some other flag"),
|
2018-01-25 04:05:05 +00:00
|
|
|
])
|
2018-01-31 20:15:01 +00:00
|
|
|
.try_get_matches_from(vec!["", "--flag=some", "--color=other"]);
|
2016-01-26 07:05:32 +00:00
|
|
|
assert!(r.is_ok());
|
|
|
|
let m = r.unwrap();
|
2015-08-27 21:03:45 +00:00
|
|
|
assert!(m.is_present("flag"));
|
|
|
|
assert_eq!(m.value_of("flag").unwrap(), "some");
|
|
|
|
assert!(m.is_present("color"));
|
|
|
|
assert_eq!(m.value_of("color").unwrap(), "other");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn opts_using_mixed() {
|
2016-01-26 07:05:32 +00:00
|
|
|
let r = App::new("opts")
|
2018-01-25 04:05:05 +00:00
|
|
|
.args(&[
|
2021-11-19 20:33:11 +00:00
|
|
|
arg!(-f --flag [flag] "some flag"),
|
|
|
|
arg!(-c --color [color] "some other flag"),
|
2018-01-25 04:05:05 +00:00
|
|
|
])
|
2018-01-31 20:15:01 +00:00
|
|
|
.try_get_matches_from(vec!["", "-f", "some", "--color", "other"]);
|
2016-01-26 07:05:32 +00:00
|
|
|
assert!(r.is_ok());
|
|
|
|
let m = r.unwrap();
|
2015-08-27 21:03:45 +00:00
|
|
|
assert!(m.is_present("flag"));
|
|
|
|
assert_eq!(m.value_of("flag").unwrap(), "some");
|
|
|
|
assert!(m.is_present("color"));
|
|
|
|
assert_eq!(m.value_of("color").unwrap(), "other");
|
2016-01-26 07:05:32 +00:00
|
|
|
}
|
2015-08-27 21:03:45 +00:00
|
|
|
|
2016-01-26 07:05:32 +00:00
|
|
|
#[test]
|
|
|
|
fn opts_using_mixed2() {
|
|
|
|
let r = App::new("opts")
|
2018-01-25 04:05:05 +00:00
|
|
|
.args(&[
|
2021-11-19 20:33:11 +00:00
|
|
|
arg!(-f --flag [flag] "some flag"),
|
|
|
|
arg!(-c --color [color] "some other flag"),
|
2018-01-25 04:05:05 +00:00
|
|
|
])
|
2018-01-31 20:15:01 +00:00
|
|
|
.try_get_matches_from(vec!["", "--flag=some", "-c", "other"]);
|
2016-01-26 07:05:32 +00:00
|
|
|
assert!(r.is_ok());
|
|
|
|
let m = r.unwrap();
|
2015-08-27 21:03:45 +00:00
|
|
|
assert!(m.is_present("flag"));
|
|
|
|
assert_eq!(m.value_of("flag").unwrap(), "some");
|
|
|
|
assert!(m.is_present("color"));
|
|
|
|
assert_eq!(m.value_of("color").unwrap(), "other");
|
2016-01-21 05:18:53 +00:00
|
|
|
}
|
2016-02-09 14:05:19 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn default_values_user_value() {
|
|
|
|
let r = App::new("df")
|
2021-11-19 20:33:11 +00:00
|
|
|
.arg(arg!(o: -o [opt] "some opt").default_value("default"))
|
2018-01-31 20:15:01 +00:00
|
|
|
.try_get_matches_from(vec!["", "-o", "value"]);
|
2016-02-09 14:05:19 +00:00
|
|
|
assert!(r.is_ok());
|
|
|
|
let m = r.unwrap();
|
|
|
|
assert!(m.is_present("o"));
|
|
|
|
assert_eq!(m.value_of("o").unwrap(), "value");
|
|
|
|
}
|
2016-05-09 03:20:50 +00:00
|
|
|
|
2016-06-29 17:30:30 +00:00
|
|
|
#[test]
|
|
|
|
fn multiple_vals_pos_arg_equals() {
|
|
|
|
let r = App::new("mvae")
|
2021-11-19 20:33:11 +00:00
|
|
|
.arg(arg!(o: -o [opt] ... "some opt"))
|
|
|
|
.arg(arg!([file] "some file"))
|
2018-01-31 20:15:01 +00:00
|
|
|
.try_get_matches_from(vec!["", "-o=1", "some"]);
|
2016-06-29 17:30:30 +00:00
|
|
|
assert!(r.is_ok());
|
|
|
|
let m = r.unwrap();
|
|
|
|
assert!(m.is_present("o"));
|
|
|
|
assert_eq!(m.value_of("o").unwrap(), "1");
|
|
|
|
assert!(m.is_present("file"));
|
|
|
|
assert_eq!(m.value_of("file").unwrap(), "some");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn multiple_vals_pos_arg_delim() {
|
|
|
|
let r = App::new("mvae")
|
2021-11-19 20:33:11 +00:00
|
|
|
.arg(
|
|
|
|
arg!(o: -o <opt> "some opt")
|
|
|
|
.multiple_values(true)
|
2021-11-29 14:56:30 +00:00
|
|
|
.use_delimiter(true),
|
2021-11-19 20:33:11 +00:00
|
|
|
)
|
|
|
|
.arg(arg!([file] "some file"))
|
2018-01-31 20:15:01 +00:00
|
|
|
.try_get_matches_from(vec!["", "-o", "1,2", "some"]);
|
2016-06-29 17:30:30 +00:00
|
|
|
assert!(r.is_ok());
|
|
|
|
let m = r.unwrap();
|
|
|
|
assert!(m.is_present("o"));
|
|
|
|
assert_eq!(m.values_of("o").unwrap().collect::<Vec<_>>(), &["1", "2"]);
|
|
|
|
assert!(m.is_present("file"));
|
|
|
|
assert_eq!(m.value_of("file").unwrap(), "some");
|
|
|
|
}
|
|
|
|
|
2016-06-30 03:01:41 +00:00
|
|
|
#[test]
|
|
|
|
fn require_delims_no_delim() {
|
|
|
|
let r = App::new("mvae")
|
2021-02-24 15:07:57 +00:00
|
|
|
.arg(
|
2021-11-19 20:33:11 +00:00
|
|
|
arg!(o: -o [opt] ... "some opt")
|
2021-11-29 14:56:30 +00:00
|
|
|
.use_delimiter(true)
|
|
|
|
.require_delimiter(true),
|
2021-02-24 15:07:57 +00:00
|
|
|
)
|
2021-11-19 20:33:11 +00:00
|
|
|
.arg(arg!([file] "some file"))
|
2018-01-31 20:15:01 +00:00
|
|
|
.try_get_matches_from(vec!["mvae", "-o", "1", "2", "some"]);
|
2016-06-30 03:01:41 +00:00
|
|
|
assert!(r.is_err());
|
|
|
|
let err = r.unwrap_err();
|
|
|
|
assert_eq!(err.kind, ErrorKind::UnknownArgument);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn require_delims() {
|
|
|
|
let r = App::new("mvae")
|
2021-02-24 15:07:57 +00:00
|
|
|
.arg(
|
2021-11-19 20:33:11 +00:00
|
|
|
arg!(o: -o <opt> "some opt")
|
|
|
|
.multiple_values(true)
|
2021-11-29 14:56:30 +00:00
|
|
|
.use_delimiter(true)
|
|
|
|
.require_delimiter(true),
|
2021-02-24 15:07:57 +00:00
|
|
|
)
|
2021-11-19 20:33:11 +00:00
|
|
|
.arg(arg!([file] "some file"))
|
2018-01-31 20:15:01 +00:00
|
|
|
.try_get_matches_from(vec!["", "-o", "1,2", "some"]);
|
2016-06-30 03:01:41 +00:00
|
|
|
assert!(r.is_ok());
|
|
|
|
let m = r.unwrap();
|
|
|
|
assert!(m.is_present("o"));
|
|
|
|
assert_eq!(m.values_of("o").unwrap().collect::<Vec<_>>(), &["1", "2"]);
|
|
|
|
assert!(m.is_present("file"));
|
|
|
|
assert_eq!(m.value_of("file").unwrap(), "some");
|
|
|
|
}
|
|
|
|
|
2016-11-20 01:40:53 +00:00
|
|
|
#[test]
|
|
|
|
fn leading_hyphen_pass() {
|
|
|
|
let r = App::new("mvae")
|
2021-11-19 20:33:11 +00:00
|
|
|
.arg(
|
|
|
|
arg!(o: -o <opt> "some opt")
|
|
|
|
.multiple_values(true)
|
2021-11-29 14:56:30 +00:00
|
|
|
.allow_hyphen_values(true),
|
2021-11-19 20:33:11 +00:00
|
|
|
)
|
2018-01-31 20:15:01 +00:00
|
|
|
.try_get_matches_from(vec!["", "-o", "-2", "3"]);
|
2016-11-20 01:40:53 +00:00
|
|
|
assert!(r.is_ok());
|
|
|
|
let m = r.unwrap();
|
|
|
|
assert!(m.is_present("o"));
|
|
|
|
assert_eq!(m.values_of("o").unwrap().collect::<Vec<_>>(), &["-2", "3"]);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn leading_hyphen_fail() {
|
|
|
|
let r = App::new("mvae")
|
2021-11-19 20:33:11 +00:00
|
|
|
.arg(arg!(o: -o <opt> "some opt"))
|
2018-01-31 20:15:01 +00:00
|
|
|
.try_get_matches_from(vec!["", "-o", "-2"]);
|
2016-11-20 01:40:53 +00:00
|
|
|
assert!(r.is_err());
|
|
|
|
let m = r.unwrap_err();
|
|
|
|
assert_eq!(m.kind, ErrorKind::UnknownArgument);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn leading_hyphen_with_flag_after() {
|
|
|
|
let r = App::new("mvae")
|
2021-11-19 20:33:11 +00:00
|
|
|
.arg(
|
|
|
|
arg!(o: -o <opt> "some opt")
|
|
|
|
.multiple_values(true)
|
2021-11-29 14:56:30 +00:00
|
|
|
.allow_hyphen_values(true),
|
2021-11-19 20:33:11 +00:00
|
|
|
)
|
|
|
|
.arg(arg!(f: -f "some flag"))
|
2018-01-31 20:15:01 +00:00
|
|
|
.try_get_matches_from(vec!["", "-o", "-2", "-f"]);
|
2016-11-20 01:40:53 +00:00
|
|
|
assert!(r.is_ok());
|
|
|
|
let m = r.unwrap();
|
|
|
|
assert!(m.is_present("o"));
|
|
|
|
assert_eq!(m.values_of("o").unwrap().collect::<Vec<_>>(), &["-2", "-f"]);
|
|
|
|
assert!(!m.is_present("f"));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn leading_hyphen_with_flag_before() {
|
|
|
|
let r = App::new("mvae")
|
2021-11-29 14:56:30 +00:00
|
|
|
.arg(arg!(o: -o [opt] ... "some opt").allow_hyphen_values(true))
|
2021-11-19 20:33:11 +00:00
|
|
|
.arg(arg!(f: -f "some flag"))
|
2018-01-31 20:15:01 +00:00
|
|
|
.try_get_matches_from(vec!["", "-f", "-o", "-2"]);
|
2016-11-20 01:40:53 +00:00
|
|
|
assert!(r.is_ok());
|
|
|
|
let m = r.unwrap();
|
|
|
|
assert!(m.is_present("o"));
|
|
|
|
assert_eq!(m.values_of("o").unwrap().collect::<Vec<_>>(), &["-2"]);
|
|
|
|
assert!(m.is_present("f"));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn leading_hyphen_with_only_pos_follows() {
|
|
|
|
let r = App::new("mvae")
|
2018-01-25 04:05:05 +00:00
|
|
|
.arg(
|
2021-11-19 20:33:11 +00:00
|
|
|
arg!(o: -o [opt] ... "some opt")
|
2018-01-25 04:05:05 +00:00
|
|
|
.number_of_values(1)
|
2021-11-29 14:56:30 +00:00
|
|
|
.takes_value(true)
|
|
|
|
.allow_hyphen_values(true),
|
2018-01-25 04:05:05 +00:00
|
|
|
)
|
2021-11-19 20:33:11 +00:00
|
|
|
.arg(arg!([arg] "some arg"))
|
2018-01-31 20:15:01 +00:00
|
|
|
.try_get_matches_from(vec!["", "-o", "-2", "--", "val"]);
|
2017-05-15 22:08:33 +00:00
|
|
|
assert!(r.is_ok(), "{:?}", r);
|
2016-11-20 01:40:53 +00:00
|
|
|
let m = r.unwrap();
|
|
|
|
assert!(m.is_present("o"));
|
|
|
|
assert_eq!(m.values_of("o").unwrap().collect::<Vec<_>>(), &["-2"]);
|
|
|
|
assert_eq!(m.value_of("arg"), Some("val"));
|
|
|
|
}
|
|
|
|
|
2016-05-09 03:20:50 +00:00
|
|
|
#[test]
|
2018-01-25 04:05:05 +00:00
|
|
|
#[cfg(feature = "suggestions")]
|
2016-05-09 03:20:50 +00:00
|
|
|
fn did_you_mean() {
|
2020-02-04 08:10:53 +00:00
|
|
|
assert!(utils::compare_output(
|
|
|
|
utils::complex_app(),
|
2018-01-25 04:05:05 +00:00
|
|
|
"clap-test --optio=foo",
|
|
|
|
DYM,
|
|
|
|
true
|
|
|
|
));
|
2016-05-09 03:20:50 +00:00
|
|
|
}
|
2016-12-30 21:25:48 +00:00
|
|
|
|
2017-09-14 17:18:45 +00:00
|
|
|
#[test]
|
|
|
|
fn issue_1047_min_zero_vals_default_val() {
|
|
|
|
let m = App::new("foo")
|
|
|
|
.arg(
|
2020-05-14 20:50:56 +00:00
|
|
|
Arg::new("del")
|
2018-07-23 19:09:42 +00:00
|
|
|
.short('d')
|
2017-09-14 17:18:45 +00:00
|
|
|
.long("del")
|
2021-11-29 14:56:30 +00:00
|
|
|
.takes_value(true)
|
|
|
|
.require_equals(true)
|
2017-09-14 17:18:45 +00:00
|
|
|
.min_values(0)
|
2020-10-13 21:21:01 +00:00
|
|
|
.default_missing_value("default"),
|
2017-09-14 17:18:45 +00:00
|
|
|
)
|
|
|
|
.get_matches_from(vec!["foo", "-d"]);
|
|
|
|
assert_eq!(m.occurrences_of("del"), 1);
|
|
|
|
assert_eq!(m.value_of("del"), Some("default"));
|
2017-11-13 22:05:28 +00:00
|
|
|
}
|
|
|
|
|
2019-04-05 02:06:23 +00:00
|
|
|
fn issue_1105_setup(argv: Vec<&'static str>) -> Result<ArgMatches, clap::Error> {
|
2017-11-13 22:05:28 +00:00
|
|
|
App::new("opts")
|
2021-11-19 20:33:11 +00:00
|
|
|
.arg(arg!(-o --option <opt> "some option"))
|
|
|
|
.arg(arg!(--flag "some flag"))
|
2018-01-31 20:15:01 +00:00
|
|
|
.try_get_matches_from(argv)
|
2017-11-13 22:05:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn issue_1105_empty_value_long_fail() {
|
2018-01-25 04:05:05 +00:00
|
|
|
let r = issue_1105_setup(vec!["app", "--option", "--flag"]);
|
2017-11-13 22:05:28 +00:00
|
|
|
assert!(r.is_err());
|
|
|
|
assert_eq!(r.unwrap_err().kind, ErrorKind::EmptyValue);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn issue_1105_empty_value_long_explicit() {
|
|
|
|
let r = issue_1105_setup(vec!["app", "--option", ""]);
|
|
|
|
assert!(r.is_ok());
|
|
|
|
let m = r.unwrap();
|
|
|
|
assert_eq!(m.value_of("option"), Some(""));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn issue_1105_empty_value_long_equals() {
|
2018-01-25 04:05:05 +00:00
|
|
|
let r = issue_1105_setup(vec!["app", "--option="]);
|
2017-11-13 22:05:28 +00:00
|
|
|
assert!(r.is_ok());
|
|
|
|
let m = r.unwrap();
|
|
|
|
assert_eq!(m.value_of("option"), Some(""));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn issue_1105_empty_value_short_fail() {
|
|
|
|
let r = issue_1105_setup(vec!["app", "-o", "--flag"]);
|
|
|
|
assert!(r.is_err());
|
|
|
|
assert_eq!(r.unwrap_err().kind, ErrorKind::EmptyValue);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn issue_1105_empty_value_short_explicit() {
|
|
|
|
let r = issue_1105_setup(vec!["app", "-o", ""]);
|
|
|
|
assert!(r.is_ok());
|
|
|
|
let m = r.unwrap();
|
|
|
|
assert_eq!(m.value_of("option"), Some(""));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn issue_1105_empty_value_short_equals() {
|
|
|
|
let r = issue_1105_setup(vec!["app", "-o="]);
|
|
|
|
assert!(r.is_ok());
|
|
|
|
let m = r.unwrap();
|
|
|
|
assert_eq!(m.value_of("option"), Some(""));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn issue_1105_empty_value_short_explicit_no_space() {
|
|
|
|
let r = issue_1105_setup(vec!["app", "-o", ""]);
|
|
|
|
assert!(r.is_ok());
|
|
|
|
let m = r.unwrap();
|
|
|
|
assert_eq!(m.value_of("option"), Some(""));
|
|
|
|
}
|
2020-04-12 15:44:48 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[cfg(feature = "suggestions")]
|
|
|
|
fn issue_1073_suboptimal_flag_suggestion() {
|
|
|
|
let app = App::new("ripgrep-616")
|
2020-05-14 20:50:56 +00:00
|
|
|
.arg(Arg::new("files-with-matches").long("files-with-matches"))
|
|
|
|
.arg(Arg::new("files-without-match").long("files-without-match"));
|
2020-04-12 15:44:48 +00:00
|
|
|
assert!(utils::compare_output(
|
|
|
|
app,
|
|
|
|
"ripgrep-616 --files-without-matches",
|
|
|
|
DYM_ISSUE_1073,
|
|
|
|
true
|
|
|
|
));
|
|
|
|
}
|
2020-04-20 15:29:29 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn short_non_ascii_no_space() {
|
|
|
|
let matches = App::new("app")
|
2021-11-19 20:33:11 +00:00
|
|
|
.arg(arg!(opt: -'磨' <opt>))
|
2020-04-20 15:29:29 +00:00
|
|
|
.get_matches_from(&["test", "-磨VALUE"]);
|
|
|
|
|
|
|
|
assert_eq!("VALUE", matches.value_of("opt").unwrap());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn short_eq_val_starts_with_eq() {
|
|
|
|
let matches = App::new("app")
|
2021-11-19 20:33:11 +00:00
|
|
|
.arg(arg!(opt: -f <opt>))
|
2020-04-20 15:29:29 +00:00
|
|
|
.get_matches_from(&["test", "-f==value"]);
|
|
|
|
|
|
|
|
assert_eq!("=value", matches.value_of("opt").unwrap());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn long_eq_val_starts_with_eq() {
|
|
|
|
let matches = App::new("app")
|
2021-11-19 20:33:11 +00:00
|
|
|
.arg(arg!(opt: --foo <opt>))
|
2020-04-20 15:29:29 +00:00
|
|
|
.get_matches_from(&["test", "--foo==value"]);
|
|
|
|
|
|
|
|
assert_eq!("=value", matches.value_of("opt").unwrap());
|
|
|
|
}
|
2020-10-09 18:43:46 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn issue_2022_get_flags_misuse() {
|
|
|
|
let app = App::new("test")
|
2021-10-14 16:34:48 +00:00
|
|
|
.help_heading(Some("test"))
|
2020-10-09 18:43:46 +00:00
|
|
|
.arg(Arg::new("a").long("a").default_value("32"));
|
|
|
|
let matches = app.get_matches_from(&[""]);
|
|
|
|
assert!(matches.value_of("a").is_some())
|
|
|
|
}
|
2021-01-20 17:09:20 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn issue_2279() {
|
|
|
|
let before_help_heading = App::new("app")
|
|
|
|
.arg(Arg::new("foo").short('f').default_value("bar"))
|
2021-10-14 16:34:48 +00:00
|
|
|
.help_heading(Some("This causes default_value to be ignored"))
|
2021-01-20 17:09:20 +00:00
|
|
|
.get_matches_from(&[""]);
|
|
|
|
|
|
|
|
assert_eq!(before_help_heading.value_of("foo"), Some("bar"));
|
|
|
|
|
|
|
|
let after_help_heading = App::new("app")
|
2021-10-14 16:34:48 +00:00
|
|
|
.help_heading(Some("This causes default_value to be ignored"))
|
2021-01-20 17:09:20 +00:00
|
|
|
.arg(Arg::new("foo").short('f').default_value("bar"))
|
|
|
|
.get_matches_from(&[""]);
|
|
|
|
|
|
|
|
assert_eq!(after_help_heading.value_of("foo"), Some("bar"));
|
|
|
|
}
|
2021-06-05 11:18:06 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn infer_long_arg() {
|
|
|
|
let app = App::new("test")
|
|
|
|
.setting(AppSettings::InferLongArgs)
|
|
|
|
.arg(Arg::new("racetrack").long("racetrack").alias("autobahn"))
|
|
|
|
.arg(Arg::new("racecar").long("racecar").takes_value(true));
|
|
|
|
|
|
|
|
let matches = app.clone().get_matches_from(&["test", "--racec=hello"]);
|
|
|
|
assert!(!matches.is_present("racetrack"));
|
|
|
|
assert_eq!(matches.value_of("racecar"), Some("hello"));
|
|
|
|
|
|
|
|
let matches = app.clone().get_matches_from(&["test", "--racet"]);
|
|
|
|
assert!(matches.is_present("racetrack"));
|
|
|
|
assert_eq!(matches.value_of("racecar"), None);
|
|
|
|
|
|
|
|
let matches = app.clone().get_matches_from(&["test", "--auto"]);
|
|
|
|
assert!(matches.is_present("racetrack"));
|
|
|
|
assert_eq!(matches.value_of("racecar"), None);
|
|
|
|
|
|
|
|
let app = App::new("test")
|
|
|
|
.setting(AppSettings::InferLongArgs)
|
|
|
|
.arg(Arg::new("arg").long("arg"));
|
|
|
|
|
|
|
|
let matches = app.clone().get_matches_from(&["test", "--"]);
|
|
|
|
assert!(!matches.is_present("arg"));
|
|
|
|
|
|
|
|
let matches = app.clone().get_matches_from(&["test", "--a"]);
|
|
|
|
assert!(matches.is_present("arg"));
|
|
|
|
}
|