2016-01-31 12:06:21 +00:00
|
|
|
use clap::{App, Arg, ErrorKind};
|
2015-09-17 19:20:56 +00:00
|
|
|
|
2016-02-04 06:34:46 +00:00
|
|
|
#[test]
|
|
|
|
fn only_pos_follow() {
|
|
|
|
let r = App::new("onlypos")
|
2018-01-25 04:05:05 +00:00
|
|
|
.args(&[
|
2018-04-21 18:59:19 +00:00
|
|
|
Arg::from("-f [flag] 'some opt'"),
|
|
|
|
Arg::from("[arg] 'some arg'"),
|
2018-01-25 04:05:05 +00:00
|
|
|
])
|
2018-10-19 20:42:13 +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("arg"));
|
|
|
|
assert!(!m.is_present("f"));
|
|
|
|
assert_eq!(m.value_of("arg").unwrap(), "-f");
|
|
|
|
}
|
|
|
|
|
2017-05-06 23:11:01 +00:00
|
|
|
#[test]
|
|
|
|
fn issue_946() {
|
|
|
|
let r = App::new("compiletest")
|
|
|
|
.setting(clap::AppSettings::AllowLeadingHyphen)
|
2018-10-19 20:42:13 +00:00
|
|
|
.arg("--exact 'filters match exactly'")
|
2018-01-25 04:05:05 +00:00
|
|
|
.arg(
|
2020-05-14 20:50:56 +00:00
|
|
|
clap::Arg::new("filter")
|
2018-01-25 04:05:05 +00:00
|
|
|
.index(1)
|
|
|
|
.takes_value(true)
|
2020-04-21 15:51:38 +00:00
|
|
|
.about("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"]);
|
2017-05-06 23:11:01 +00:00
|
|
|
assert!(r.is_ok(), "{:#?}", r);
|
|
|
|
let matches = r.unwrap();
|
|
|
|
|
|
|
|
assert!(matches.is_present("exact"));
|
|
|
|
assert!(matches.value_of("filter").is_none());
|
|
|
|
}
|
|
|
|
|
2015-09-17 19:20:56 +00:00
|
|
|
#[test]
|
|
|
|
fn positional() {
|
2017-05-06 23:11:01 +00:00
|
|
|
let r = App::new("positional")
|
2016-01-21 06:48:30 +00:00
|
|
|
.args(&[
|
2018-04-21 18:59:19 +00:00
|
|
|
Arg::from("-f, --flag 'some flag'"),
|
2020-05-14 20:50:56 +00:00
|
|
|
Arg::new("positional").index(1),
|
2018-01-25 04:05:05 +00:00
|
|
|
])
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec!["", "-f", "test"]);
|
2017-05-06 23:11:01 +00:00
|
|
|
assert!(r.is_ok(), "{:#?}", r);
|
|
|
|
let m = r.unwrap();
|
2015-09-17 19:20:56 +00:00
|
|
|
assert!(m.is_present("positional"));
|
|
|
|
assert!(m.is_present("flag"));
|
2016-01-25 20:56:37 +00:00
|
|
|
assert_eq!(m.value_of("positional").unwrap(), "test");
|
2015-09-17 19:20:56 +00:00
|
|
|
|
|
|
|
let m = App::new("positional")
|
2016-01-21 06:48:30 +00:00
|
|
|
.args(&[
|
2018-04-21 18:59:19 +00:00
|
|
|
Arg::from("-f, --flag 'some flag'"),
|
2020-05-14 20:50:56 +00:00
|
|
|
Arg::new("positional").index(1),
|
2018-01-25 04:05:05 +00:00
|
|
|
])
|
2016-01-25 20:56:37 +00:00
|
|
|
.get_matches_from(vec!["", "test", "--flag"]);
|
2015-09-17 19:20:56 +00:00
|
|
|
assert!(m.is_present("positional"));
|
|
|
|
assert!(m.is_present("flag"));
|
2016-01-25 20:56:37 +00:00
|
|
|
assert_eq!(m.value_of("positional").unwrap(), "test");
|
2015-09-17 19:20:56 +00:00
|
|
|
}
|
|
|
|
|
2016-02-02 12:43:53 +00:00
|
|
|
#[test]
|
|
|
|
fn lots_o_vals() {
|
|
|
|
let r = App::new("opts")
|
2018-04-21 18:59:19 +00:00
|
|
|
.arg(Arg::from("[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",
|
|
|
|
]);
|
2016-02-02 12:43:53 +00:00
|
|
|
assert!(r.is_ok());
|
|
|
|
let m = r.unwrap();
|
|
|
|
assert!(m.is_present("opt"));
|
2019-10-02 13:27:19 +00:00
|
|
|
assert_eq!(m.values_of("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() {
|
2017-05-06 23:11:01 +00:00
|
|
|
let r = App::new("positional_multiple")
|
2016-01-21 06:48:30 +00:00
|
|
|
.args(&[
|
2018-04-21 18:59:19 +00:00
|
|
|
Arg::from("-f, --flag 'some flag'"),
|
2020-05-14 20:50:56 +00:00
|
|
|
Arg::new("positional").index(1).multiple(true),
|
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"]);
|
2017-05-06 23:11:01 +00:00
|
|
|
assert!(r.is_ok(), "{:#?}", r);
|
|
|
|
let m = r.unwrap();
|
2015-09-17 19:20:56 +00:00
|
|
|
assert!(m.is_present("positional"));
|
|
|
|
assert!(m.is_present("flag"));
|
2018-01-25 04:05:05 +00:00
|
|
|
assert_eq!(
|
|
|
|
&*m.values_of("positional").unwrap().collect::<Vec<_>>(),
|
|
|
|
&["test1", "test2", "test3"]
|
|
|
|
);
|
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() {
|
|
|
|
let r = App::new("positional_multiple")
|
2016-01-21 06:48:30 +00:00
|
|
|
.args(&[
|
2018-04-21 18:59:19 +00:00
|
|
|
Arg::from("-f, --flag 'some flag'"),
|
2020-05-14 20:50:56 +00:00
|
|
|
Arg::new("positional").index(1).multiple(true),
|
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"]);
|
2017-05-06 23:11:01 +00:00
|
|
|
assert!(r.is_ok(), "{:#?}", r);
|
|
|
|
let m = r.unwrap();
|
2015-09-17 19:20:56 +00:00
|
|
|
assert!(m.is_present("positional"));
|
|
|
|
assert!(m.is_present("flag"));
|
2018-01-25 04:05:05 +00:00
|
|
|
assert_eq!(
|
|
|
|
&*m.values_of("positional").unwrap().collect::<Vec<_>>(),
|
|
|
|
&["test1", "test2", "test3"]
|
|
|
|
);
|
2015-09-17 19:20:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn positional_multiple_2() {
|
|
|
|
let result = App::new("positional_multiple")
|
2016-01-21 06:48:30 +00:00
|
|
|
.args(&[
|
2018-04-21 18:59:19 +00:00
|
|
|
Arg::from("-f, --flag 'some flag'"),
|
2020-05-14 20:50:56 +00:00
|
|
|
Arg::new("positional").index(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"]);
|
2015-09-17 19:20:56 +00:00
|
|
|
assert!(result.is_err());
|
|
|
|
let err = result.err().unwrap();
|
2016-01-22 04:18:52 +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() {
|
2017-05-06 23:11:01 +00:00
|
|
|
let r = App::new("positional_possible_values")
|
2016-01-25 20:56:37 +00:00
|
|
|
.args(&[
|
2018-04-21 18:59:19 +00:00
|
|
|
Arg::from("-f, --flag 'some flag'"),
|
2020-05-14 20:50:56 +00:00
|
|
|
Arg::new("positional").index(1).possible_value("test123"),
|
2018-01-25 04:05:05 +00:00
|
|
|
])
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec!["", "-f", "test123"]);
|
2017-05-06 23:11:01 +00:00
|
|
|
assert!(r.is_ok(), "{:#?}", r);
|
|
|
|
let m = r.unwrap();
|
2016-01-25 20:56:37 +00:00
|
|
|
assert!(m.is_present("positional"));
|
|
|
|
assert!(m.is_present("flag"));
|
2018-01-25 04:05:05 +00:00
|
|
|
assert_eq!(
|
|
|
|
&*m.values_of("positional").unwrap().collect::<Vec<_>>(),
|
|
|
|
&["test123"]
|
|
|
|
);
|
2016-01-25 20:56:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn create_positional() {
|
|
|
|
let _ = App::new("test")
|
2020-05-14 20:50:56 +00:00
|
|
|
.arg(Arg::new("test").index(1).about("testing testing"))
|
2018-01-25 04:05:05 +00:00
|
|
|
.get_matches_from(vec![""]);
|
2016-01-25 20:56:37 +00:00
|
|
|
}
|
2016-02-03 05:05:45 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn positional_hyphen_does_not_panic() {
|
|
|
|
let _ = App::new("test")
|
2020-05-14 20:50:56 +00:00
|
|
|
.arg(Arg::new("dummy"))
|
2016-02-03 05:05:45 +00:00
|
|
|
.get_matches_from(vec!["test", "-"]);
|
|
|
|
}
|
2016-02-09 14:05:19 +00:00
|
|
|
|
2016-06-04 03:16:02 +00:00
|
|
|
#[test]
|
|
|
|
fn single_positional_usage_string() {
|
2018-10-19 20:42:13 +00:00
|
|
|
let mut app = App::new("test").arg("[FILE] 'some file'");
|
2018-07-24 19:27:36 +00:00
|
|
|
assert_eq!(app.generate_usage(), "USAGE:\n test [FILE]");
|
2016-06-04 03:16:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn single_positional_multiple_usage_string() {
|
2018-10-19 20:42:13 +00:00
|
|
|
let mut app = App::new("test").arg("[FILE]... 'some file'");
|
2018-07-24 19:27:36 +00:00
|
|
|
assert_eq!(app.generate_usage(), "USAGE:\n test [FILE]...");
|
2016-06-04 03:16:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn multiple_positional_usage_string() {
|
2018-07-24 19:27:36 +00:00
|
|
|
let mut app = App::new("test")
|
2018-10-19 20:42:13 +00:00
|
|
|
.arg("[FILE] 'some file'")
|
|
|
|
.arg("[FILES]... 'some file'");
|
2018-07-24 19:27:36 +00:00
|
|
|
assert_eq!(app.generate_usage(), "USAGE:\n test [ARGS]");
|
2016-06-04 03:16:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn multiple_positional_one_required_usage_string() {
|
2018-07-24 19:27:36 +00:00
|
|
|
let mut app = App::new("test")
|
2018-10-19 20:42:13 +00:00
|
|
|
.arg("<FILE> 'some file'")
|
|
|
|
.arg("[FILES]... 'some file'");
|
2018-07-24 19:27:36 +00:00
|
|
|
assert_eq!(app.generate_usage(), "USAGE:\n test <FILE> [FILES]...");
|
2016-06-04 03:16:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn single_positional_required_usage_string() {
|
2018-10-19 20:42:13 +00:00
|
|
|
let mut app = App::new("test").arg("<FILE> 'some file'");
|
2018-07-24 19:27:36 +00:00
|
|
|
assert_eq!(app.generate_usage(), "USAGE:\n 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() {
|
2019-06-25 23:02:53 +00:00
|
|
|
let _ = App::new("test")
|
2018-10-19 20:42:13 +00:00
|
|
|
.arg("[FILE1] 'some file'")
|
|
|
|
.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() {
|
|
|
|
let r = App::new("test")
|
2018-10-19 20:42:13 +00:00
|
|
|
.arg("<FILE1> 'some file'")
|
|
|
|
.arg("<FILE2> 'some file'")
|
|
|
|
.try_get_matches_from(vec!["test", "file"]);
|
2017-01-04 02:58:12 +00:00
|
|
|
assert!(r.is_err());
|
|
|
|
assert_eq!(r.unwrap_err().kind, ErrorKind::MissingRequiredArgument);
|
|
|
|
}
|
2017-03-11 05:17:57 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn last_positional() {
|
|
|
|
let r = App::new("test")
|
2018-10-19 20:42:13 +00:00
|
|
|
.arg("<TARGET> 'some target'")
|
|
|
|
.arg("[CORPUS] 'some corpus'")
|
2018-04-21 18:59:19 +00:00
|
|
|
.arg(Arg::from("[ARGS]... 'some file'").last(true))
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec!["test", "tgt", "--", "arg"]);
|
2017-03-11 05:17:57 +00:00
|
|
|
assert!(r.is_ok());
|
|
|
|
let m = r.unwrap();
|
|
|
|
assert_eq!(m.values_of("ARGS").unwrap().collect::<Vec<_>>(), &["arg"]);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn last_positional_no_double_dash() {
|
|
|
|
let r = App::new("test")
|
2018-10-19 20:42:13 +00:00
|
|
|
.arg("<TARGET> 'some target'")
|
|
|
|
.arg("[CORPUS] 'some corpus'")
|
2018-04-21 18:59:19 +00:00
|
|
|
.arg(Arg::from("[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());
|
|
|
|
assert_eq!(r.unwrap_err().kind, ErrorKind::UnknownArgument);
|
2017-03-12 16:45:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn last_positional_second_to_last_mult() {
|
|
|
|
let r = App::new("test")
|
2018-10-19 20:42:13 +00:00
|
|
|
.arg("<TARGET> 'some target'")
|
|
|
|
.arg("[CORPUS]... 'some corpus'")
|
2018-04-21 18:59:19 +00:00
|
|
|
.arg(Arg::from("[ARGS]... 'some file'").last(true))
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec!["test", "tgt", "crp1", "crp2", "--", "arg"]);
|
2017-03-12 16:45:59 +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() {
|
|
|
|
use clap::{App, Arg};
|
|
|
|
|
|
|
|
let _ = App::new("test")
|
|
|
|
.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() {
|
|
|
|
use clap::{App, Arg};
|
|
|
|
|
|
|
|
let _ = App::new("test")
|
|
|
|
.arg(Arg::new("arg").index(1).short('a'))
|
|
|
|
.try_get_matches();
|
|
|
|
}
|