2015-09-17 19:20:56 +00:00
|
|
|
extern crate clap;
|
|
|
|
|
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")
|
|
|
|
.args(&[Arg::from_usage("-f [flag] 'some opt'"),
|
|
|
|
Arg::from_usage("[arg] 'some arg'")])
|
|
|
|
.get_matches_from_safe(vec!["", "--", "-f"]);
|
|
|
|
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");
|
|
|
|
}
|
|
|
|
|
2015-09-17 19:20:56 +00:00
|
|
|
#[test]
|
|
|
|
fn positional() {
|
|
|
|
let m = App::new("positional")
|
2016-01-21 06:48:30 +00:00
|
|
|
.args(&[
|
2015-09-17 19:20:56 +00:00
|
|
|
Arg::from_usage("-f, --flag 'some flag'"),
|
|
|
|
Arg::with_name("positional")
|
|
|
|
.index(1)
|
|
|
|
])
|
2016-01-25 20:56:37 +00:00
|
|
|
.get_matches_from(vec!["", "-f", "test"]);
|
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(&[
|
2015-09-17 19:20:56 +00:00
|
|
|
Arg::from_usage("-f, --flag 'some flag'"),
|
|
|
|
Arg::with_name("positional")
|
|
|
|
.index(1)
|
|
|
|
])
|
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")
|
|
|
|
.arg(
|
|
|
|
Arg::from_usage("[opt]... 'some pos'"),
|
|
|
|
)
|
|
|
|
.get_matches_from_safe(vec!["",
|
|
|
|
"some", "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
|
|
|
|
"some", "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
|
|
|
|
"some", "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
|
|
|
|
"some", "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
|
|
|
|
"some", "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
|
|
|
|
"some", "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
|
|
|
|
"some", "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
|
|
|
|
"some", "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
|
|
|
|
"some", "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
|
|
|
|
"some", "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
|
|
|
|
"some", "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
|
|
|
|
"some", "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
|
|
|
|
"some", "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
|
|
|
|
"some", "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
|
|
|
|
"some", "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
|
|
|
|
"some", "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
|
|
|
|
"some", "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
|
|
|
|
"some", "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
|
|
|
|
"some", "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
|
|
|
|
"some", "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
|
|
|
|
"some", "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
|
|
|
|
"some", "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
|
|
|
|
"some", "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
|
|
|
|
"some", "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
|
|
|
|
"some", "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
|
|
|
|
"some", "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
|
|
|
|
"some", "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
|
|
|
|
]);
|
|
|
|
assert!(r.is_ok());
|
|
|
|
let m = r.unwrap();
|
|
|
|
assert!(m.is_present("opt"));
|
|
|
|
assert_eq!(m.values_of("opt").unwrap().collect::<Vec<_>>().len(), 297); // i.e. more than u8
|
|
|
|
}
|
|
|
|
|
2015-09-17 19:20:56 +00:00
|
|
|
#[test]
|
|
|
|
fn positional_multiple() {
|
|
|
|
let m = App::new("positional_multiple")
|
2016-01-21 06:48:30 +00:00
|
|
|
.args(&[
|
2015-09-17 19:20:56 +00:00
|
|
|
Arg::from_usage("-f, --flag 'some flag'"),
|
|
|
|
Arg::with_name("positional")
|
|
|
|
.index(1)
|
|
|
|
.multiple(true)
|
|
|
|
])
|
2016-01-25 20:56:37 +00:00
|
|
|
.get_matches_from(vec!["", "-f", "test1", "test2", "test3"]);
|
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.values_of("positional").unwrap().collect::<Vec<_>>(), &["test1", "test2", "test3"]);
|
2015-09-17 19:20:56 +00:00
|
|
|
|
|
|
|
let m = App::new("positional_multiple")
|
2016-01-21 06:48:30 +00:00
|
|
|
.args(&[
|
2015-09-17 19:20:56 +00:00
|
|
|
Arg::from_usage("-f, --flag 'some flag'"),
|
|
|
|
Arg::with_name("positional")
|
|
|
|
.index(1)
|
|
|
|
.multiple(true)
|
|
|
|
])
|
2016-01-25 20:56:37 +00:00
|
|
|
.get_matches_from(vec!["", "test1", "test2", "test3", "--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.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(&[
|
2015-09-17 19:20:56 +00:00
|
|
|
Arg::from_usage("-f, --flag 'some flag'"),
|
|
|
|
Arg::with_name("positional")
|
|
|
|
.index(1)
|
|
|
|
])
|
2016-01-25 20:56:37 +00:00
|
|
|
.get_matches_from_safe(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() {
|
|
|
|
let m = App::new("positional_possible_values")
|
|
|
|
.args(&[
|
|
|
|
Arg::from_usage("-f, --flag 'some flag'"),
|
|
|
|
Arg::with_name("positional")
|
|
|
|
.index(1)
|
|
|
|
.possible_value("test123")
|
|
|
|
])
|
|
|
|
.get_matches_from(vec!["", "-f", "test123"]);
|
|
|
|
assert!(m.is_present("positional"));
|
|
|
|
assert!(m.is_present("flag"));
|
|
|
|
assert_eq!(&*m.values_of("positional").unwrap().collect::<Vec<_>>(), &["test123"]);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn create_positional() {
|
|
|
|
let _ = App::new("test")
|
|
|
|
.arg(Arg::with_name("test")
|
|
|
|
.index(1)
|
|
|
|
.help("testing testing"))
|
2016-10-04 21:22:43 +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")
|
|
|
|
.arg(Arg::with_name("dummy"))
|
|
|
|
.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() {
|
|
|
|
let m = App::new("test").arg_from_usage("[FILE] 'some file'").get_matches_from(vec!["test"]);
|
2016-06-24 04:17:04 +00:00
|
|
|
assert_eq!(m.usage(), "USAGE:\n test [FILE]");
|
2016-06-04 03:16:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn single_positional_multiple_usage_string() {
|
|
|
|
let m = App::new("test").arg_from_usage("[FILE]... 'some file'").get_matches_from(vec!["test"]);
|
2016-06-24 04:17:04 +00:00
|
|
|
assert_eq!(m.usage(), "USAGE:\n test [FILE]...");
|
2016-06-04 03:16:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn multiple_positional_usage_string() {
|
|
|
|
let m = App::new("test")
|
|
|
|
.arg_from_usage("[FILE] 'some file'")
|
|
|
|
.arg_from_usage("[FILES]... 'some file'")
|
|
|
|
.get_matches_from(vec!["test"]);
|
2016-06-24 04:17:04 +00:00
|
|
|
assert_eq!(m.usage(), "USAGE:\n test [ARGS]");
|
2016-06-04 03:16:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn multiple_positional_one_required_usage_string() {
|
|
|
|
let m = App::new("test")
|
|
|
|
.arg_from_usage("<FILE> 'some file'")
|
|
|
|
.arg_from_usage("[FILES]... 'some file'")
|
|
|
|
.get_matches_from(vec!["test", "file"]);
|
2017-03-04 22:37:08 +00:00
|
|
|
assert_eq!(m.usage(), "USAGE:\n test <FILE> [FILES]...");
|
2016-06-04 03:16:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn single_positional_required_usage_string() {
|
|
|
|
let m = App::new("test")
|
|
|
|
.arg_from_usage("<FILE> 'some file'")
|
|
|
|
.get_matches_from(vec!["test", "file"]);
|
2016-06-24 04:17:04 +00:00
|
|
|
assert_eq!(m.usage(), "USAGE:\n test <FILE>");
|
2016-06-04 03:16:02 +00:00
|
|
|
}
|
2017-01-04 02:58:12 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[should_panic]
|
|
|
|
fn missing_required() {
|
|
|
|
let r = App::new("test")
|
|
|
|
.arg_from_usage("[FILE1] 'some file'")
|
|
|
|
.arg_from_usage("<FILE2> 'some file'")
|
|
|
|
.get_matches_from_safe(vec!["test", "file"]);
|
|
|
|
assert!(r.is_err());
|
|
|
|
assert_eq!(r.unwrap_err().kind, ErrorKind::MissingRequiredArgument);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn missing_required_2() {
|
|
|
|
let r = App::new("test")
|
|
|
|
.arg_from_usage("<FILE1> 'some file'")
|
|
|
|
.arg_from_usage("<FILE2> 'some file'")
|
|
|
|
.get_matches_from_safe(vec!["test", "file"]);
|
|
|
|
assert!(r.is_err());
|
|
|
|
assert_eq!(r.unwrap_err().kind, ErrorKind::MissingRequiredArgument);
|
|
|
|
}
|