2015-09-17 19:20:56 +00:00
|
|
|
extern crate clap;
|
|
|
|
|
2016-01-21 05:18:53 +00:00
|
|
|
use clap::{App, Arg, ErrorKind};
|
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-21 05:18:53 +00:00
|
|
|
.get_matches_from(vec!["myprog", "-f", "test"]);
|
2015-09-17 19:20:56 +00:00
|
|
|
assert!(m.is_present("positional"));
|
|
|
|
assert!(m.is_present("flag"));
|
2016-01-21 06:48:30 +00:00
|
|
|
assert_eq!(m.value_of("positional"), "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-21 05:18:53 +00:00
|
|
|
.get_matches_from(vec!["myprog", "test", "--flag"]);
|
2015-09-17 19:20:56 +00:00
|
|
|
assert!(m.is_present("positional"));
|
|
|
|
assert!(m.is_present("flag"));
|
2016-01-21 06:48:30 +00:00
|
|
|
assert_eq!(m.value_of("positional"), "test");
|
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-21 05:18:53 +00:00
|
|
|
.get_matches_from(vec!["myprog", "-f", "test1", "test2", "test3"]);
|
2015-09-17 19:20:56 +00:00
|
|
|
assert!(m.is_present("positional"));
|
|
|
|
assert!(m.is_present("flag"));
|
2016-01-21 06:48:30 +00:00
|
|
|
assert_eq!(m.values_of("positional").unwrap().collect::<Vec<_>>(), 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-21 05:18:53 +00:00
|
|
|
.get_matches_from(vec!["myprog", "test1", "test2", "test3", "--flag"]);
|
2015-09-17 19:20:56 +00:00
|
|
|
assert!(m.is_present("positional"));
|
|
|
|
assert!(m.is_present("flag"));
|
2016-01-21 06:48:30 +00:00
|
|
|
assert_eq!(m.values_of("positional").unwrap().collect::<Vec<_>>(), 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-21 05:18:53 +00:00
|
|
|
.get_matches_from_safe(vec!["myprog", "-f", "test1", "test2", "test3"]);
|
2015-09-17 19:20:56 +00:00
|
|
|
assert!(result.is_err());
|
|
|
|
let err = result.err().unwrap();
|
2016-01-21 05:18:53 +00:00
|
|
|
assert_eq!(err.kind, ErrorKind::UnexpectedArgument);
|
2015-09-17 19:20:56 +00:00
|
|
|
}
|