2021-11-30 15:31:19 +00:00
|
|
|
use crate::utils;
|
2022-02-12 03:48:29 +00:00
|
|
|
use clap::{arg, Arg, Command};
|
2015-08-27 21:03:45 +00:00
|
|
|
|
2020-10-06 11:47:50 +00:00
|
|
|
const USE_FLAG_AS_ARGUMENT: &str =
|
|
|
|
"error: Found argument '--another-flag' which wasn't expected, or isn't valid in this context
|
|
|
|
|
2020-10-09 16:31:00 +00:00
|
|
|
\tIf you tried to supply `--another-flag` as a value rather than a flag, use `-- --another-flag`
|
2020-10-06 11:47:50 +00:00
|
|
|
|
|
|
|
USAGE:
|
2021-10-11 22:01:33 +00:00
|
|
|
mycat [OPTIONS] [filename]
|
2020-10-06 11:47:50 +00:00
|
|
|
|
2021-09-24 15:58:39 +00:00
|
|
|
For more information try --help
|
|
|
|
";
|
2020-10-06 11:47:50 +00:00
|
|
|
|
2015-08-27 21:03:45 +00:00
|
|
|
#[test]
|
|
|
|
fn flag_using_short() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let m = Command::new("flag")
|
2016-01-21 06:48:30 +00:00
|
|
|
.args(&[
|
2021-11-19 20:33:11 +00:00
|
|
|
arg!(-f --flag "some flag"),
|
|
|
|
arg!(-c --color "some other flag"),
|
2018-01-25 04:05:05 +00:00
|
|
|
])
|
2021-12-27 18:56:12 +00:00
|
|
|
.try_get_matches_from(vec!["", "-f", "-c"])
|
|
|
|
.unwrap();
|
2015-08-27 21:03:45 +00:00
|
|
|
assert!(m.is_present("flag"));
|
|
|
|
assert!(m.is_present("color"));
|
|
|
|
}
|
|
|
|
|
2016-02-02 12:43:53 +00:00
|
|
|
#[test]
|
|
|
|
fn lots_o_flags_sep() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let r = Command::new("opts")
|
2021-11-19 20:33:11 +00:00
|
|
|
.arg(arg!(o: -o ... "some flag"))
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec![
|
2018-01-25 04:05:05 +00:00
|
|
|
"", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o",
|
|
|
|
"-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o",
|
|
|
|
"-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o",
|
|
|
|
"-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o",
|
|
|
|
"-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o",
|
|
|
|
"-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o",
|
|
|
|
"-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o",
|
|
|
|
"-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o",
|
|
|
|
"-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o",
|
|
|
|
"-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o",
|
|
|
|
"-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o",
|
|
|
|
"-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o",
|
|
|
|
"-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o",
|
|
|
|
"-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o",
|
|
|
|
"-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o",
|
|
|
|
"-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o",
|
|
|
|
"-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o",
|
|
|
|
"-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o",
|
|
|
|
"-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o",
|
|
|
|
"-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o",
|
|
|
|
"-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o",
|
|
|
|
"-o", "-o", "-o",
|
|
|
|
]);
|
2022-01-25 22:19:28 +00:00
|
|
|
assert!(r.is_ok(), "{:?}", r.unwrap_err().kind());
|
2016-02-02 12:43:53 +00:00
|
|
|
let m = r.unwrap();
|
|
|
|
assert!(m.is_present("o"));
|
|
|
|
assert_eq!(m.occurrences_of("o"), 297); // i.e. more than u8
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn lots_o_flags_combined() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let r = Command::new("opts")
|
2021-11-19 20:33:11 +00:00
|
|
|
.arg(arg!(o: -o ... "some flag"))
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec![
|
2018-01-25 04:05:05 +00:00
|
|
|
"",
|
2016-02-02 12:43:53 +00:00
|
|
|
"-oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo",
|
|
|
|
"-oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo",
|
|
|
|
"-oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo",
|
|
|
|
"-oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo",
|
|
|
|
"-ooooooooooooooooooooooooooooooooooooooooo",
|
2018-01-25 04:05:05 +00:00
|
|
|
]);
|
2022-01-25 22:19:28 +00:00
|
|
|
assert!(r.is_ok(), "{:?}", r.unwrap_err().kind());
|
2016-02-02 12:43:53 +00:00
|
|
|
let m = r.unwrap();
|
|
|
|
assert!(m.is_present("o"));
|
|
|
|
assert_eq!(m.occurrences_of("o"), 297); // i.e. more than u8
|
|
|
|
}
|
|
|
|
|
2015-08-27 21:03:45 +00:00
|
|
|
#[test]
|
|
|
|
fn flag_using_long() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let m = Command::new("flag")
|
2021-11-19 20:33:11 +00:00
|
|
|
.args(&[arg!(--flag "some flag"), arg!(--color "some other flag")])
|
2021-12-27 18:56:12 +00:00
|
|
|
.try_get_matches_from(vec!["", "--flag", "--color"])
|
|
|
|
.unwrap();
|
2015-08-27 21:03:45 +00:00
|
|
|
assert!(m.is_present("flag"));
|
|
|
|
assert!(m.is_present("color"));
|
|
|
|
}
|
|
|
|
|
2021-07-24 20:58:42 +00:00
|
|
|
#[test]
|
|
|
|
fn flag_using_long_with_literals() {
|
2022-02-02 21:41:24 +00:00
|
|
|
use clap::error::ErrorKind;
|
2021-07-28 12:30:10 +00:00
|
|
|
|
2022-02-12 03:48:29 +00:00
|
|
|
let m = Command::new("flag")
|
2021-08-01 10:19:00 +00:00
|
|
|
.arg(Arg::new("rainbow").long("rainbow"))
|
2021-08-01 09:50:41 +00:00
|
|
|
.try_get_matches_from(vec!["", "--rainbow=false"]);
|
2021-07-28 12:30:10 +00:00
|
|
|
assert!(m.is_err(), "{:#?}", m.unwrap());
|
2022-01-25 22:19:28 +00:00
|
|
|
assert_eq!(m.unwrap_err().kind(), ErrorKind::TooManyValues);
|
2021-07-24 20:58:42 +00:00
|
|
|
}
|
|
|
|
|
2015-08-27 21:03:45 +00:00
|
|
|
#[test]
|
|
|
|
fn flag_using_mixed() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let m = Command::new("flag")
|
2016-01-21 06:48:30 +00:00
|
|
|
.args(&[
|
2021-11-19 20:33:11 +00:00
|
|
|
arg!(-f --flag "some flag"),
|
|
|
|
arg!(-c --color "some other flag"),
|
2018-01-25 04:05:05 +00:00
|
|
|
])
|
2021-12-27 18:56:12 +00:00
|
|
|
.try_get_matches_from(vec!["", "-f", "--color"])
|
|
|
|
.unwrap();
|
2015-08-27 21:03:45 +00:00
|
|
|
assert!(m.is_present("flag"));
|
|
|
|
assert!(m.is_present("color"));
|
|
|
|
|
2022-02-12 03:48:29 +00:00
|
|
|
let m = Command::new("flag")
|
2016-01-21 06:48:30 +00:00
|
|
|
.args(&[
|
2021-11-19 20:33:11 +00:00
|
|
|
arg!(-f --flag "some flag"),
|
|
|
|
arg!(-c --color "some other flag"),
|
2018-01-25 04:05:05 +00:00
|
|
|
])
|
2021-12-27 18:56:12 +00:00
|
|
|
.try_get_matches_from(vec!["", "--flag", "-c"])
|
|
|
|
.unwrap();
|
2015-08-27 21:03:45 +00:00
|
|
|
assert!(m.is_present("flag"));
|
|
|
|
assert!(m.is_present("color"));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn multiple_flags_in_single() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let m = Command::new("multe_flags")
|
2016-01-21 06:48:30 +00:00
|
|
|
.args(&[
|
2021-11-19 20:33:11 +00:00
|
|
|
arg!(-f --flag "some flag"),
|
|
|
|
arg!(-c --color "some other flag"),
|
|
|
|
arg!(-d --debug "another other flag"),
|
2018-01-25 04:05:05 +00:00
|
|
|
])
|
2021-12-27 18:56:12 +00:00
|
|
|
.try_get_matches_from(vec!["", "-fcd"])
|
|
|
|
.unwrap();
|
2015-08-27 21:03:45 +00:00
|
|
|
assert!(m.is_present("flag"));
|
|
|
|
assert!(m.is_present("color"));
|
|
|
|
assert!(m.is_present("debug"));
|
|
|
|
}
|
2020-10-06 11:47:50 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn issue_1284_argument_in_flag_style() {
|
2022-02-14 21:47:20 +00:00
|
|
|
let cmd = Command::new("mycat")
|
2020-10-06 11:47:50 +00:00
|
|
|
.arg(Arg::new("filename"))
|
|
|
|
.arg(Arg::new("a-flag").long("a-flag"));
|
|
|
|
|
2022-02-14 21:47:20 +00:00
|
|
|
let m = cmd
|
2020-10-06 11:47:50 +00:00
|
|
|
.clone()
|
2021-12-27 18:56:12 +00:00
|
|
|
.try_get_matches_from(vec!["", "--", "--another-flag"])
|
|
|
|
.unwrap();
|
2022-05-24 15:16:50 +00:00
|
|
|
assert_eq!(
|
|
|
|
m.get_one::<String>("filename").map(|v| v.as_str()),
|
|
|
|
Some("--another-flag")
|
|
|
|
);
|
2020-10-06 11:47:50 +00:00
|
|
|
|
2022-02-14 21:47:20 +00:00
|
|
|
let m = cmd
|
2021-12-27 18:56:12 +00:00
|
|
|
.clone()
|
|
|
|
.try_get_matches_from(vec!["", "--a-flag"])
|
|
|
|
.unwrap();
|
2020-10-06 11:47:50 +00:00
|
|
|
assert!(m.is_present("a-flag"));
|
|
|
|
|
2022-02-14 21:47:20 +00:00
|
|
|
let m = cmd
|
2021-12-27 18:56:12 +00:00
|
|
|
.clone()
|
|
|
|
.try_get_matches_from(vec!["", "--", "--a-flag"])
|
|
|
|
.unwrap();
|
2022-05-24 15:16:50 +00:00
|
|
|
assert_eq!(
|
|
|
|
m.get_one::<String>("filename").map(|v| v.as_str()),
|
|
|
|
Some("--a-flag")
|
|
|
|
);
|
2020-10-06 11:47:50 +00:00
|
|
|
|
2022-04-29 20:32:25 +00:00
|
|
|
utils::assert_output(cmd, "mycat --another-flag", USE_FLAG_AS_ARGUMENT, true);
|
2020-10-06 11:47:50 +00:00
|
|
|
}
|
2021-01-23 07:18:55 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn issue_2308_multiple_dashes() {
|
|
|
|
static MULTIPLE_DASHES: &str =
|
|
|
|
"error: Found argument '-----' which wasn't expected, or isn't valid in this context
|
|
|
|
|
|
|
|
If you tried to supply `-----` as a value rather than a flag, use `-- -----`
|
|
|
|
|
|
|
|
USAGE:
|
|
|
|
test <arg>
|
|
|
|
|
2021-09-24 15:58:39 +00:00
|
|
|
For more information try --help
|
|
|
|
";
|
2022-02-14 21:47:20 +00:00
|
|
|
let cmd = Command::new("test").arg(Arg::new("arg").takes_value(true).required(true));
|
2021-01-23 07:18:55 +00:00
|
|
|
|
2022-04-29 20:32:25 +00:00
|
|
|
utils::assert_output(cmd, "test -----", MULTIPLE_DASHES, true);
|
2021-01-23 07:18:55 +00:00
|
|
|
}
|
2022-05-04 20:38:06 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[cfg(not(feature = "unstable-v4"))]
|
|
|
|
fn leading_dash_stripped() {
|
|
|
|
let cmd = Command::new("mycat").arg(Arg::new("filename").long("--filename"));
|
|
|
|
let matches = cmd.try_get_matches_from(["mycat", "--filename"]).unwrap();
|
|
|
|
assert!(matches.is_present("filename"));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[cfg(feature = "unstable-v4")]
|
|
|
|
#[cfg(debug_assertions)]
|
|
|
|
#[should_panic = "Argument filename: long \"--filename\" must not start with a `-`, that will be handled by the parser"]
|
|
|
|
fn leading_dash_stripped() {
|
|
|
|
let cmd = Command::new("mycat").arg(Arg::new("filename").long("--filename"));
|
|
|
|
cmd.debug_assert();
|
|
|
|
}
|