2016-05-09 03:20:50 +00:00
|
|
|
extern crate clap_test;
|
2016-01-13 16:06:00 +00:00
|
|
|
extern crate clap;
|
|
|
|
|
|
|
|
use clap::{App, Arg, SubCommand};
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn subcommand() {
|
|
|
|
let m = App::new("test")
|
|
|
|
.subcommand(SubCommand::with_name("some")
|
|
|
|
.arg(Arg::with_name("test")
|
|
|
|
.short("t")
|
|
|
|
.long("test")
|
|
|
|
.takes_value(true)
|
|
|
|
.help("testing testing")))
|
|
|
|
.arg(Arg::with_name("other").long("other"))
|
2016-01-21 05:18:53 +00:00
|
|
|
.get_matches_from(vec!["myprog", "some", "--test", "testing"]);
|
2016-01-13 16:06:00 +00:00
|
|
|
|
|
|
|
assert_eq!(m.subcommand_name().unwrap(), "some");
|
|
|
|
let sub_m = m.subcommand_matches("some").unwrap();
|
|
|
|
assert!(sub_m.is_present("test"));
|
|
|
|
assert_eq!(sub_m.value_of("test").unwrap(), "testing");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn subcommand_none_given() {
|
|
|
|
let m = App::new("test")
|
|
|
|
.subcommand(SubCommand::with_name("some")
|
|
|
|
.arg(Arg::with_name("test")
|
|
|
|
.short("t")
|
|
|
|
.long("test")
|
|
|
|
.takes_value(true)
|
|
|
|
.help("testing testing")))
|
|
|
|
.arg(Arg::with_name("other").long("other"))
|
|
|
|
.get_matches_from(vec![""]);
|
|
|
|
|
|
|
|
assert!(m.subcommand_name().is_none());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn subcommand_multiple() {
|
|
|
|
let m = App::new("test")
|
|
|
|
.subcommands(vec![
|
|
|
|
SubCommand::with_name("some")
|
|
|
|
.arg(Arg::with_name("test")
|
|
|
|
.short("t")
|
|
|
|
.long("test")
|
|
|
|
.takes_value(true)
|
|
|
|
.help("testing testing")),
|
|
|
|
SubCommand::with_name("add")
|
|
|
|
.arg(Arg::with_name("roster").short("r"))
|
|
|
|
])
|
|
|
|
.arg(Arg::with_name("other").long("other"))
|
2016-01-21 05:18:53 +00:00
|
|
|
.get_matches_from(vec!["myprog", "some", "--test", "testing"]);
|
2016-01-13 16:06:00 +00:00
|
|
|
|
|
|
|
assert!(m.subcommand_matches("some").is_some());
|
|
|
|
assert!(m.subcommand_matches("add").is_none());
|
|
|
|
assert_eq!(m.subcommand_name().unwrap(), "some");
|
|
|
|
let sub_m = m.subcommand_matches("some").unwrap();
|
|
|
|
assert!(sub_m.is_present("test"));
|
|
|
|
assert_eq!(sub_m.value_of("test").unwrap(), "testing");
|
|
|
|
}
|
|
|
|
|
2016-05-09 03:20:50 +00:00
|
|
|
#[test]
|
|
|
|
fn subcmd_did_you_mean_output() {
|
|
|
|
clap_test::check_err_output(clap_test::complex_app(), "clap-test subcm",
|
|
|
|
"error: The subcommand 'subcm' wasn't recognized
|
|
|
|
\tDid you mean 'subcmd' ?
|
|
|
|
|
|
|
|
If you believe you received this message in error, try re-running with 'clap-test -- subcm'
|
|
|
|
|
|
|
|
USAGE:
|
|
|
|
clap-test [FLAGS] [OPTIONS] [ARGS] [SUBCOMMAND]
|
|
|
|
|
|
|
|
For more information try --help", true);
|
|
|
|
}
|