2015-09-09 02:39:31 +00:00
|
|
|
extern crate clap;
|
2016-05-10 02:46:09 +00:00
|
|
|
extern crate regex;
|
|
|
|
|
|
|
|
include!("../clap-test.rs");
|
2015-09-09 02:39:31 +00:00
|
|
|
|
2016-05-09 03:20:50 +00:00
|
|
|
use clap::{App, SubCommand, ErrorKind};
|
|
|
|
|
|
|
|
static HELP: &'static str = "clap-test v1.4.8
|
|
|
|
Kevin K. <kbknapp@gmail.com>
|
|
|
|
tests clap library
|
|
|
|
|
|
|
|
USAGE:
|
|
|
|
clap-test [FLAGS] [OPTIONS] [ARGS] [SUBCOMMAND]
|
|
|
|
|
|
|
|
FLAGS:
|
|
|
|
-f, --flag tests flags
|
|
|
|
-F tests flags with exclusions
|
|
|
|
-h, --help Prints help information
|
|
|
|
-V, --version Prints version information
|
|
|
|
|
|
|
|
OPTIONS:
|
|
|
|
-O, --Option <option3> specific vals [values: fast, slow]
|
|
|
|
--long-option-2 <option2> tests long options with exclusions
|
|
|
|
--maxvals3 <maxvals>... Tests 3 max vals
|
|
|
|
--minvals2 <minvals>... Tests 2 min vals
|
|
|
|
--multvals <one> <two> Tests mutliple values, not mult occs
|
|
|
|
--multvalsmo <one> <two> Tests mutliple values, and mult occs
|
|
|
|
-o, --option <opt>... tests options
|
|
|
|
|
|
|
|
ARGS:
|
|
|
|
<positional> tests positionals
|
|
|
|
<positional2> tests positionals with exclusions
|
|
|
|
<positional3>... tests specific values [values: vi, emacs]
|
|
|
|
|
|
|
|
SUBCOMMANDS:
|
|
|
|
help Prints this message or the help of the given subcommand(s)
|
2016-05-30 09:39:54 +00:00
|
|
|
subcmd tests subcommands";
|
2016-05-09 03:20:50 +00:00
|
|
|
|
2016-05-30 10:01:00 +00:00
|
|
|
static AFTER_HELP: &'static str = "some text that comes before the help
|
|
|
|
|
|
|
|
clap-test v1.4.8
|
|
|
|
tests clap library
|
|
|
|
|
|
|
|
USAGE:
|
2016-06-24 04:17:04 +00:00
|
|
|
clap-test
|
2016-05-30 10:01:00 +00:00
|
|
|
|
|
|
|
FLAGS:
|
|
|
|
-h, --help Prints help information
|
|
|
|
-V, --version Prints version information
|
|
|
|
|
|
|
|
some text that comes after the help";
|
|
|
|
|
2016-05-09 03:20:50 +00:00
|
|
|
static SC_HELP: &'static str = "subcmd 0.1
|
|
|
|
Kevin K. <kbknapp@gmail.com>
|
|
|
|
tests subcommands
|
|
|
|
|
|
|
|
USAGE:
|
2016-06-04 03:16:02 +00:00
|
|
|
subcmd [FLAGS] [OPTIONS] [--] [scpositional]
|
2016-05-09 03:20:50 +00:00
|
|
|
|
|
|
|
FLAGS:
|
|
|
|
-f, --flag tests flags
|
|
|
|
|
|
|
|
OPTIONS:
|
|
|
|
-o, --option <scoption>... tests options
|
|
|
|
|
|
|
|
ARGS:
|
2016-05-30 09:39:54 +00:00
|
|
|
<scpositional> tests positionals";
|
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]
|
2016-05-09 03:20:50 +00:00
|
|
|
fn subcommand_short_help() {
|
2016-05-10 02:46:09 +00:00
|
|
|
let m = test::complex_app()
|
2016-05-09 03:20:50 +00:00
|
|
|
.get_matches_from_safe(vec!["clap-test", "subcmd", "-h"]);
|
2015-09-09 02:39:31 +00:00
|
|
|
|
2016-05-09 03:20:50 +00:00
|
|
|
assert!(m.is_err());
|
|
|
|
assert_eq!(m.unwrap_err().kind, ErrorKind::HelpDisplayed);
|
|
|
|
}
|
2015-09-09 02:39:31 +00:00
|
|
|
|
2016-05-09 03:20:50 +00:00
|
|
|
#[test]
|
|
|
|
fn subcommand_long_help() {
|
2016-05-10 02:46:09 +00:00
|
|
|
let m = test::complex_app()
|
2016-05-09 03:20:50 +00:00
|
|
|
.get_matches_from_safe(vec!["clap-test", "subcmd", "--help"]);
|
2015-09-09 02:39:31 +00:00
|
|
|
|
2016-05-09 03:20:50 +00:00
|
|
|
assert!(m.is_err());
|
|
|
|
assert_eq!(m.unwrap_err().kind, ErrorKind::HelpDisplayed);
|
|
|
|
}
|
2015-09-09 02:39:31 +00:00
|
|
|
|
2016-05-09 03:20:50 +00:00
|
|
|
#[test]
|
|
|
|
fn subcommand_help_rev() {
|
2016-05-10 02:46:09 +00:00
|
|
|
let m = test::complex_app()
|
2016-05-09 03:20:50 +00:00
|
|
|
.get_matches_from_safe(vec!["clap-test", "help", "subcmd"]);
|
|
|
|
|
|
|
|
assert!(m.is_err());
|
|
|
|
assert_eq!(m.unwrap_err().kind, ErrorKind::HelpDisplayed);
|
2015-09-09 02:39:31 +00:00
|
|
|
}
|
2016-02-02 08:13:43 +00:00
|
|
|
|
|
|
|
#[test]
|
2016-05-09 03:20:50 +00:00
|
|
|
fn complex_help_output() {
|
2016-05-10 02:46:09 +00:00
|
|
|
test::check_help(test::complex_app(), HELP);
|
2016-05-09 03:20:50 +00:00
|
|
|
}
|
2016-02-02 08:13:43 +00:00
|
|
|
|
2016-05-30 10:01:00 +00:00
|
|
|
#[test]
|
|
|
|
fn after_and_before_help_output() {
|
|
|
|
let app = App::new("clap-test")
|
|
|
|
.version("v1.4.8")
|
|
|
|
.about("tests clap library")
|
|
|
|
.before_help("some text that comes before the help")
|
|
|
|
.after_help("some text that comes after the help");
|
|
|
|
test::check_help(app, AFTER_HELP);
|
|
|
|
}
|
|
|
|
|
2016-05-09 03:20:50 +00:00
|
|
|
#[test]
|
|
|
|
fn complex_subcommand_help_output() {
|
2016-05-10 02:46:09 +00:00
|
|
|
let mut a = test::complex_app();
|
2016-05-09 03:20:50 +00:00
|
|
|
let _ = a.get_matches_from_safe_borrow(vec![""]);
|
|
|
|
let sc = a.p.subcommands.iter().filter(|s| s.p.meta.name == "subcmd").next().unwrap();
|
2016-02-02 08:13:43 +00:00
|
|
|
// Now we check the output of print_help()
|
|
|
|
let mut help = vec![];
|
2016-05-09 03:20:50 +00:00
|
|
|
sc.write_help(&mut help).ok().expect("failed to print help");
|
|
|
|
assert_eq!(&*String::from_utf8(help).unwrap(), SC_HELP);
|
2016-02-02 08:13:43 +00:00
|
|
|
}
|