2015-08-27 21:03:45 +00:00
|
|
|
extern crate clap;
|
2016-12-31 03:40:28 +00:00
|
|
|
extern crate regex;
|
2015-08-27 21:03:45 +00:00
|
|
|
|
2018-11-14 17:05:06 +00:00
|
|
|
use clap::{App, AppSettings, Arg, ErrorKind, Propagation};
|
2015-08-27 21:03:45 +00:00
|
|
|
|
2016-12-31 03:40:28 +00:00
|
|
|
include!("../clap-test.rs");
|
|
|
|
|
2017-11-07 01:15:04 +00:00
|
|
|
static ALLOW_EXT_SC: &'static str = "clap-test v1.4.8
|
|
|
|
|
|
|
|
USAGE:
|
|
|
|
clap-test [SUBCOMMAND]
|
|
|
|
|
|
|
|
FLAGS:
|
|
|
|
-h, --help Prints help information
|
|
|
|
-V, --version Prints version information";
|
|
|
|
|
2016-12-31 03:40:28 +00:00
|
|
|
static DONT_COLLAPSE_ARGS: &'static str = "clap-test v1.4.8
|
|
|
|
|
|
|
|
USAGE:
|
|
|
|
clap-test [arg1] [arg2] [arg3]
|
|
|
|
|
|
|
|
ARGS:
|
|
|
|
<arg1> some
|
|
|
|
<arg2> some
|
2018-03-07 11:23:20 +00:00
|
|
|
<arg3> some
|
|
|
|
|
|
|
|
FLAGS:
|
|
|
|
-h, --help Prints help information
|
|
|
|
-V, --version Prints version information";
|
2016-12-31 03:40:28 +00:00
|
|
|
|
2017-03-17 01:45:45 +00:00
|
|
|
static REQUIRE_EQUALS: &'static str = "clap-test v1.4.8
|
|
|
|
|
|
|
|
USAGE:
|
|
|
|
clap-test --opt=<FILE>
|
|
|
|
|
|
|
|
FLAGS:
|
|
|
|
-h, --help Prints help information
|
|
|
|
-V, --version Prints version information
|
|
|
|
|
|
|
|
OPTIONS:
|
|
|
|
-o, --opt=<FILE> some";
|
|
|
|
|
2017-01-03 04:05:23 +00:00
|
|
|
static UNIFIED_HELP: &'static str = "test 1.3
|
|
|
|
Kevin K.
|
|
|
|
tests stuff
|
|
|
|
|
|
|
|
USAGE:
|
|
|
|
test [OPTIONS] [arg1]
|
|
|
|
|
2018-03-07 11:23:20 +00:00
|
|
|
ARGS:
|
|
|
|
<arg1> some pos arg
|
|
|
|
|
2017-01-03 04:05:23 +00:00
|
|
|
OPTIONS:
|
|
|
|
-f, --flag some flag
|
|
|
|
-h, --help Prints help information
|
|
|
|
--option <opt> some option
|
2018-03-07 11:23:20 +00:00
|
|
|
-V, --version Prints version information";
|
2017-01-03 04:05:23 +00:00
|
|
|
|
|
|
|
static SKIP_POS_VALS: &'static str = "test 1.3
|
|
|
|
Kevin K.
|
|
|
|
tests stuff
|
|
|
|
|
|
|
|
USAGE:
|
|
|
|
test [OPTIONS] [arg1]
|
|
|
|
|
2018-03-07 11:23:20 +00:00
|
|
|
ARGS:
|
|
|
|
<arg1> some pos arg
|
|
|
|
|
2017-01-03 04:05:23 +00:00
|
|
|
FLAGS:
|
|
|
|
-h, --help Prints help information
|
|
|
|
-V, --version Prints version information
|
|
|
|
|
|
|
|
OPTIONS:
|
2018-03-07 11:23:20 +00:00
|
|
|
-o, --opt <opt> some option";
|
2017-01-03 04:05:23 +00:00
|
|
|
|
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)
|
2018-01-25 04:05:05 +00:00
|
|
|
.arg(Arg::with_name("test").required(true).index(1))
|
2018-10-19 20:42:13 +00:00
|
|
|
.subcommand(App::new("sub1"))
|
2016-01-21 05:18:53 +00:00
|
|
|
.get_matches_from(vec!["myprog", "sub1"]);
|
2015-09-04 17:16:24 +00:00
|
|
|
}
|
|
|
|
|
2016-02-04 16:55:58 +00:00
|
|
|
#[test]
|
|
|
|
fn global_version() {
|
2016-09-05 21:16:40 +00:00
|
|
|
let mut app = App::new("global_version")
|
2016-02-04 16:55:58 +00:00
|
|
|
.setting(AppSettings::GlobalVersion)
|
|
|
|
.version("1.1")
|
2018-10-19 20:42:13 +00:00
|
|
|
.subcommand(App::new("sub1"));
|
2018-01-26 03:48:17 +00:00
|
|
|
app._propagate(Propagation::NextLevel);
|
2018-01-25 02:08:14 +00:00
|
|
|
assert_eq!(app.subcommands[0].version, Some("1.1"));
|
2016-02-04 16:55:58 +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)
|
2018-01-25 04:05:05 +00:00
|
|
|
.arg(Arg::with_name("test").required(true).index(1))
|
2018-10-19 20:42:13 +00:00
|
|
|
.subcommand(App::new("sub1"))
|
|
|
|
.try_get_matches_from(vec![""]);
|
2015-09-06 19:11:18 +00:00
|
|
|
assert!(result.is_err());
|
|
|
|
let err = result.err().unwrap();
|
2016-01-21 05:18:53 +00:00
|
|
|
assert_eq!(err.kind, ErrorKind::MissingRequiredArgument);
|
2015-09-06 19:11:18 +00:00
|
|
|
}
|
|
|
|
|
2015-10-01 03:52:24 +00:00
|
|
|
#[test]
|
|
|
|
fn sub_command_required() {
|
|
|
|
let result = App::new("sc_required")
|
|
|
|
.setting(AppSettings::SubcommandRequired)
|
2018-10-19 20:42:13 +00:00
|
|
|
.subcommand(App::new("sub1"))
|
|
|
|
.try_get_matches_from(vec![""]);
|
2015-10-01 03:52:24 +00:00
|
|
|
assert!(result.is_err());
|
|
|
|
let err = result.err().unwrap();
|
2016-01-21 05:18:53 +00:00
|
|
|
assert_eq!(err.kind, ErrorKind::MissingSubcommand);
|
2015-10-01 03:52:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn arg_required_else_help() {
|
|
|
|
let result = App::new("arg_required")
|
|
|
|
.setting(AppSettings::ArgRequiredElseHelp)
|
2018-01-25 04:05:05 +00:00
|
|
|
.arg(Arg::with_name("test").index(1))
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec![""]);
|
2015-10-01 03:52:24 +00:00
|
|
|
assert!(result.is_err());
|
|
|
|
let err = result.err().unwrap();
|
2016-01-21 05:18:53 +00:00
|
|
|
assert_eq!(err.kind, ErrorKind::MissingArgumentOrSubcommand);
|
2015-10-01 03:52:24 +00:00
|
|
|
}
|
|
|
|
|
2017-03-12 16:53:40 +00:00
|
|
|
#[test]
|
|
|
|
fn arg_required_else_help_over_reqs() {
|
|
|
|
let result = App::new("arg_required")
|
|
|
|
.setting(AppSettings::ArgRequiredElseHelp)
|
2018-01-25 04:05:05 +00:00
|
|
|
.arg(Arg::with_name("test").index(1).required(true))
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec![""]);
|
2017-03-12 16:53:40 +00:00
|
|
|
assert!(result.is_err());
|
|
|
|
let err = result.err().unwrap();
|
|
|
|
assert_eq!(err.kind, ErrorKind::MissingArgumentOrSubcommand);
|
|
|
|
}
|
|
|
|
|
2017-03-10 02:20:19 +00:00
|
|
|
#[cfg(not(feature = "suggestions"))]
|
2017-03-02 15:58:06 +00:00
|
|
|
#[test]
|
|
|
|
fn infer_subcommands_fail_no_args() {
|
|
|
|
let m = App::new("prog")
|
|
|
|
.setting(AppSettings::InferSubcommands)
|
2018-10-19 20:42:13 +00:00
|
|
|
.subcommand(App::new("test"))
|
|
|
|
.subcommand(App::new("temp"))
|
|
|
|
.try_get_matches_from(vec!["prog", "te"]);
|
2017-03-10 02:20:19 +00:00
|
|
|
assert!(m.is_err(), "{:#?}", m.unwrap());
|
|
|
|
assert_eq!(m.unwrap_err().kind, ErrorKind::UnrecognizedSubcommand);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(feature = "suggestions")]
|
|
|
|
#[test]
|
|
|
|
fn infer_subcommands_fail_no_args() {
|
|
|
|
let m = App::new("prog")
|
|
|
|
.setting(AppSettings::InferSubcommands)
|
2018-10-19 20:42:13 +00:00
|
|
|
.subcommand(App::new("test"))
|
|
|
|
.subcommand(App::new("temp"))
|
|
|
|
.try_get_matches_from(vec!["prog", "te"]);
|
2017-03-10 02:20:19 +00:00
|
|
|
assert!(m.is_err(), "{:#?}", m.unwrap());
|
2017-03-02 15:58:06 +00:00
|
|
|
assert_eq!(m.unwrap_err().kind, ErrorKind::InvalidSubcommand);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn infer_subcommands_fail_with_args() {
|
|
|
|
let m = App::new("prog")
|
|
|
|
.setting(AppSettings::InferSubcommands)
|
|
|
|
.arg(Arg::with_name("some"))
|
2018-10-19 20:42:13 +00:00
|
|
|
.subcommand(App::new("test"))
|
|
|
|
.subcommand(App::new("temp"))
|
|
|
|
.try_get_matches_from(vec!["prog", "t"]);
|
2017-03-02 15:58:06 +00:00
|
|
|
assert!(m.is_ok(), "{:?}", m.unwrap_err().kind);
|
|
|
|
assert_eq!(m.unwrap().value_of("some"), Some("t"));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn infer_subcommands_fail_with_args2() {
|
|
|
|
let m = App::new("prog")
|
|
|
|
.setting(AppSettings::InferSubcommands)
|
|
|
|
.arg(Arg::with_name("some"))
|
2018-10-19 20:42:13 +00:00
|
|
|
.subcommand(App::new("test"))
|
|
|
|
.subcommand(App::new("temp"))
|
|
|
|
.try_get_matches_from(vec!["prog", "te"]);
|
2017-03-02 15:58:06 +00:00
|
|
|
assert!(m.is_ok(), "{:?}", m.unwrap_err().kind);
|
|
|
|
assert_eq!(m.unwrap().value_of("some"), Some("te"));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn infer_subcommands_pass() {
|
|
|
|
let m = App::new("prog")
|
|
|
|
.setting(AppSettings::InferSubcommands)
|
2018-10-19 20:42:13 +00:00
|
|
|
.subcommand(App::new("test"))
|
2018-01-25 04:05:05 +00:00
|
|
|
.get_matches_from(vec!["prog", "te"]);
|
2017-03-02 15:58:06 +00:00
|
|
|
assert_eq!(m.subcommand_name(), Some("test"));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn infer_subcommands_pass_close() {
|
|
|
|
let m = App::new("prog")
|
|
|
|
.setting(AppSettings::InferSubcommands)
|
2018-10-19 20:42:13 +00:00
|
|
|
.subcommand(App::new("test"))
|
|
|
|
.subcommand(App::new("temp"))
|
2018-01-25 04:05:05 +00:00
|
|
|
.get_matches_from(vec!["prog", "tes"]);
|
2017-03-02 15:58:06 +00:00
|
|
|
assert_eq!(m.subcommand_name(), Some("test"));
|
|
|
|
}
|
|
|
|
|
2017-03-10 02:20:19 +00:00
|
|
|
#[cfg(feature = "suggestions")]
|
2017-03-02 15:58:06 +00:00
|
|
|
#[test]
|
|
|
|
fn infer_subcommands_fail_suggestions() {
|
|
|
|
let m = App::new("prog")
|
|
|
|
.setting(AppSettings::InferSubcommands)
|
2018-10-19 20:42:13 +00:00
|
|
|
.subcommand(App::new("test"))
|
|
|
|
.subcommand(App::new("temp"))
|
|
|
|
.try_get_matches_from(vec!["prog", "temps"]);
|
2017-03-10 02:20:19 +00:00
|
|
|
assert!(m.is_err(), "{:#?}", m.unwrap());
|
2017-03-02 15:58:06 +00:00
|
|
|
assert_eq!(m.unwrap_err().kind, ErrorKind::InvalidSubcommand);
|
|
|
|
}
|
|
|
|
|
2017-03-10 02:20:19 +00:00
|
|
|
#[cfg(not(feature = "suggestions"))]
|
|
|
|
#[test]
|
|
|
|
fn infer_subcommands_fail_suggestions() {
|
|
|
|
let m = App::new("prog")
|
|
|
|
.setting(AppSettings::InferSubcommands)
|
2018-10-19 20:42:13 +00:00
|
|
|
.subcommand(App::new("test"))
|
|
|
|
.subcommand(App::new("temp"))
|
|
|
|
.try_get_matches_from(vec!["prog", "temps"]);
|
2017-03-10 02:20:19 +00:00
|
|
|
assert!(m.is_err(), "{:#?}", m.unwrap());
|
|
|
|
assert_eq!(m.unwrap_err().kind, ErrorKind::UnrecognizedSubcommand);
|
|
|
|
}
|
|
|
|
|
2015-10-28 10:45:45 +00:00
|
|
|
#[test]
|
|
|
|
fn no_bin_name() {
|
|
|
|
let result = App::new("arg_required")
|
|
|
|
.setting(AppSettings::NoBinaryName)
|
2018-01-25 04:05:05 +00:00
|
|
|
.arg(Arg::with_name("test").required(true).index(1))
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec!["testing"]);
|
2015-10-28 10:45:45 +00:00
|
|
|
assert!(result.is_ok());
|
|
|
|
let matches = result.unwrap();
|
|
|
|
assert_eq!(matches.value_of("test").unwrap(), "testing");
|
|
|
|
}
|
|
|
|
|
2015-10-01 03:52:24 +00:00
|
|
|
#[test]
|
|
|
|
fn unified_help() {
|
2017-03-21 10:00:30 +00:00
|
|
|
let app = App::new("myTest")
|
|
|
|
.name("test")
|
2015-10-01 03:52:24 +00:00
|
|
|
.author("Kevin K.")
|
|
|
|
.about("tests stuff")
|
|
|
|
.version("1.3")
|
|
|
|
.setting(AppSettings::UnifiedHelpMessage)
|
2018-10-19 20:42:13 +00:00
|
|
|
.arg("-f, --flag 'some flag'")
|
|
|
|
.arg("[arg1] 'some pos arg'")
|
|
|
|
.arg("--option [opt] 'some option'");
|
2015-10-01 03:52:24 +00:00
|
|
|
|
2018-01-25 04:05:05 +00:00
|
|
|
assert!(test::compare_output(
|
|
|
|
app,
|
|
|
|
"test --help",
|
|
|
|
UNIFIED_HELP,
|
|
|
|
false
|
|
|
|
));
|
2015-10-01 03:52:24 +00:00
|
|
|
}
|
|
|
|
|
2016-02-02 08:13:43 +00:00
|
|
|
#[test]
|
|
|
|
fn skip_possible_values() {
|
2017-01-03 04:05:23 +00:00
|
|
|
let app = App::new("test")
|
2016-02-02 08:13:43 +00:00
|
|
|
.author("Kevin K.")
|
|
|
|
.about("tests stuff")
|
|
|
|
.version("1.3")
|
|
|
|
.setting(AppSettings::HidePossibleValuesInHelp)
|
2018-01-25 04:05:05 +00:00
|
|
|
.args(&[
|
2018-04-21 18:59:19 +00:00
|
|
|
Arg::from("-o, --opt [opt] 'some option'").possible_values(&["one", "two"]),
|
|
|
|
Arg::from("[arg1] 'some pos arg'").possible_values(&["three", "four"]),
|
2018-01-25 04:05:05 +00:00
|
|
|
]);
|
2016-02-02 08:13:43 +00:00
|
|
|
|
2018-01-25 04:05:05 +00:00
|
|
|
assert!(test::compare_output(
|
|
|
|
app,
|
|
|
|
"test --help",
|
|
|
|
SKIP_POS_VALS,
|
|
|
|
false
|
|
|
|
));
|
2016-02-02 08:13:43 +00:00
|
|
|
}
|
2016-06-04 02:45:55 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn global_setting() {
|
2016-09-05 21:16:40 +00:00
|
|
|
let mut app = App::new("test")
|
2016-06-04 02:45:55 +00:00
|
|
|
.global_setting(AppSettings::ColoredHelp)
|
2018-10-19 20:42:13 +00:00
|
|
|
.subcommand(App::new("subcmd"));
|
2018-01-26 03:48:17 +00:00
|
|
|
app._propagate(Propagation::NextLevel);
|
2018-11-14 17:05:06 +00:00
|
|
|
assert!(app
|
|
|
|
.subcommands
|
|
|
|
.iter()
|
|
|
|
.filter(|s| s.name == "subcmd")
|
|
|
|
.next()
|
|
|
|
.unwrap()
|
|
|
|
.is_set(AppSettings::ColoredHelp));
|
2016-06-04 02:45:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn global_settings() {
|
2016-09-05 21:16:40 +00:00
|
|
|
let mut app = App::new("test")
|
2018-10-19 20:42:13 +00:00
|
|
|
.global_setting(AppSettings::ColoredHelp)
|
|
|
|
.global_setting(AppSettings::TrailingVarArg)
|
|
|
|
.subcommand(App::new("subcmd"));
|
2018-01-26 03:48:17 +00:00
|
|
|
app._propagate(Propagation::NextLevel);
|
2018-11-14 17:05:06 +00:00
|
|
|
assert!(app
|
|
|
|
.subcommands
|
|
|
|
.iter()
|
|
|
|
.filter(|s| s.name == "subcmd")
|
|
|
|
.next()
|
|
|
|
.unwrap()
|
|
|
|
.is_set(AppSettings::ColoredHelp));
|
|
|
|
assert!(app
|
|
|
|
.subcommands
|
|
|
|
.iter()
|
|
|
|
.filter(|s| s.name == "subcmd")
|
|
|
|
.next()
|
|
|
|
.unwrap()
|
|
|
|
.is_set(AppSettings::TrailingVarArg));
|
2016-06-04 02:45:55 +00:00
|
|
|
}
|
2016-06-05 09:21:52 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn stop_delim_values_only_pos_follows() {
|
|
|
|
let r = App::new("onlypos")
|
|
|
|
.setting(AppSettings::DontDelimitTrailingValues)
|
2018-01-25 04:05:05 +00:00
|
|
|
.args(&[
|
2018-04-21 18:59:19 +00:00
|
|
|
Arg::from("-f [flag] 'some opt'"),
|
|
|
|
Arg::from("[arg]... 'some arg'"),
|
2018-01-25 04:05:05 +00:00
|
|
|
])
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec!["", "--", "-f", "-g,x"]);
|
2016-06-05 09:21:52 +00:00
|
|
|
assert!(r.is_ok());
|
|
|
|
let m = r.unwrap();
|
|
|
|
assert!(m.is_present("arg"));
|
|
|
|
assert!(!m.is_present("f"));
|
2018-01-25 04:05:05 +00:00
|
|
|
assert_eq!(
|
|
|
|
m.values_of("arg").unwrap().collect::<Vec<_>>(),
|
|
|
|
&["-f", "-g,x"]
|
|
|
|
);
|
2016-06-05 09:21:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn dont_delim_values_trailingvararg() {
|
|
|
|
let m = App::new("positional")
|
|
|
|
.setting(AppSettings::TrailingVarArg)
|
|
|
|
.setting(AppSettings::DontDelimitTrailingValues)
|
2018-04-21 18:59:19 +00:00
|
|
|
.arg(Arg::from("[opt]... 'some pos'"))
|
2016-06-05 09:21:52 +00:00
|
|
|
.get_matches_from(vec!["", "test", "--foo", "-Wl,-bar"]);
|
|
|
|
assert!(m.is_present("opt"));
|
2018-01-25 04:05:05 +00:00
|
|
|
assert_eq!(
|
|
|
|
m.values_of("opt").unwrap().collect::<Vec<_>>(),
|
|
|
|
&["test", "--foo", "-Wl,-bar"]
|
|
|
|
);
|
2016-06-05 09:21:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn delim_values_only_pos_follows() {
|
|
|
|
let r = App::new("onlypos")
|
2018-01-25 04:05:05 +00:00
|
|
|
.args(&[
|
2018-04-21 18:59:19 +00:00
|
|
|
Arg::from("-f [flag] 'some opt'"),
|
|
|
|
Arg::from("[arg]... 'some arg'"),
|
2018-01-25 04:05:05 +00:00
|
|
|
])
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec!["", "--", "-f", "-g,x"]);
|
2016-06-05 09:21:52 +00:00
|
|
|
assert!(r.is_ok());
|
|
|
|
let m = r.unwrap();
|
|
|
|
assert!(m.is_present("arg"));
|
|
|
|
assert!(!m.is_present("f"));
|
2018-01-25 04:05:05 +00:00
|
|
|
assert_eq!(
|
|
|
|
m.values_of("arg").unwrap().collect::<Vec<_>>(),
|
|
|
|
&["-f", "-g,x"]
|
|
|
|
);
|
2016-06-05 09:21:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn delim_values_trailingvararg() {
|
|
|
|
let m = App::new("positional")
|
|
|
|
.setting(AppSettings::TrailingVarArg)
|
2018-04-21 18:59:19 +00:00
|
|
|
.arg(Arg::from("[opt]... 'some pos'"))
|
2016-06-05 09:21:52 +00:00
|
|
|
.get_matches_from(vec!["", "test", "--foo", "-Wl,-bar"]);
|
|
|
|
assert!(m.is_present("opt"));
|
2018-01-25 04:05:05 +00:00
|
|
|
assert_eq!(
|
|
|
|
m.values_of("opt").unwrap().collect::<Vec<_>>(),
|
|
|
|
&["test", "--foo", "-Wl,-bar"]
|
|
|
|
);
|
2016-09-10 22:18:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn delim_values_only_pos_follows_with_delim() {
|
|
|
|
let r = App::new("onlypos")
|
2018-01-25 04:05:05 +00:00
|
|
|
.args(&[
|
2018-04-21 18:59:19 +00:00
|
|
|
Arg::from("-f [flag] 'some opt'"),
|
|
|
|
Arg::from("[arg]... 'some arg'").use_delimiter(true),
|
2018-01-25 04:05:05 +00:00
|
|
|
])
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec!["", "--", "-f", "-g,x"]);
|
2016-09-10 22:18:43 +00:00
|
|
|
assert!(r.is_ok());
|
|
|
|
let m = r.unwrap();
|
|
|
|
assert!(m.is_present("arg"));
|
|
|
|
assert!(!m.is_present("f"));
|
2018-01-25 04:05:05 +00:00
|
|
|
assert_eq!(
|
|
|
|
m.values_of("arg").unwrap().collect::<Vec<_>>(),
|
|
|
|
&["-f", "-g", "x"]
|
|
|
|
);
|
2016-09-10 22:18:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn delim_values_trailingvararg_with_delim() {
|
|
|
|
let m = App::new("positional")
|
|
|
|
.setting(AppSettings::TrailingVarArg)
|
2018-04-21 18:59:19 +00:00
|
|
|
.arg(Arg::from("[opt]... 'some pos'").use_delimiter(true))
|
2016-09-10 22:18:43 +00:00
|
|
|
.get_matches_from(vec!["", "test", "--foo", "-Wl,-bar"]);
|
|
|
|
assert!(m.is_present("opt"));
|
2018-01-25 04:05:05 +00:00
|
|
|
assert_eq!(
|
|
|
|
m.values_of("opt").unwrap().collect::<Vec<_>>(),
|
|
|
|
&["test", "--foo", "-Wl", "-bar"]
|
|
|
|
);
|
2016-06-05 09:21:52 +00:00
|
|
|
}
|
2016-07-01 17:50:32 +00:00
|
|
|
|
2016-09-13 03:26:49 +00:00
|
|
|
#[test]
|
2016-10-21 13:42:25 +00:00
|
|
|
fn leading_hyphen_short() {
|
2016-09-13 03:26:49 +00:00
|
|
|
let res = App::new("leadhy")
|
|
|
|
.setting(AppSettings::AllowLeadingHyphen)
|
|
|
|
.arg(Arg::with_name("some"))
|
2018-07-23 19:09:42 +00:00
|
|
|
.arg(Arg::with_name("other").short('o'))
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec!["", "-bar", "-o"]);
|
2016-09-13 03:26:49 +00:00
|
|
|
assert!(res.is_ok(), "Error: {:?}", res.unwrap_err().kind);
|
|
|
|
let m = res.unwrap();
|
|
|
|
assert!(m.is_present("some"));
|
|
|
|
assert!(m.is_present("other"));
|
2016-10-21 13:42:25 +00:00
|
|
|
assert_eq!(m.value_of("some").unwrap(), "-bar");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn leading_hyphen_long() {
|
|
|
|
let res = App::new("leadhy")
|
|
|
|
.setting(AppSettings::AllowLeadingHyphen)
|
|
|
|
.arg(Arg::with_name("some"))
|
2018-07-23 19:09:42 +00:00
|
|
|
.arg(Arg::with_name("other").short('o'))
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec!["", "--bar", "-o"]);
|
2016-10-21 13:42:25 +00:00
|
|
|
assert!(res.is_ok(), "Error: {:?}", res.unwrap_err().kind);
|
|
|
|
let m = res.unwrap();
|
|
|
|
assert!(m.is_present("some"));
|
|
|
|
assert!(m.is_present("other"));
|
|
|
|
assert_eq!(m.value_of("some").unwrap(), "--bar");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn leading_hyphen_opt() {
|
|
|
|
let res = App::new("leadhy")
|
|
|
|
.setting(AppSettings::AllowLeadingHyphen)
|
2018-01-25 04:05:05 +00:00
|
|
|
.arg(Arg::with_name("some").takes_value(true).long("opt"))
|
2018-07-23 19:09:42 +00:00
|
|
|
.arg(Arg::with_name("other").short('o'))
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec!["", "--opt", "--bar", "-o"]);
|
2016-10-21 13:42:25 +00:00
|
|
|
assert!(res.is_ok(), "Error: {:?}", res.unwrap_err().kind);
|
|
|
|
let m = res.unwrap();
|
|
|
|
assert!(m.is_present("some"));
|
|
|
|
assert!(m.is_present("other"));
|
|
|
|
assert_eq!(m.value_of("some").unwrap(), "--bar");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn allow_negative_numbers() {
|
|
|
|
let res = App::new("negnum")
|
|
|
|
.setting(AppSettings::AllowNegativeNumbers)
|
|
|
|
.arg(Arg::with_name("panum"))
|
2018-07-23 19:09:42 +00:00
|
|
|
.arg(Arg::with_name("onum").short('o').takes_value(true))
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec!["negnum", "-20", "-o", "-1.2"]);
|
2016-10-21 13:42:25 +00:00
|
|
|
assert!(res.is_ok(), "Error: {:?}", res.unwrap_err().kind);
|
|
|
|
let m = res.unwrap();
|
|
|
|
assert_eq!(m.value_of("panum").unwrap(), "-20");
|
|
|
|
assert_eq!(m.value_of("onum").unwrap(), "-1.2");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn allow_negative_numbers_fail() {
|
|
|
|
let res = App::new("negnum")
|
|
|
|
.setting(AppSettings::AllowNegativeNumbers)
|
|
|
|
.arg(Arg::with_name("panum"))
|
2018-07-23 19:09:42 +00:00
|
|
|
.arg(Arg::with_name("onum").short('o').takes_value(true))
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec!["negnum", "--foo", "-o", "-1.2"]);
|
2016-10-21 13:42:25 +00:00
|
|
|
assert!(res.is_err());
|
|
|
|
assert_eq!(res.unwrap_err().kind, ErrorKind::UnknownArgument)
|
2016-09-13 03:26:49 +00:00
|
|
|
}
|
|
|
|
|
2016-07-01 17:50:32 +00:00
|
|
|
#[test]
|
|
|
|
fn leading_double_hyphen_trailingvararg() {
|
|
|
|
let m = App::new("positional")
|
|
|
|
.setting(AppSettings::TrailingVarArg)
|
|
|
|
.setting(AppSettings::AllowLeadingHyphen)
|
2018-04-21 18:59:19 +00:00
|
|
|
.arg(Arg::from("[opt]... 'some pos'"))
|
2016-07-01 17:50:32 +00:00
|
|
|
.get_matches_from(vec!["", "--foo", "-Wl", "bar"]);
|
|
|
|
assert!(m.is_present("opt"));
|
2018-01-25 04:05:05 +00:00
|
|
|
assert_eq!(
|
|
|
|
m.values_of("opt").unwrap().collect::<Vec<_>>(),
|
|
|
|
&["--foo", "-Wl", "bar"]
|
|
|
|
);
|
2016-07-01 17:50:32 +00:00
|
|
|
}
|
2016-07-24 04:08:24 +00:00
|
|
|
|
|
|
|
#[test]
|
2018-01-25 02:08:14 +00:00
|
|
|
fn unset_setting() {
|
2018-08-02 03:13:51 +00:00
|
|
|
let m = App::new("unset_setting").setting(AppSettings::AllArgsOverrideSelf);
|
2018-03-21 23:41:58 +00:00
|
|
|
assert!(m.is_set(AppSettings::AllArgsOverrideSelf));
|
2016-07-24 04:08:24 +00:00
|
|
|
|
2018-03-21 23:41:58 +00:00
|
|
|
let m = m.unset_setting(AppSettings::AllArgsOverrideSelf);
|
|
|
|
assert!(!m.is_set(AppSettings::AllArgsOverrideSelf));
|
2016-07-24 04:08:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2018-01-25 02:08:14 +00:00
|
|
|
fn unset_settings() {
|
2016-07-24 04:08:24 +00:00
|
|
|
let m = App::new("unset_settings");
|
2018-01-25 02:08:14 +00:00
|
|
|
assert!(&m.is_set(AppSettings::AllowInvalidUtf8));
|
|
|
|
assert!(&m.is_set(AppSettings::ColorAuto));
|
2016-09-05 21:16:40 +00:00
|
|
|
|
2018-11-14 17:05:06 +00:00
|
|
|
let m = m
|
|
|
|
.unset_setting(AppSettings::AllowInvalidUtf8)
|
2018-10-19 20:42:13 +00:00
|
|
|
.unset_setting(AppSettings::ColorAuto);
|
2018-11-14 17:05:06 +00:00
|
|
|
assert!(
|
|
|
|
!m.is_set(AppSettings::AllowInvalidUtf8),
|
|
|
|
"l: {:?}\ng:{:?}",
|
|
|
|
m.settings,
|
|
|
|
m.g_settings
|
|
|
|
);
|
2018-01-25 02:08:14 +00:00
|
|
|
assert!(!m.is_set(AppSettings::ColorAuto));
|
2016-07-24 04:08:24 +00:00
|
|
|
}
|
2016-12-31 02:53:58 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn disable_help_subcommand() {
|
|
|
|
let result = App::new("disablehelp")
|
|
|
|
.setting(AppSettings::DisableHelpSubcommand)
|
2018-10-19 20:42:13 +00:00
|
|
|
.subcommand(App::new("sub1"))
|
|
|
|
.try_get_matches_from(vec!["", "help"]);
|
2016-12-31 02:53:58 +00:00
|
|
|
assert!(result.is_err());
|
|
|
|
let err = result.err().unwrap();
|
|
|
|
assert_eq!(err.kind, ErrorKind::UnknownArgument);
|
2016-12-31 03:40:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn dont_collapse_args() {
|
|
|
|
let app = App::new("clap-test")
|
|
|
|
.version("v1.4.8")
|
|
|
|
.setting(AppSettings::DontCollapseArgsInUsage)
|
|
|
|
.args(&[
|
|
|
|
Arg::with_name("arg1").help("some"),
|
|
|
|
Arg::with_name("arg2").help("some"),
|
|
|
|
Arg::with_name("arg3").help("some"),
|
|
|
|
]);
|
2018-01-25 04:05:05 +00:00
|
|
|
assert!(test::compare_output(
|
|
|
|
app,
|
|
|
|
"clap-test --help",
|
|
|
|
DONT_COLLAPSE_ARGS,
|
|
|
|
false
|
|
|
|
));
|
2016-12-31 04:22:09 +00:00
|
|
|
}
|
|
|
|
|
2017-03-17 01:45:45 +00:00
|
|
|
#[test]
|
|
|
|
fn require_eq() {
|
2018-01-25 04:05:05 +00:00
|
|
|
let app = App::new("clap-test").version("v1.4.8").arg(
|
|
|
|
Arg::with_name("opt")
|
2017-03-17 01:45:45 +00:00
|
|
|
.long("opt")
|
2018-07-23 19:09:42 +00:00
|
|
|
.short('o')
|
2017-03-17 01:45:45 +00:00
|
|
|
.required(true)
|
|
|
|
.require_equals(true)
|
|
|
|
.value_name("FILE")
|
|
|
|
.help("some"),
|
2018-01-25 04:05:05 +00:00
|
|
|
);
|
|
|
|
assert!(test::compare_output(
|
|
|
|
app,
|
|
|
|
"clap-test --help",
|
|
|
|
REQUIRE_EQUALS,
|
|
|
|
false
|
|
|
|
));
|
2017-03-17 01:45:45 +00:00
|
|
|
}
|
|
|
|
|
2016-12-31 04:22:09 +00:00
|
|
|
#[test]
|
|
|
|
fn args_negate_subcommands_one_level() {
|
2018-11-14 17:05:06 +00:00
|
|
|
let res = App::new("disablehelp")
|
|
|
|
.setting(AppSettings::ArgsNegateSubcommands)
|
|
|
|
.setting(AppSettings::SubcommandsNegateReqs)
|
|
|
|
.arg("<arg1> 'some arg'")
|
|
|
|
.arg("<arg2> 'some arg'")
|
|
|
|
.subcommand(App::new("sub1").subcommand(App::new("sub2").subcommand(App::new("sub3"))))
|
|
|
|
.try_get_matches_from(vec!["", "pickles", "sub1"]);
|
2016-12-31 04:22:09 +00:00
|
|
|
assert!(res.is_ok(), "error: {:?}", res.unwrap_err().kind);
|
|
|
|
let m = res.unwrap();
|
|
|
|
assert_eq!(m.value_of("arg2"), Some("sub1"));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn args_negate_subcommands_two_levels() {
|
|
|
|
let res = App::new("disablehelp")
|
|
|
|
.global_setting(AppSettings::ArgsNegateSubcommands)
|
|
|
|
.global_setting(AppSettings::SubcommandsNegateReqs)
|
2018-10-19 20:42:13 +00:00
|
|
|
.arg("<arg1> 'some arg'")
|
|
|
|
.arg("<arg2> 'some arg'")
|
2018-01-25 04:05:05 +00:00
|
|
|
.subcommand(
|
2018-10-19 20:42:13 +00:00
|
|
|
App::new("sub1")
|
|
|
|
.arg("<arg> 'some'")
|
|
|
|
.arg("<arg2> 'some'")
|
2018-11-14 17:05:06 +00:00
|
|
|
.subcommand(App::new("sub2").subcommand(App::new("sub3"))),
|
2016-12-31 04:22:09 +00:00
|
|
|
)
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec!["", "sub1", "arg", "sub2"]);
|
2016-12-31 04:22:09 +00:00
|
|
|
assert!(res.is_ok(), "error: {:?}", res.unwrap_err().kind);
|
|
|
|
let m = res.unwrap();
|
2018-01-25 04:05:05 +00:00
|
|
|
assert_eq!(
|
|
|
|
m.subcommand_matches("sub1").unwrap().value_of("arg2"),
|
|
|
|
Some("sub2")
|
|
|
|
);
|
2017-01-03 04:04:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn propagate_vals_down() {
|
|
|
|
let m = App::new("myprog")
|
2018-04-21 18:59:19 +00:00
|
|
|
.arg(Arg::from("[cmd] 'command to run'").global(true))
|
2018-10-19 20:42:13 +00:00
|
|
|
.subcommand(App::new("foo"))
|
|
|
|
.try_get_matches_from(vec!["myprog", "set", "foo"]);
|
2017-03-02 15:58:06 +00:00
|
|
|
assert!(m.is_ok(), "{:?}", m.unwrap_err().kind);
|
|
|
|
let m = m.unwrap();
|
2017-01-03 04:04:48 +00:00
|
|
|
assert_eq!(m.value_of("cmd"), Some("set"));
|
|
|
|
let sub_m = m.subcommand_matches("foo").unwrap();
|
|
|
|
assert_eq!(sub_m.value_of("cmd"), Some("set"));
|
2017-01-04 02:58:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn allow_missing_positional() {
|
|
|
|
let m = App::new("test")
|
|
|
|
.setting(AppSettings::AllowMissingPositional)
|
2018-04-21 18:59:19 +00:00
|
|
|
.arg(Arg::from("[src] 'some file'").default_value("src"))
|
2018-10-19 20:42:13 +00:00
|
|
|
.arg("<dest> 'some file'")
|
|
|
|
.try_get_matches_from(vec!["test", "file"]);
|
2017-03-02 15:58:06 +00:00
|
|
|
assert!(m.is_ok(), "{:?}", m.unwrap_err().kind);
|
|
|
|
let m = m.unwrap();
|
2017-01-04 02:58:12 +00:00
|
|
|
assert_eq!(m.value_of("src"), Some("src"));
|
|
|
|
assert_eq!(m.value_of("dest"), Some("file"));
|
2017-03-21 10:00:30 +00:00
|
|
|
}
|
2017-10-24 22:21:52 +00:00
|
|
|
|
2018-03-06 15:39:58 +00:00
|
|
|
#[test]
|
|
|
|
fn allow_missing_positional_no_default() {
|
|
|
|
let m = App::new("test")
|
|
|
|
.setting(AppSettings::AllowMissingPositional)
|
2018-04-21 18:59:19 +00:00
|
|
|
.arg(Arg::from("[src] 'some file'"))
|
2018-10-19 20:42:13 +00:00
|
|
|
.arg("<dest> 'some file'")
|
|
|
|
.try_get_matches_from(vec!["test", "file"]);
|
2018-03-06 15:39:58 +00:00
|
|
|
assert!(m.is_ok(), "{:?}", m.unwrap_err().kind);
|
|
|
|
let m = m.unwrap();
|
|
|
|
assert_eq!(m.value_of("src"), None);
|
|
|
|
assert_eq!(m.value_of("dest"), Some("file"));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn missing_positional_no_hyphen() {
|
|
|
|
let r = App::new("bench")
|
|
|
|
.setting(AppSettings::AllowMissingPositional)
|
2018-04-21 18:59:19 +00:00
|
|
|
.arg(Arg::from("[BENCH] 'some bench'"))
|
|
|
|
.arg(Arg::from("[ARGS]... 'some args'"))
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec!["bench", "foo", "arg1", "arg2", "arg3"]);
|
2018-03-06 15:39:58 +00:00
|
|
|
assert!(r.is_ok(), "{:?}", r.unwrap_err().kind);
|
|
|
|
|
|
|
|
let m = r.unwrap();
|
|
|
|
|
|
|
|
let expected_bench = Some("foo");
|
|
|
|
let expected_args = vec!["arg1", "arg2", "arg3"];
|
|
|
|
|
|
|
|
assert_eq!(m.value_of("BENCH"), expected_bench);
|
2018-08-02 03:13:51 +00:00
|
|
|
assert_eq!(
|
|
|
|
m.values_of("ARGS").unwrap().collect::<Vec<_>>(),
|
|
|
|
&*expected_args
|
|
|
|
);
|
2018-03-06 15:39:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn missing_positional_hyphen() {
|
|
|
|
let r = App::new("bench")
|
|
|
|
.setting(AppSettings::AllowMissingPositional)
|
2018-04-21 18:59:19 +00:00
|
|
|
.arg(Arg::from("[BENCH] 'some bench'"))
|
|
|
|
.arg(Arg::from("[ARGS]... 'some args'"))
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec!["bench", "--", "arg1", "arg2", "arg3"]);
|
2018-03-06 15:39:58 +00:00
|
|
|
assert!(r.is_ok(), "{:?}", r.unwrap_err().kind);
|
|
|
|
|
|
|
|
let m = r.unwrap();
|
|
|
|
|
|
|
|
let expected_bench = None;
|
|
|
|
let expected_args = vec!["arg1", "arg2", "arg3"];
|
|
|
|
|
|
|
|
assert_eq!(m.value_of("BENCH"), expected_bench);
|
2018-08-02 03:13:51 +00:00
|
|
|
assert_eq!(
|
|
|
|
m.values_of("ARGS").unwrap().collect::<Vec<_>>(),
|
|
|
|
&*expected_args
|
|
|
|
);
|
2018-03-06 15:39:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn missing_positional_hyphen_far_back() {
|
|
|
|
let r = App::new("bench")
|
|
|
|
.setting(AppSettings::AllowMissingPositional)
|
2018-04-21 18:59:19 +00:00
|
|
|
.arg(Arg::from("[BENCH1] 'some bench'"))
|
|
|
|
.arg(Arg::from("[BENCH2] 'some bench'"))
|
|
|
|
.arg(Arg::from("[BENCH3] 'some bench'"))
|
|
|
|
.arg(Arg::from("[ARGS]... 'some args'"))
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec!["bench", "foo", "--", "arg1", "arg2", "arg3"]);
|
2018-03-06 15:39:58 +00:00
|
|
|
assert!(r.is_ok(), "{:?}", r.unwrap_err().kind);
|
|
|
|
|
|
|
|
let m = r.unwrap();
|
|
|
|
|
|
|
|
let expected_bench1 = Some("foo");
|
|
|
|
let expected_bench2 = None;
|
|
|
|
let expected_bench3 = None;
|
|
|
|
let expected_args = vec!["arg1", "arg2", "arg3"];
|
|
|
|
|
|
|
|
assert_eq!(m.value_of("BENCH1"), expected_bench1);
|
|
|
|
assert_eq!(m.value_of("BENCH2"), expected_bench2);
|
|
|
|
assert_eq!(m.value_of("BENCH3"), expected_bench3);
|
2018-08-02 03:13:51 +00:00
|
|
|
assert_eq!(
|
|
|
|
m.values_of("ARGS").unwrap().collect::<Vec<_>>(),
|
|
|
|
&*expected_args
|
|
|
|
);
|
2018-03-06 15:39:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn missing_positional_hyphen_req_error() {
|
|
|
|
let r = App::new("bench")
|
|
|
|
.setting(AppSettings::AllowMissingPositional)
|
2018-04-21 18:59:19 +00:00
|
|
|
.arg(Arg::from("[BENCH1] 'some bench'"))
|
|
|
|
.arg(Arg::from("<BENCH2> 'some bench'"))
|
|
|
|
.arg(Arg::from("[ARGS]... 'some args'"))
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec!["bench", "foo", "--", "arg1", "arg2", "arg3"]);
|
2018-03-06 15:39:58 +00:00
|
|
|
assert!(r.is_err());
|
|
|
|
assert_eq!(r.unwrap_err().kind, ErrorKind::MissingRequiredArgument);
|
|
|
|
}
|
|
|
|
|
2017-10-24 22:21:52 +00:00
|
|
|
#[test]
|
|
|
|
fn issue_1066_allow_leading_hyphen_and_unknown_args() {
|
|
|
|
let res = App::new("prog")
|
2018-01-25 02:08:14 +00:00
|
|
|
.global_setting(AppSettings::AllowLeadingHyphen)
|
2018-04-21 18:59:19 +00:00
|
|
|
.arg(Arg::from("--some-argument"))
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec!["prog", "hello"]);
|
2017-10-24 22:21:52 +00:00
|
|
|
|
|
|
|
assert!(res.is_err());
|
|
|
|
assert_eq!(res.unwrap_err().kind, ErrorKind::UnknownArgument);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn issue_1066_allow_leading_hyphen_and_unknown_args_no_vals() {
|
|
|
|
let res = App::new("prog")
|
2018-01-25 02:08:14 +00:00
|
|
|
.global_setting(AppSettings::AllowLeadingHyphen)
|
2018-04-21 18:59:19 +00:00
|
|
|
.arg(Arg::from("--some-argument"))
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec!["prog", "--hello"]);
|
2017-10-24 22:21:52 +00:00
|
|
|
|
|
|
|
assert!(res.is_err());
|
|
|
|
assert_eq!(res.unwrap_err().kind, ErrorKind::UnknownArgument);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn issue_1066_allow_leading_hyphen_and_unknown_args_option() {
|
|
|
|
let res = App::new("prog")
|
2018-01-25 02:08:14 +00:00
|
|
|
.global_setting(AppSettings::AllowLeadingHyphen)
|
2018-04-21 18:59:19 +00:00
|
|
|
.arg(Arg::from("--some-argument=[val]"))
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec!["prog", "-hello"]);
|
2017-10-24 22:21:52 +00:00
|
|
|
|
|
|
|
assert!(res.is_err());
|
|
|
|
assert_eq!(res.unwrap_err().kind, ErrorKind::UnknownArgument);
|
2017-11-07 01:15:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn issue_1093_allow_ext_sc() {
|
|
|
|
let app = App::new("clap-test")
|
|
|
|
.version("v1.4.8")
|
|
|
|
.setting(AppSettings::AllowExternalSubcommands);
|
2018-01-25 04:05:05 +00:00
|
|
|
assert!(test::compare_output(
|
|
|
|
app,
|
|
|
|
"clap-test --help",
|
|
|
|
ALLOW_EXT_SC,
|
|
|
|
false
|
|
|
|
));
|
|
|
|
}
|
2018-02-10 20:43:39 +00:00
|
|
|
|
2018-03-19 06:35:57 +00:00
|
|
|
#[test]
|
|
|
|
fn external_subcommand_looks_like_built_in() {
|
|
|
|
let res = App::new("cargo")
|
|
|
|
.version("1.26.0")
|
|
|
|
.setting(AppSettings::AllowExternalSubcommands)
|
2018-10-19 20:42:13 +00:00
|
|
|
.subcommand(App::new("install"))
|
|
|
|
.try_get_matches_from(vec!["cargo", "install-update", "foo"]);
|
2018-03-19 06:35:57 +00:00
|
|
|
assert!(res.is_ok());
|
|
|
|
let m = res.unwrap();
|
|
|
|
match m.subcommand() {
|
|
|
|
(name, Some(args)) => {
|
|
|
|
assert_eq!(name, "install-update");
|
|
|
|
assert_eq!(args.values_of_lossy(""), Some(vec!["foo".to_string()]));
|
|
|
|
}
|
|
|
|
_ => assert!(false),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-10 20:43:39 +00:00
|
|
|
#[test]
|
|
|
|
fn aaos_flags() {
|
|
|
|
// flags
|
|
|
|
let res = App::new("posix")
|
|
|
|
.setting(AppSettings::AllArgsOverrideSelf)
|
2018-04-21 18:59:19 +00:00
|
|
|
.arg(Arg::from("--flag 'some flag'"))
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec!["", "--flag", "--flag"]);
|
2018-02-10 20:43:39 +00:00
|
|
|
assert!(res.is_ok());
|
|
|
|
let m = res.unwrap();
|
|
|
|
assert!(m.is_present("flag"));
|
|
|
|
assert_eq!(m.occurrences_of("flag"), 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn aaos_flags_mult() {
|
|
|
|
// flags with multiple
|
|
|
|
let res = App::new("posix")
|
|
|
|
.setting(AppSettings::AllArgsOverrideSelf)
|
2018-04-21 18:59:19 +00:00
|
|
|
.arg(Arg::from("--flag... 'some flag'"))
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec!["", "--flag", "--flag", "--flag", "--flag"]);
|
2018-02-10 20:43:39 +00:00
|
|
|
assert!(res.is_ok());
|
|
|
|
let m = res.unwrap();
|
|
|
|
assert!(m.is_present("flag"));
|
|
|
|
assert_eq!(m.occurrences_of("flag"), 4);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn aaos_opts() {
|
|
|
|
// opts
|
|
|
|
let res = App::new("posix")
|
|
|
|
.setting(AppSettings::AllArgsOverrideSelf)
|
2018-04-21 18:59:19 +00:00
|
|
|
.arg(Arg::from("--opt [val] 'some option'"))
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec!["", "--opt=some", "--opt=other"]);
|
2018-02-10 20:43:39 +00:00
|
|
|
assert!(res.is_ok());
|
|
|
|
let m = res.unwrap();
|
|
|
|
assert!(m.is_present("opt"));
|
|
|
|
assert_eq!(m.occurrences_of("opt"), 1);
|
|
|
|
assert_eq!(m.value_of("opt"), Some("other"));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn aaos_opts_w_other_overrides() {
|
|
|
|
// opts with other overrides
|
|
|
|
let res = App::new("posix")
|
|
|
|
.setting(AppSettings::AllArgsOverrideSelf)
|
2018-04-21 18:59:19 +00:00
|
|
|
.arg(Arg::from("--opt [val] 'some option'"))
|
|
|
|
.arg(Arg::from("--other [val] 'some other option'").overrides_with("opt"))
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec!["", "--opt=some", "--other=test", "--opt=other"]);
|
2018-02-10 20:43:39 +00:00
|
|
|
assert!(res.is_ok());
|
|
|
|
let m = res.unwrap();
|
|
|
|
assert!(m.is_present("opt"));
|
|
|
|
assert!(!m.is_present("other"));
|
|
|
|
assert_eq!(m.occurrences_of("opt"), 1);
|
|
|
|
assert_eq!(m.value_of("opt"), Some("other"));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn aaos_opts_w_other_overrides_rev() {
|
|
|
|
// opts with other overrides, rev
|
|
|
|
let res = App::new("posix")
|
|
|
|
.setting(AppSettings::AllArgsOverrideSelf)
|
2018-04-21 18:59:19 +00:00
|
|
|
.arg(Arg::from("--opt [val] 'some option'"))
|
|
|
|
.arg(Arg::from("--other [val] 'some other option'").overrides_with("opt"))
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec!["", "--opt=some", "--opt=other", "--other=val"]);
|
2018-02-10 20:43:39 +00:00
|
|
|
assert!(res.is_ok());
|
|
|
|
let m = res.unwrap();
|
|
|
|
assert!(!m.is_present("opt"));
|
|
|
|
assert!(m.is_present("other"));
|
|
|
|
assert_eq!(m.value_of("other"), Some("val"));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn aaos_opts_w_other_overrides_2() {
|
|
|
|
// opts with other overrides
|
|
|
|
let res = App::new("posix")
|
|
|
|
.setting(AppSettings::AllArgsOverrideSelf)
|
2018-04-21 18:59:19 +00:00
|
|
|
.arg(Arg::from("--opt [val] 'some option'").overrides_with("other"))
|
|
|
|
.arg(Arg::from("--other [val] 'some other option'"))
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec!["", "--opt=some", "--other=test", "--opt=other"]);
|
2018-02-10 20:43:39 +00:00
|
|
|
assert!(res.is_ok());
|
|
|
|
let m = res.unwrap();
|
|
|
|
assert!(m.is_present("opt"));
|
|
|
|
assert!(!m.is_present("other"));
|
|
|
|
assert_eq!(m.occurrences_of("opt"), 1);
|
|
|
|
assert_eq!(m.value_of("opt"), Some("other"));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn aaos_opts_w_other_overrides_rev_2() {
|
|
|
|
// opts with other overrides, rev
|
|
|
|
let res = App::new("posix")
|
|
|
|
.setting(AppSettings::AllArgsOverrideSelf)
|
2018-04-21 18:59:19 +00:00
|
|
|
.arg(Arg::from("--opt [val] 'some option'").overrides_with("other"))
|
|
|
|
.arg(Arg::from("--other [val] 'some other option'"))
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec!["", "--opt=some", "--opt=other", "--other=val"]);
|
2018-02-10 20:43:39 +00:00
|
|
|
assert!(res.is_ok());
|
|
|
|
let m = res.unwrap();
|
|
|
|
assert!(!m.is_present("opt"));
|
|
|
|
assert!(m.is_present("other"));
|
|
|
|
assert_eq!(m.value_of("other"), Some("val"));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn aaos_opts_mult() {
|
|
|
|
// opts with multiple
|
|
|
|
let res = App::new("posix")
|
|
|
|
.setting(AppSettings::AllArgsOverrideSelf)
|
|
|
|
.arg(
|
2018-04-21 18:59:19 +00:00
|
|
|
Arg::from("--opt [val]... 'some option'")
|
2018-02-10 20:43:39 +00:00
|
|
|
.number_of_values(1)
|
|
|
|
.require_delimiter(true),
|
|
|
|
)
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec!["", "--opt=some", "--opt=other", "--opt=one,two"]);
|
2018-02-10 20:43:39 +00:00
|
|
|
assert!(res.is_ok());
|
|
|
|
let m = res.unwrap();
|
|
|
|
assert!(m.is_present("opt"));
|
|
|
|
assert_eq!(m.occurrences_of("opt"), 3);
|
|
|
|
assert_eq!(
|
|
|
|
m.values_of("opt").unwrap().collect::<Vec<_>>(),
|
|
|
|
&["some", "other", "one", "two"]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn aaos_opts_mult_req_delims() {
|
|
|
|
// opts with multiple and require delims
|
|
|
|
let res = App::new("posix")
|
|
|
|
.setting(AppSettings::AllArgsOverrideSelf)
|
2018-04-21 18:59:19 +00:00
|
|
|
.arg(Arg::from("--opt [val]... 'some option'"))
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec![
|
2018-08-02 03:13:51 +00:00
|
|
|
"", "--opt", "first", "overides", "--opt", "some", "other", "val",
|
2018-02-10 20:43:39 +00:00
|
|
|
]);
|
|
|
|
assert!(res.is_ok());
|
|
|
|
let m = res.unwrap();
|
|
|
|
assert!(m.is_present("opt"));
|
|
|
|
assert_eq!(m.occurrences_of("opt"), 2);
|
|
|
|
assert_eq!(
|
|
|
|
m.values_of("opt").unwrap().collect::<Vec<_>>(),
|
|
|
|
&["first", "overides", "some", "other", "val"]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn aaos_pos_mult() {
|
|
|
|
// opts with multiple
|
|
|
|
let res = App::new("posix")
|
|
|
|
.setting(AppSettings::AllArgsOverrideSelf)
|
2018-04-21 18:59:19 +00:00
|
|
|
.arg(Arg::from("[val]... 'some pos'"))
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec!["", "some", "other", "value"]);
|
2018-02-10 20:43:39 +00:00
|
|
|
assert!(res.is_ok());
|
|
|
|
let m = res.unwrap();
|
|
|
|
assert!(m.is_present("val"));
|
|
|
|
assert_eq!(m.occurrences_of("val"), 3);
|
|
|
|
assert_eq!(
|
|
|
|
m.values_of("val").unwrap().collect::<Vec<_>>(),
|
|
|
|
&["some", "other", "value"]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn aaos_option_use_delim_false() {
|
|
|
|
let m = App::new("posix")
|
|
|
|
.setting(AppSettings::AllArgsOverrideSelf)
|
2018-04-21 18:59:19 +00:00
|
|
|
.arg(Arg::from("--opt [val] 'some option'").use_delimiter(false))
|
2018-02-10 20:43:39 +00:00
|
|
|
.get_matches_from(vec!["", "--opt=some,other", "--opt=one,two"]);
|
|
|
|
assert!(m.is_present("opt"));
|
|
|
|
assert_eq!(m.occurrences_of("opt"), 1);
|
|
|
|
assert_eq!(
|
|
|
|
m.values_of("opt").unwrap().collect::<Vec<_>>(),
|
|
|
|
&["one,two"]
|
|
|
|
);
|
|
|
|
}
|