2022-06-10 01:03:28 +00:00
|
|
|
use clap::{arg, error::ErrorKind, Arg, ArgAction, Command};
|
2024-05-24 15:13:50 +00:00
|
|
|
use snapbox::assert_data_eq;
|
|
|
|
use snapbox::str;
|
2015-09-17 19:20:56 +00:00
|
|
|
|
2016-02-04 06:34:46 +00:00
|
|
|
#[test]
|
|
|
|
fn only_pos_follow() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let r = Command::new("onlypos")
|
2022-11-24 13:54:25 +00:00
|
|
|
.args([arg!(f: -f [flag] "some opt"), arg!([arg] "some arg")])
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec!["", "--", "-f"]);
|
2021-12-27 19:57:38 +00:00
|
|
|
assert!(r.is_ok(), "{}", r.unwrap_err());
|
2016-02-04 06:34:46 +00:00
|
|
|
let m = r.unwrap();
|
2022-06-10 01:03:28 +00:00
|
|
|
assert!(m.contains_id("arg"));
|
|
|
|
assert!(!m.contains_id("f"));
|
2022-05-24 15:16:50 +00:00
|
|
|
assert_eq!(
|
|
|
|
m.get_one::<String>("arg").map(|v| v.as_str()).unwrap(),
|
|
|
|
"-f"
|
|
|
|
);
|
2016-02-04 06:34:46 +00:00
|
|
|
}
|
|
|
|
|
2017-05-06 23:11:01 +00:00
|
|
|
#[test]
|
|
|
|
fn issue_946() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let r = Command::new("compiletest")
|
2022-06-10 01:03:28 +00:00
|
|
|
.arg(arg!(--exact "filters match exactly").action(ArgAction::SetTrue))
|
2018-01-25 04:05:05 +00:00
|
|
|
.arg(
|
2024-05-04 19:59:40 +00:00
|
|
|
Arg::new("filter")
|
2018-01-25 04:05:05 +00:00
|
|
|
.index(1)
|
2022-07-26 00:17:01 +00:00
|
|
|
.action(ArgAction::Set)
|
2022-09-06 22:12:17 +00:00
|
|
|
.allow_hyphen_values(true)
|
2021-11-18 16:17:15 +00:00
|
|
|
.help("filters to apply to output"),
|
2018-01-25 04:05:05 +00:00
|
|
|
)
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec!["compiletest", "--exact"]);
|
2023-05-04 01:40:21 +00:00
|
|
|
assert!(r.is_ok(), "{r:#?}");
|
2017-05-06 23:11:01 +00:00
|
|
|
let matches = r.unwrap();
|
|
|
|
|
2022-06-10 01:03:28 +00:00
|
|
|
assert!(*matches.get_one::<bool>("exact").expect("defaulted by clap"));
|
2022-05-24 15:16:50 +00:00
|
|
|
assert!(matches
|
|
|
|
.get_one::<String>("filter")
|
|
|
|
.map(|v| v.as_str())
|
|
|
|
.is_none());
|
2017-05-06 23:11:01 +00:00
|
|
|
}
|
|
|
|
|
2015-09-17 19:20:56 +00:00
|
|
|
#[test]
|
|
|
|
fn positional() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let r = Command::new("positional")
|
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::new("positional").index(1),
|
|
|
|
])
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec!["", "-f", "test"]);
|
2023-05-04 01:40:21 +00:00
|
|
|
assert!(r.is_ok(), "{r:#?}");
|
2017-05-06 23:11:01 +00:00
|
|
|
let m = r.unwrap();
|
2022-06-10 01:03:28 +00:00
|
|
|
assert!(m.contains_id("positional"));
|
|
|
|
assert!(*m.get_one::<bool>("flag").expect("defaulted by clap"));
|
2022-05-24 15:16:50 +00:00
|
|
|
assert_eq!(
|
|
|
|
m.get_one::<String>("positional")
|
|
|
|
.map(|v| v.as_str())
|
|
|
|
.unwrap(),
|
|
|
|
"test"
|
|
|
|
);
|
2015-09-17 19:20:56 +00:00
|
|
|
|
2022-02-12 03:48:29 +00:00
|
|
|
let m = Command::new("positional")
|
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::new("positional").index(1),
|
|
|
|
])
|
2021-12-27 18:56:12 +00:00
|
|
|
.try_get_matches_from(vec!["", "test", "--flag"])
|
|
|
|
.unwrap();
|
2022-06-10 01:03:28 +00:00
|
|
|
assert!(m.contains_id("positional"));
|
|
|
|
assert!(*m.get_one::<bool>("flag").expect("defaulted by clap"));
|
2022-05-24 15:16:50 +00:00
|
|
|
assert_eq!(
|
|
|
|
m.get_one::<String>("positional")
|
|
|
|
.map(|v| v.as_str())
|
|
|
|
.unwrap(),
|
|
|
|
"test"
|
|
|
|
);
|
2015-09-17 19:20:56 +00:00
|
|
|
}
|
|
|
|
|
2016-02-02 12:43:53 +00:00
|
|
|
#[test]
|
|
|
|
fn lots_o_vals() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let r = Command::new("opts")
|
2021-11-19 20:33:11 +00:00
|
|
|
.arg(arg!(<opt>... "some pos"))
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec![
|
2018-01-25 04:05:05 +00:00
|
|
|
"", "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
|
2016-02-02 12:43:53 +00:00
|
|
|
"some", "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
|
|
|
|
"some", "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
|
|
|
|
"some", "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
|
|
|
|
"some", "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
|
|
|
|
"some", "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
|
|
|
|
"some", "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
|
|
|
|
"some", "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
|
|
|
|
"some", "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
|
|
|
|
"some", "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
|
|
|
|
"some", "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
|
|
|
|
"some", "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
|
|
|
|
"some", "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
|
|
|
|
"some", "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
|
|
|
|
"some", "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
|
|
|
|
"some", "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
|
|
|
|
"some", "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
|
|
|
|
"some", "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
|
|
|
|
"some", "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
|
|
|
|
"some", "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
|
|
|
|
"some", "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
|
|
|
|
"some", "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
|
|
|
|
"some", "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
|
|
|
|
"some", "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
|
|
|
|
"some", "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
|
|
|
|
"some", "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
|
|
|
|
"some", "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
|
2018-01-25 04:05:05 +00:00
|
|
|
"some",
|
|
|
|
]);
|
2021-12-27 19:57:38 +00:00
|
|
|
assert!(r.is_ok(), "{}", r.unwrap_err());
|
2016-02-02 12:43: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_many::<String>("opt").unwrap().count(), 297); // i.e. more than u8
|
2016-02-02 12:43:53 +00:00
|
|
|
}
|
|
|
|
|
2015-09-17 19:20:56 +00:00
|
|
|
#[test]
|
|
|
|
fn positional_multiple() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let r = Command::new("positional_multiple")
|
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),
|
2021-02-24 15:07:57 +00:00
|
|
|
Arg::new("positional")
|
|
|
|
.index(1)
|
2022-07-26 00:17:01 +00:00
|
|
|
.action(ArgAction::Set)
|
2022-08-03 16:20:07 +00:00
|
|
|
.num_args(1..),
|
2018-01-25 04:05:05 +00:00
|
|
|
])
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec!["", "-f", "test1", "test2", "test3"]);
|
2023-05-04 01:40:21 +00:00
|
|
|
assert!(r.is_ok(), "{r:#?}");
|
2017-05-06 23:11:01 +00:00
|
|
|
let m = r.unwrap();
|
2022-06-10 01:03:28 +00:00
|
|
|
assert!(m.contains_id("positional"));
|
|
|
|
assert!(*m.get_one::<bool>("flag").expect("defaulted by clap"));
|
2018-01-25 04:05:05 +00:00
|
|
|
assert_eq!(
|
2022-05-24 15:16:50 +00:00
|
|
|
&*m.get_many::<String>("positional")
|
|
|
|
.unwrap()
|
|
|
|
.map(|v| v.as_str())
|
|
|
|
.collect::<Vec<_>>(),
|
2022-11-24 13:54:25 +00:00
|
|
|
["test1", "test2", "test3"]
|
2018-01-25 04:05:05 +00:00
|
|
|
);
|
2017-05-06 23:11:01 +00:00
|
|
|
}
|
2015-09-17 19:20:56 +00:00
|
|
|
|
2017-05-06 23:11:01 +00:00
|
|
|
#[test]
|
|
|
|
fn positional_multiple_3() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let r = Command::new("positional_multiple")
|
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),
|
2021-02-24 15:07:57 +00:00
|
|
|
Arg::new("positional")
|
|
|
|
.index(1)
|
2022-07-26 00:17:01 +00:00
|
|
|
.action(ArgAction::Set)
|
2022-08-03 16:20:07 +00:00
|
|
|
.num_args(1..),
|
2018-01-25 04:05:05 +00:00
|
|
|
])
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec!["", "test1", "test2", "test3", "--flag"]);
|
2023-05-04 01:40:21 +00:00
|
|
|
assert!(r.is_ok(), "{r:#?}");
|
2017-05-06 23:11:01 +00:00
|
|
|
let m = r.unwrap();
|
2022-06-10 01:03:28 +00:00
|
|
|
assert!(m.contains_id("positional"));
|
|
|
|
assert!(*m.get_one::<bool>("flag").expect("defaulted by clap"));
|
2018-01-25 04:05:05 +00:00
|
|
|
assert_eq!(
|
2022-05-24 15:16:50 +00:00
|
|
|
&*m.get_many::<String>("positional")
|
|
|
|
.unwrap()
|
|
|
|
.map(|v| v.as_str())
|
|
|
|
.collect::<Vec<_>>(),
|
2022-11-24 13:54:25 +00:00
|
|
|
["test1", "test2", "test3"]
|
2018-01-25 04:05:05 +00:00
|
|
|
);
|
2015-09-17 19:20:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn positional_multiple_2() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let result = Command::new("positional_multiple")
|
2022-11-24 13:54:25 +00:00
|
|
|
.args([arg!(-f --flag "some flag"), Arg::new("positional").index(1)])
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec!["", "-f", "test1", "test2", "test3"]);
|
2015-09-17 19:20:56 +00:00
|
|
|
assert!(result.is_err());
|
|
|
|
let err = result.err().unwrap();
|
2022-01-25 22:19:28 +00:00
|
|
|
assert_eq!(err.kind(), ErrorKind::UnknownArgument);
|
2015-09-17 19:20:56 +00:00
|
|
|
}
|
2016-01-25 20:56:37 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn positional_possible_values() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let r = Command::new("positional_possible_values")
|
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),
|
2022-05-24 01:16:02 +00:00
|
|
|
Arg::new("positional").index(1).value_parser(["test123"]),
|
2018-01-25 04:05:05 +00:00
|
|
|
])
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec!["", "-f", "test123"]);
|
2023-01-29 19:14:47 +00:00
|
|
|
assert!(r.is_ok(), "{r:#?}");
|
2017-05-06 23:11:01 +00:00
|
|
|
let m = r.unwrap();
|
2022-06-10 01:03:28 +00:00
|
|
|
assert!(m.contains_id("positional"));
|
|
|
|
assert!(*m.get_one::<bool>("flag").expect("defaulted by clap"));
|
2018-01-25 04:05:05 +00:00
|
|
|
assert_eq!(
|
2022-05-24 15:16:50 +00:00
|
|
|
&*m.get_many::<String>("positional")
|
|
|
|
.unwrap()
|
|
|
|
.map(|v| v.as_str())
|
|
|
|
.collect::<Vec<_>>(),
|
2022-11-24 13:54:25 +00:00
|
|
|
["test123"]
|
2018-01-25 04:05:05 +00:00
|
|
|
);
|
2016-01-25 20:56:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn create_positional() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let _ = Command::new("test")
|
2021-11-18 16:17:15 +00:00
|
|
|
.arg(Arg::new("test").index(1).help("testing testing"))
|
2021-12-27 18:56:12 +00:00
|
|
|
.try_get_matches_from(vec![""])
|
|
|
|
.unwrap();
|
2016-01-25 20:56:37 +00:00
|
|
|
}
|
2016-02-03 05:05:45 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn positional_hyphen_does_not_panic() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let _ = Command::new("test")
|
2020-05-14 20:50:56 +00:00
|
|
|
.arg(Arg::new("dummy"))
|
2021-12-27 18:56:12 +00:00
|
|
|
.try_get_matches_from(vec!["test", "-"])
|
|
|
|
.unwrap();
|
2016-02-03 05:05:45 +00:00
|
|
|
}
|
2016-02-09 14:05:19 +00:00
|
|
|
|
2016-06-04 03:16:02 +00:00
|
|
|
#[test]
|
|
|
|
fn single_positional_usage_string() {
|
2022-02-14 21:47:20 +00:00
|
|
|
let mut cmd = Command::new("test").arg(arg!([FILE] "some file"));
|
2024-05-24 15:13:50 +00:00
|
|
|
assert_data_eq!(cmd.render_usage().to_string(), str!["Usage: test [FILE]"]);
|
2016-06-04 03:16:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn single_positional_multiple_usage_string() {
|
2022-02-14 21:47:20 +00:00
|
|
|
let mut cmd = Command::new("test").arg(arg!([FILE]... "some file"));
|
2024-05-24 15:13:50 +00:00
|
|
|
assert_data_eq!(cmd.render_usage().to_string(), str!["Usage: test [FILE]..."]);
|
2016-06-04 03:16:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn multiple_positional_usage_string() {
|
2022-02-14 21:47:20 +00:00
|
|
|
let mut cmd = Command::new("test")
|
2021-11-19 20:33:11 +00:00
|
|
|
.arg(arg!([FILE] "some file"))
|
|
|
|
.arg(arg!([FILES]... "some file"));
|
2024-05-24 15:13:50 +00:00
|
|
|
assert_data_eq!(cmd.render_usage().to_string(), str!["Usage: test [FILE] [FILES]..."],);
|
2016-06-04 03:16:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn multiple_positional_one_required_usage_string() {
|
2022-02-14 21:47:20 +00:00
|
|
|
let mut cmd = Command::new("test")
|
2021-11-19 20:33:11 +00:00
|
|
|
.arg(arg!(<FILE> "some file"))
|
|
|
|
.arg(arg!([FILES]... "some file"));
|
2024-05-24 15:13:50 +00:00
|
|
|
assert_data_eq!(cmd.render_usage().to_string(), str!["Usage: test <FILE> [FILES]..."]);
|
2016-06-04 03:16:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn single_positional_required_usage_string() {
|
2022-02-14 21:47:20 +00:00
|
|
|
let mut cmd = Command::new("test").arg(arg!(<FILE> "some file"));
|
2024-05-24 15:13:50 +00:00
|
|
|
assert_data_eq!(cmd.render_usage().to_string(), str!["Usage: test <FILE>"]);
|
2016-06-04 03:16:02 +00:00
|
|
|
}
|
2017-01-04 02:58:12 +00:00
|
|
|
|
2019-06-25 23:02:53 +00:00
|
|
|
// This tests a programmer error and will only succeed with debug_assertions
|
|
|
|
#[cfg(debug_assertions)]
|
2017-01-04 02:58:12 +00:00
|
|
|
#[test]
|
2020-11-27 20:37:26 +00:00
|
|
|
#[should_panic = "Found non-required positional argument \
|
2020-04-09 22:33:16 +00:00
|
|
|
with a lower index than a required positional argument"]
|
2017-01-04 02:58:12 +00:00
|
|
|
fn missing_required() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let _ = Command::new("test")
|
2021-11-19 20:33:11 +00:00
|
|
|
.arg(arg!([FILE1] "some file"))
|
|
|
|
.arg(arg!(<FILE2> "some file"))
|
2019-06-25 23:02:53 +00:00
|
|
|
.try_get_matches_from(vec![""]);
|
2017-01-04 02:58:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn missing_required_2() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let r = Command::new("test")
|
2021-11-19 20:33:11 +00:00
|
|
|
.arg(arg!(<FILE1> "some file"))
|
|
|
|
.arg(arg!(<FILE2> "some file"))
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec!["test", "file"]);
|
2017-01-04 02:58:12 +00:00
|
|
|
assert!(r.is_err());
|
2022-01-25 22:19:28 +00:00
|
|
|
assert_eq!(r.unwrap_err().kind(), ErrorKind::MissingRequiredArgument);
|
2017-01-04 02:58:12 +00:00
|
|
|
}
|
2017-03-11 05:17:57 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn last_positional() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let r = Command::new("test")
|
2021-11-19 20:33:11 +00:00
|
|
|
.arg(arg!(<TARGET> "some target"))
|
|
|
|
.arg(arg!([CORPUS] "some corpus"))
|
|
|
|
.arg(arg!([ARGS]... "some file").last(true))
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec!["test", "tgt", "--", "arg"]);
|
2021-12-27 19:57:38 +00:00
|
|
|
assert!(r.is_ok(), "{}", r.unwrap_err());
|
2017-03-11 05:17:57 +00:00
|
|
|
let m = r.unwrap();
|
2022-05-24 15:16:50 +00:00
|
|
|
assert_eq!(
|
|
|
|
m.get_many::<String>("ARGS")
|
|
|
|
.unwrap()
|
|
|
|
.map(|v| v.as_str())
|
|
|
|
.collect::<Vec<_>>(),
|
2022-11-24 13:54:25 +00:00
|
|
|
["arg"]
|
2022-05-24 15:16:50 +00:00
|
|
|
);
|
2017-03-11 05:17:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn last_positional_no_double_dash() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let r = Command::new("test")
|
2021-11-19 20:33:11 +00:00
|
|
|
.arg(arg!(<TARGET> "some target"))
|
|
|
|
.arg(arg!([CORPUS] "some corpus"))
|
|
|
|
.arg(arg!([ARGS]... "some file").last(true))
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec!["test", "tgt", "crp", "arg"]);
|
2017-03-11 05:17:57 +00:00
|
|
|
assert!(r.is_err());
|
2022-01-25 22:19:28 +00:00
|
|
|
assert_eq!(r.unwrap_err().kind(), ErrorKind::UnknownArgument);
|
2017-03-12 16:45:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn last_positional_second_to_last_mult() {
|
2022-02-12 03:48:29 +00:00
|
|
|
let r = Command::new("test")
|
2021-11-19 20:33:11 +00:00
|
|
|
.arg(arg!(<TARGET> "some target"))
|
|
|
|
.arg(arg!([CORPUS]... "some corpus"))
|
|
|
|
.arg(arg!([ARGS]... "some file").last(true))
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec!["test", "tgt", "crp1", "crp2", "--", "arg"]);
|
2022-01-25 22:19:28 +00:00
|
|
|
assert!(r.is_ok(), "{:?}", r.unwrap_err().kind());
|
2018-01-25 04:05:05 +00:00
|
|
|
}
|
2020-10-23 10:55:08 +00:00
|
|
|
|
|
|
|
#[cfg(debug_assertions)]
|
|
|
|
#[test]
|
|
|
|
#[should_panic = "Argument 'arg' is a positional argument and can't have short or long name versions"]
|
|
|
|
fn positional_arg_with_long() {
|
2022-02-12 03:48:29 +00:00
|
|
|
use clap::{Arg, Command};
|
2020-10-23 10:55:08 +00:00
|
|
|
|
2022-02-12 03:48:29 +00:00
|
|
|
let _ = Command::new("test")
|
2020-10-23 10:55:08 +00:00
|
|
|
.arg(Arg::new("arg").index(1).long("arg"))
|
|
|
|
.try_get_matches();
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(debug_assertions)]
|
|
|
|
#[test]
|
|
|
|
#[should_panic = "Argument 'arg' is a positional argument and can't have short or long name versions"]
|
|
|
|
fn positional_arg_with_short() {
|
2022-02-12 03:48:29 +00:00
|
|
|
use clap::{Arg, Command};
|
2020-10-23 10:55:08 +00:00
|
|
|
|
2022-02-12 03:48:29 +00:00
|
|
|
let _ = Command::new("test")
|
2020-10-23 10:55:08 +00:00
|
|
|
.arg(Arg::new("arg").index(1).short('a'))
|
|
|
|
.try_get_matches();
|
|
|
|
}
|
2022-01-04 15:04:31 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn ignore_hyphen_values_on_last() {
|
2024-05-04 19:59:40 +00:00
|
|
|
let cmd = Command::new("foo")
|
2022-01-04 15:04:31 +00:00
|
|
|
.arg(
|
2024-05-04 19:59:40 +00:00
|
|
|
Arg::new("cmd")
|
2022-08-03 16:20:07 +00:00
|
|
|
.num_args(1..)
|
2022-01-04 15:04:31 +00:00
|
|
|
.last(true)
|
|
|
|
.allow_hyphen_values(true),
|
|
|
|
)
|
|
|
|
.arg(
|
2024-05-04 19:59:40 +00:00
|
|
|
Arg::new("name")
|
2022-01-04 15:04:31 +00:00
|
|
|
.long("name")
|
|
|
|
.short('n')
|
2022-07-26 00:17:01 +00:00
|
|
|
.action(ArgAction::Set)
|
2022-01-04 15:04:31 +00:00
|
|
|
.required(false),
|
|
|
|
);
|
|
|
|
|
2022-02-14 21:47:20 +00:00
|
|
|
let matches = cmd.try_get_matches_from(["test", "-n", "foo"]).unwrap();
|
2022-05-24 15:16:50 +00:00
|
|
|
assert_eq!(
|
|
|
|
matches.get_one::<String>("name").map(|v| v.as_str()),
|
|
|
|
Some("foo")
|
|
|
|
);
|
2022-01-04 15:04:31 +00:00
|
|
|
}
|