2022-06-10 01:03:28 +00:00
|
|
|
use clap::{arg, Arg, ArgAction, Command};
|
2015-08-27 21:03:45 +00:00
|
|
|
|
2022-09-19 14:59:04 +00:00
|
|
|
#[cfg(feature = "error-context")]
|
|
|
|
use super::utils;
|
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")
|
2022-11-24 13:54:25 +00:00
|
|
|
.args([
|
2022-06-10 01:03:28 +00:00
|
|
|
arg!(-f --flag "some flag").action(ArgAction::SetTrue),
|
|
|
|
arg!(-c --color "some other flag").action(ArgAction::SetTrue),
|
2018-01-25 04:05:05 +00:00
|
|
|
])
|
2021-12-27 18:56:12 +00:00
|
|
|
.try_get_matches_from(vec!["", "-f", "-c"])
|
|
|
|
.unwrap();
|
2022-06-10 01:03:28 +00:00
|
|
|
assert!(*m.get_one::<bool>("flag").expect("defaulted by clap"));
|
|
|
|
assert!(*m.get_one::<bool>("color").expect("defaulted by clap"));
|
2015-08-27 21:03:45 +00:00
|
|
|
}
|
|
|
|
|
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")
|
2022-09-23 20:18:14 +00:00
|
|
|
.args_override_self(true)
|
2022-06-10 01:03:28 +00:00
|
|
|
.arg(arg!(o: -o ... "some flag").action(ArgAction::SetTrue))
|
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();
|
2022-06-10 01:03:28 +00:00
|
|
|
assert!(m.contains_id("o"));
|
|
|
|
assert!(*m.get_one::<bool>("o").expect("defaulted by clap"));
|
2016-02-02 12:43:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn lots_o_flags_combined() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let r = Command::new("opts")
|
2022-09-23 20:18:14 +00:00
|
|
|
.args_override_self(true)
|
2022-06-10 01:03:28 +00:00
|
|
|
.arg(arg!(o: -o ... "some flag").action(ArgAction::SetTrue))
|
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();
|
2022-06-10 01:03:28 +00:00
|
|
|
assert!(m.contains_id("o"));
|
|
|
|
assert!(*m.get_one::<bool>("o").expect("defaulted by clap"));
|
2016-02-02 12:43:53 +00:00
|
|
|
}
|
|
|
|
|
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")
|
2022-11-24 13:54:25 +00:00
|
|
|
.args([
|
2022-06-10 01:03:28 +00:00
|
|
|
arg!(--flag "some flag").action(ArgAction::SetTrue),
|
|
|
|
arg!(--color "some other flag").action(ArgAction::SetTrue),
|
|
|
|
])
|
2021-12-27 18:56:12 +00:00
|
|
|
.try_get_matches_from(vec!["", "--flag", "--color"])
|
|
|
|
.unwrap();
|
2022-06-10 01:03:28 +00:00
|
|
|
assert!(*m.get_one::<bool>("flag").expect("defaulted by clap"));
|
|
|
|
assert!(*m.get_one::<bool>("color").expect("defaulted by clap"));
|
2015-08-27 21:03:45 +00:00
|
|
|
}
|
|
|
|
|
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")
|
2022-07-26 00:17:01 +00:00
|
|
|
.arg(
|
|
|
|
Arg::new("rainbow")
|
|
|
|
.long("rainbow")
|
|
|
|
.action(ArgAction::SetTrue),
|
|
|
|
)
|
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")
|
2022-11-24 13:54:25 +00:00
|
|
|
.args([
|
2022-06-10 01:03:28 +00:00
|
|
|
arg!(-f --flag "some flag").action(ArgAction::SetTrue),
|
|
|
|
arg!(-c --color "some other flag").action(ArgAction::SetTrue),
|
2018-01-25 04:05:05 +00:00
|
|
|
])
|
2021-12-27 18:56:12 +00:00
|
|
|
.try_get_matches_from(vec!["", "-f", "--color"])
|
|
|
|
.unwrap();
|
2022-06-10 01:03:28 +00:00
|
|
|
assert!(*m.get_one::<bool>("flag").expect("defaulted by clap"));
|
|
|
|
assert!(*m.get_one::<bool>("color").expect("defaulted by clap"));
|
2015-08-27 21:03:45 +00:00
|
|
|
|
2022-02-12 03:48:29 +00:00
|
|
|
let m = Command::new("flag")
|
2022-11-24 13:54:25 +00:00
|
|
|
.args([
|
2022-06-10 01:03:28 +00:00
|
|
|
arg!(-f --flag "some flag").action(ArgAction::SetTrue),
|
|
|
|
arg!(-c --color "some other flag").action(ArgAction::SetTrue),
|
2018-01-25 04:05:05 +00:00
|
|
|
])
|
2021-12-27 18:56:12 +00:00
|
|
|
.try_get_matches_from(vec!["", "--flag", "-c"])
|
|
|
|
.unwrap();
|
2022-06-10 01:03:28 +00:00
|
|
|
assert!(*m.get_one::<bool>("flag").expect("defaulted by clap"));
|
|
|
|
assert!(*m.get_one::<bool>("color").expect("defaulted by clap"));
|
2015-08-27 21:03:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn multiple_flags_in_single() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let m = Command::new("multe_flags")
|
2022-11-24 13:54:25 +00:00
|
|
|
.args([
|
2022-06-10 01:03:28 +00:00
|
|
|
arg!(-f --flag "some flag").action(ArgAction::SetTrue),
|
|
|
|
arg!(-c --color "some other flag").action(ArgAction::SetTrue),
|
|
|
|
arg!(-d --debug "another other flag").action(ArgAction::SetTrue),
|
2018-01-25 04:05:05 +00:00
|
|
|
])
|
2021-12-27 18:56:12 +00:00
|
|
|
.try_get_matches_from(vec!["", "-fcd"])
|
|
|
|
.unwrap();
|
2022-06-10 01:03:28 +00:00
|
|
|
assert!(*m.get_one::<bool>("flag").expect("defaulted by clap"));
|
|
|
|
assert!(*m.get_one::<bool>("color").expect("defaulted by clap"));
|
|
|
|
assert!(*m.get_one::<bool>("debug").expect("defaulted by clap"));
|
2015-08-27 21:03:45 +00:00
|
|
|
}
|
2022-09-27 14:19:27 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[cfg(feature = "error-context")]
|
|
|
|
fn unexpected_value_error() {
|
|
|
|
const USE_FLAG_AS_ARGUMENT: &str = "\
|
2023-01-14 03:29:49 +00:00
|
|
|
error: unexpected value 'foo' for '--a-flag' found; no more were expected
|
2022-09-27 14:19:27 +00:00
|
|
|
|
2022-09-27 14:25:24 +00:00
|
|
|
Usage: mycat --a-flag [filename]
|
2022-09-27 14:19:27 +00:00
|
|
|
|
2023-01-03 19:22:35 +00:00
|
|
|
For more information, try '--help'.
|
2022-09-27 14:19:27 +00:00
|
|
|
";
|
|
|
|
|
|
|
|
let cmd = Command::new("mycat")
|
|
|
|
.arg(Arg::new("filename"))
|
|
|
|
.arg(Arg::new("a-flag").long("a-flag").action(ArgAction::SetTrue));
|
|
|
|
|
|
|
|
utils::assert_output(cmd, "mycat --a-flag=foo", USE_FLAG_AS_ARGUMENT, true);
|
|
|
|
}
|
2020-10-06 11:47:50 +00:00
|
|
|
|
|
|
|
#[test]
|
2022-09-19 14:59:04 +00:00
|
|
|
#[cfg(feature = "error-context")]
|
2020-10-06 11:47:50 +00:00
|
|
|
fn issue_1284_argument_in_flag_style() {
|
2022-09-19 14:59:04 +00:00
|
|
|
const USE_FLAG_AS_ARGUMENT: &str = "\
|
2023-01-14 03:29:49 +00:00
|
|
|
error: unexpected argument '--another-flag' found
|
2022-09-19 14:59:04 +00:00
|
|
|
|
2023-03-28 01:18:18 +00:00
|
|
|
tip: to pass '--another-flag' as a value, use '-- --another-flag'
|
2022-09-19 14:59:04 +00:00
|
|
|
|
|
|
|
Usage: mycat [OPTIONS] [filename]
|
|
|
|
|
2023-01-03 19:22:35 +00:00
|
|
|
For more information, try '--help'.
|
2022-09-19 14:59:04 +00:00
|
|
|
";
|
|
|
|
|
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"))
|
2022-06-10 01:03:28 +00:00
|
|
|
.arg(Arg::new("a-flag").long("a-flag").action(ArgAction::SetTrue));
|
2020-10-06 11:47:50 +00:00
|
|
|
|
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();
|
2022-06-10 01:03:28 +00:00
|
|
|
assert!(*m.get_one::<bool>("a-flag").expect("defaulted by clap"));
|
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();
|
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]
|
2022-09-19 14:59:04 +00:00
|
|
|
#[cfg(feature = "error-context")]
|
2021-01-23 07:18:55 +00:00
|
|
|
fn issue_2308_multiple_dashes() {
|
2022-09-07 20:29:15 +00:00
|
|
|
static MULTIPLE_DASHES: &str = "\
|
2023-01-14 03:29:49 +00:00
|
|
|
error: unexpected argument '-----' found
|
2021-01-23 07:18:55 +00:00
|
|
|
|
2023-03-28 01:18:18 +00:00
|
|
|
tip: to pass '-----' as a value, use '-- -----'
|
2021-01-23 07:18:55 +00:00
|
|
|
|
2022-09-07 16:03:55 +00:00
|
|
|
Usage: test <arg>
|
2021-01-23 07:18:55 +00:00
|
|
|
|
2023-01-03 19:22:35 +00:00
|
|
|
For more information, try '--help'.
|
2021-09-24 15:58:39 +00:00
|
|
|
";
|
2022-07-26 00:17:01 +00:00
|
|
|
let cmd = Command::new("test").arg(Arg::new("arg").action(ArgAction::Set).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(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();
|
|
|
|
}
|