2015-09-09 02:39:31 +00:00
|
|
|
extern crate clap;
|
|
|
|
|
2016-02-02 08:13:43 +00:00
|
|
|
use clap::{App, SubCommand, ErrorKind, Arg};
|
2016-01-13 17:03:27 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn help_short() {
|
|
|
|
let m = App::new("test")
|
|
|
|
.author("Kevin K.")
|
|
|
|
.about("tests stuff")
|
|
|
|
.version("1.3")
|
2016-01-21 05:18:53 +00:00
|
|
|
.get_matches_from_safe(vec!["myprog", "-h"]);
|
2016-01-13 17:03:27 +00:00
|
|
|
|
|
|
|
assert!(m.is_err());
|
2016-01-21 05:18:53 +00:00
|
|
|
assert_eq!(m.unwrap_err().kind, ErrorKind::HelpDisplayed);
|
2016-01-13 17:03:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn help_long() {
|
|
|
|
let m = App::new("test")
|
|
|
|
.author("Kevin K.")
|
|
|
|
.about("tests stuff")
|
|
|
|
.version("1.3")
|
2016-01-21 05:18:53 +00:00
|
|
|
.get_matches_from_safe(vec!["myprog", "--help"]);
|
2016-01-13 17:03:27 +00:00
|
|
|
|
|
|
|
assert!(m.is_err());
|
2016-01-21 05:18:53 +00:00
|
|
|
assert_eq!(m.unwrap_err().kind, ErrorKind::HelpDisplayed);
|
2016-01-13 17:03:27 +00:00
|
|
|
}
|
2015-09-09 02:39:31 +00:00
|
|
|
|
2016-01-15 04:23:50 +00:00
|
|
|
#[test]
|
|
|
|
fn help_no_subcommand() {
|
|
|
|
let m = App::new("test")
|
|
|
|
.author("Kevin K.")
|
|
|
|
.about("tests stuff")
|
|
|
|
.version("1.3")
|
2016-01-21 05:18:53 +00:00
|
|
|
.get_matches_from_safe(vec!["myprog", "help"]);
|
2016-01-15 04:23:50 +00:00
|
|
|
|
|
|
|
assert!(m.is_err());
|
2016-01-21 06:48:30 +00:00
|
|
|
assert_eq!(m.unwrap_err().kind, ErrorKind::UnknownArgument);
|
2016-01-15 04:23:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn help_subcommand() {
|
|
|
|
let m = App::new("test")
|
|
|
|
.author("Kevin K.")
|
|
|
|
.about("tests stuff")
|
|
|
|
.version("1.3")
|
|
|
|
.subcommand(SubCommand::with_name("test")
|
|
|
|
.about("tests things")
|
|
|
|
.arg_from_usage("-v --verbose 'with verbosity'"))
|
2016-01-21 05:18:53 +00:00
|
|
|
.get_matches_from_safe(vec!["myprog", "help"]);
|
2016-01-15 04:23:50 +00:00
|
|
|
|
|
|
|
assert!(m.is_err());
|
2016-01-21 05:18:53 +00:00
|
|
|
assert_eq!(m.unwrap_err().kind, ErrorKind::HelpDisplayed);
|
2016-01-15 04:23:50 +00:00
|
|
|
}
|
|
|
|
|
2015-09-09 02:39:31 +00:00
|
|
|
#[test]
|
|
|
|
fn print_app_help() {
|
|
|
|
let mut app = App::new("test")
|
|
|
|
.author("Kevin K.")
|
|
|
|
.about("tests stuff")
|
|
|
|
.version("1.3")
|
|
|
|
.args_from_usage("-f, --flag 'some flag'
|
2016-04-03 00:39:50 +00:00
|
|
|
--option [opt] 'some option'")
|
|
|
|
.arg(Arg::with_name("other")
|
|
|
|
.short("O")
|
|
|
|
.long("other-opt")
|
|
|
|
.takes_value(true)
|
|
|
|
.help("some other opt"));
|
2015-09-09 02:39:31 +00:00
|
|
|
// We call a get_matches method to cause --help and --version to be built
|
|
|
|
let _ = app.get_matches_from_safe_borrow(vec![""]);
|
|
|
|
|
|
|
|
// Now we check the output of print_help()
|
|
|
|
let mut help = vec![];
|
|
|
|
app.write_help(&mut help).ok().expect("failed to print help");
|
|
|
|
assert_eq!(&*String::from_utf8_lossy(&*help), &*String::from("test 1.3\n\
|
|
|
|
Kevin K.
|
|
|
|
tests stuff
|
|
|
|
|
|
|
|
USAGE:
|
2016-03-14 01:32:44 +00:00
|
|
|
test [FLAGS] [OPTIONS]
|
2015-09-09 02:39:31 +00:00
|
|
|
|
|
|
|
FLAGS:
|
|
|
|
-f, --flag some flag
|
|
|
|
-h, --help Prints help information
|
|
|
|
-V, --version Prints version information
|
|
|
|
|
|
|
|
OPTIONS:
|
2016-04-03 00:39:50 +00:00
|
|
|
--option <opt> some option
|
|
|
|
-O, --other-opt <other> some other opt\n"));
|
2015-09-09 02:39:31 +00:00
|
|
|
}
|
2016-02-02 08:13:43 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn possible_values() {
|
|
|
|
let mut app = App::new("test")
|
|
|
|
.author("Kevin K.")
|
|
|
|
.about("tests stuff")
|
|
|
|
.version("1.3")
|
|
|
|
.args(&[Arg::from_usage("-o, --opt [opt] 'some option'").possible_values(&["one", "two"]),
|
|
|
|
Arg::from_usage("[arg1] 'some pos arg'").possible_values(&["three", "four"])]);
|
|
|
|
// We call a get_matches method to cause --help and --version to be built
|
|
|
|
let _ = app.get_matches_from_safe_borrow(vec![""]);
|
|
|
|
|
|
|
|
// Now we check the output of print_help()
|
|
|
|
let mut help = vec![];
|
|
|
|
app.write_help(&mut help).expect("failed to print help");
|
|
|
|
assert_eq!(&*String::from_utf8_lossy(&*help), &*String::from("test 1.3\n\
|
|
|
|
Kevin K.
|
|
|
|
tests stuff
|
|
|
|
|
|
|
|
USAGE:
|
2016-03-14 01:32:44 +00:00
|
|
|
test [FLAGS] [OPTIONS] [ARGS]
|
2016-02-02 08:13:43 +00:00
|
|
|
|
|
|
|
FLAGS:
|
|
|
|
-h, --help Prints help information
|
|
|
|
-V, --version Prints version information
|
|
|
|
|
|
|
|
OPTIONS:
|
|
|
|
-o, --opt <opt> some option [values: one, two]
|
|
|
|
|
|
|
|
ARGS:
|
2016-05-09 00:38:13 +00:00
|
|
|
<arg1> some pos arg [values: three, four]\n"));
|
2016-02-02 08:13:43 +00:00
|
|
|
}
|