2015-08-27 21:03:45 +00:00
|
|
|
extern crate clap;
|
|
|
|
|
2015-09-06 19:11:18 +00:00
|
|
|
use clap::{App, Arg, SubCommand, AppSettings, ClapErrorType};
|
2015-08-27 21:03:45 +00:00
|
|
|
|
|
|
|
#[test]
|
2015-10-01 03:52:24 +00:00
|
|
|
fn sub_command_negate_required() {
|
2015-08-27 21:03:45 +00:00
|
|
|
App::new("sub_command_negate")
|
|
|
|
.setting(AppSettings::SubcommandsNegateReqs)
|
|
|
|
.arg(Arg::with_name("test")
|
|
|
|
.required(true)
|
|
|
|
.index(1))
|
|
|
|
.subcommand(SubCommand::with_name("sub1"))
|
|
|
|
.get_matches_from(vec!["", "sub1"]);
|
2015-09-04 17:16:24 +00:00
|
|
|
}
|
|
|
|
|
2015-09-06 19:11:18 +00:00
|
|
|
#[test]
|
2015-10-01 03:52:24 +00:00
|
|
|
fn sub_command_negate_required_2() {
|
2015-09-06 19:11:18 +00:00
|
|
|
let result = App::new("sub_command_negate")
|
|
|
|
.setting(AppSettings::SubcommandsNegateReqs)
|
|
|
|
.arg(Arg::with_name("test")
|
|
|
|
.required(true)
|
|
|
|
.index(1))
|
|
|
|
.subcommand(SubCommand::with_name("sub1"))
|
|
|
|
.get_matches_from_safe(vec![""]);
|
|
|
|
assert!(result.is_err());
|
|
|
|
let err = result.err().unwrap();
|
|
|
|
assert_eq!(err.error_type, ClapErrorType::MissingRequiredArgument);
|
|
|
|
}
|
|
|
|
|
2015-10-01 03:52:24 +00:00
|
|
|
#[test]
|
|
|
|
fn sub_command_required() {
|
|
|
|
let result = App::new("sc_required")
|
|
|
|
.setting(AppSettings::SubcommandRequired)
|
|
|
|
.subcommand(SubCommand::with_name("sub1"))
|
|
|
|
.get_matches_from_safe(vec![""]);
|
|
|
|
assert!(result.is_err());
|
|
|
|
let err = result.err().unwrap();
|
|
|
|
assert_eq!(err.error_type, ClapErrorType::MissingSubcommand);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn arg_required_else_help() {
|
|
|
|
let result = App::new("arg_required")
|
|
|
|
.setting(AppSettings::ArgRequiredElseHelp)
|
|
|
|
.arg(Arg::with_name("test")
|
|
|
|
.required(true)
|
|
|
|
.index(1))
|
|
|
|
.get_matches_from_safe(vec![""]);
|
|
|
|
assert!(result.is_err());
|
|
|
|
let err = result.err().unwrap();
|
|
|
|
assert_eq!(err.error_type, ClapErrorType::MissingRequiredArgument);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn unified_help() {
|
|
|
|
let mut app = App::new("test")
|
|
|
|
.author("Kevin K.")
|
|
|
|
.about("tests stuff")
|
|
|
|
.version("1.3")
|
|
|
|
.setting(AppSettings::UnifiedHelpMessage)
|
|
|
|
.args_from_usage("-f, --flag 'some flag'
|
|
|
|
[arg1] 'some pos arg'
|
|
|
|
--option [opt] 'some option'");
|
|
|
|
// 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:
|
|
|
|
\ttest [OPTIONS] [ARGS]
|
|
|
|
|
|
|
|
OPTIONS:
|
|
|
|
-f, --flag some flag
|
|
|
|
-h, --help Prints help information
|
|
|
|
--option <opt> some option
|
2015-10-06 00:37:44 +00:00
|
|
|
-V, --version Prints version information
|
2015-10-01 03:52:24 +00:00
|
|
|
|
|
|
|
ARGS:
|
|
|
|
arg1 some pos arg\n"));
|
|
|
|
}
|
|
|
|
|
2015-09-04 17:16:24 +00:00
|
|
|
#[test]
|
|
|
|
fn app_settings_fromstr() {
|
|
|
|
assert_eq!("subcommandsnegatereqs".parse::<AppSettings>().ok().unwrap(), AppSettings::SubcommandsNegateReqs);
|
|
|
|
assert_eq!("subcommandsrequired".parse::<AppSettings>().ok().unwrap(), AppSettings::SubcommandRequired);
|
|
|
|
assert_eq!("argrequiredelsehelp".parse::<AppSettings>().ok().unwrap(), AppSettings::ArgRequiredElseHelp);
|
|
|
|
assert_eq!("globalversion".parse::<AppSettings>().ok().unwrap(), AppSettings::GlobalVersion);
|
|
|
|
assert_eq!("versionlesssubcommands".parse::<AppSettings>().ok().unwrap(), AppSettings::VersionlessSubcommands);
|
|
|
|
assert_eq!("unifiedhelpmessage".parse::<AppSettings>().ok().unwrap(), AppSettings::UnifiedHelpMessage);
|
|
|
|
assert_eq!("waitonerror".parse::<AppSettings>().ok().unwrap(), AppSettings::WaitOnError);
|
|
|
|
assert_eq!("subcommandrequiredelsehelp".parse::<AppSettings>().ok().unwrap(), AppSettings::SubcommandRequiredElseHelp);
|
2015-10-01 03:52:24 +00:00
|
|
|
assert_eq!("hidden".parse::<AppSettings>().ok().unwrap(), AppSettings::Hidden);
|
2015-09-04 17:16:24 +00:00
|
|
|
assert!("hahahaha".parse::<AppSettings>().is_err());
|
2015-08-27 21:03:45 +00:00
|
|
|
}
|