2020-01-09 03:56:51 +00:00
|
|
|
use clap::{arg_enum, clap_app, ErrorKind};
|
2018-03-30 20:49:49 +00:00
|
|
|
|
2016-12-13 06:42:11 +00:00
|
|
|
#[test]
|
|
|
|
fn basic() {
|
|
|
|
clap_app!(claptests =>
|
|
|
|
(version: "0.1")
|
|
|
|
(about: "tests clap library")
|
|
|
|
(author: "Kevin K. <kbknapp@gmail.com>")
|
|
|
|
(@arg opt: -o --option +takes_value ... "tests options")
|
|
|
|
(@arg positional: index(1) "tests positionals")
|
|
|
|
(@arg flag: -f --flag ... +global "tests flags")
|
|
|
|
(@arg flag2: -F conflicts_with[flag] requires[option2]
|
|
|
|
"tests flags with exclusions")
|
|
|
|
(@arg option2: --long_option_2 conflicts_with[option] requires[positional2]
|
|
|
|
"tests long options with exclusions")
|
|
|
|
(@arg positional2: index(2) "tests positionals with exclusions")
|
|
|
|
(@arg option3: -O --Option +takes_value possible_value[fast slow]
|
|
|
|
"tests options with specific value sets")
|
|
|
|
(@arg positional3: index(3) ... possible_value[vi emacs]
|
|
|
|
"tests positionals with specific values")
|
|
|
|
(@arg multvals: --multvals +takes_value value_name[one two]
|
|
|
|
"Tests mutliple values, not mult occs")
|
|
|
|
(@arg multvalsmo: --multvalsmo ... +takes_value value_name[one two]
|
|
|
|
"Tests mutliple values, not mult occs")
|
|
|
|
(@arg minvals: --minvals2 min_values(1) ... +takes_value "Tests 2 min vals")
|
|
|
|
(@arg maxvals: --maxvals3 ... +takes_value max_values(3) "Tests 3 max vals")
|
|
|
|
(@subcommand subcmd =>
|
|
|
|
(about: "tests subcommands")
|
|
|
|
(version: "0.1")
|
|
|
|
(author: "Kevin K. <kbknapp@gmail.com>")
|
|
|
|
(@arg scoption: -o --option ... +takes_value "tests options")
|
|
|
|
(@arg scpositional: index(1) "tests positionals"))
|
|
|
|
);
|
|
|
|
}
|
2016-12-13 06:42:11 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn quoted_app_name() {
|
2018-01-25 02:08:14 +00:00
|
|
|
let mut app = clap_app!(("app name with spaces-and-hyphens") =>
|
2016-12-13 06:42:11 +00:00
|
|
|
(version: "0.1")
|
|
|
|
(about: "tests clap library")
|
|
|
|
(author: "Kevin K. <kbknapp@gmail.com>")
|
2020-04-09 16:19:05 +00:00
|
|
|
(@arg option: -o --option +takes_value ... "tests options")
|
2016-12-13 06:42:11 +00:00
|
|
|
(@arg positional: index(1) "tests positionals")
|
|
|
|
(@arg flag: -f --flag ... +global "tests flags")
|
|
|
|
(@arg flag2: -F conflicts_with[flag] requires[option2]
|
|
|
|
"tests flags with exclusions")
|
|
|
|
(@arg option2: --long_option_2 conflicts_with[option] requires[positional2]
|
|
|
|
"tests long options with exclusions")
|
|
|
|
(@arg positional2: index(2) "tests positionals with exclusions")
|
|
|
|
(@arg option3: -O --Option +takes_value possible_value[fast slow]
|
|
|
|
"tests options with specific value sets")
|
|
|
|
(@arg positional3: index(3) ... possible_value[vi emacs]
|
|
|
|
"tests positionals with specific values")
|
|
|
|
(@arg multvals: --multvals +takes_value value_name[one two]
|
|
|
|
"Tests mutliple values, not mult occs")
|
|
|
|
(@arg multvalsmo: --multvalsmo ... +takes_value value_name[one two]
|
|
|
|
"Tests mutliple values, not mult occs")
|
|
|
|
(@arg minvals: --minvals2 min_values(1) ... +takes_value "Tests 2 min vals")
|
|
|
|
(@arg maxvals: --maxvals3 ... +takes_value max_values(3) "Tests 3 max vals")
|
|
|
|
(@subcommand subcmd =>
|
|
|
|
(about: "tests subcommands")
|
|
|
|
(version: "0.1")
|
|
|
|
(author: "Kevin K. <kbknapp@gmail.com>")
|
|
|
|
(@arg scoption: -o --option ... +takes_value "tests options")
|
|
|
|
(@arg scpositional: index(1) "tests positionals"))
|
|
|
|
);
|
|
|
|
|
2020-04-10 21:15:09 +00:00
|
|
|
assert_eq!(app.get_name(), "app name with spaces-and-hyphens");
|
2016-12-13 06:42:11 +00:00
|
|
|
|
|
|
|
let mut help_text = vec![];
|
2018-01-25 04:05:05 +00:00
|
|
|
app.write_help(&mut help_text)
|
|
|
|
.expect("Could not write help text.");
|
2016-12-13 06:42:11 +00:00
|
|
|
let help_text = String::from_utf8(help_text).expect("Help text is not valid utf-8");
|
|
|
|
assert!(help_text.starts_with("app name with spaces-and-hyphens 0.1\n"));
|
|
|
|
}
|
2016-12-13 06:42:11 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn quoted_arg_long_name() {
|
|
|
|
let app = clap_app!(claptests =>
|
|
|
|
(version: "0.1")
|
|
|
|
(about: "tests clap library")
|
|
|
|
(author: "Kevin K. <kbknapp@gmail.com>")
|
2020-04-09 16:19:05 +00:00
|
|
|
(@arg option: -o --option +takes_value ... "tests options")
|
2016-12-13 06:42:11 +00:00
|
|
|
(@arg positional: index(1) "tests positionals")
|
|
|
|
(@arg flag: -f --flag ... +global "tests flags")
|
|
|
|
(@arg flag2: -F conflicts_with[flag] requires[option2]
|
|
|
|
"tests flags with exclusions")
|
|
|
|
(@arg option2: --("long-option-2") conflicts_with[option] requires[positional2]
|
|
|
|
"tests long options with exclusions")
|
|
|
|
(@arg positional2: index(2) "tests positionals with exclusions")
|
|
|
|
(@arg option3: -O --Option +takes_value possible_value[fast slow]
|
|
|
|
"tests options with specific value sets")
|
|
|
|
(@arg positional3: index(3) ... possible_value[vi emacs]
|
2017-04-05 04:57:40 +00:00
|
|
|
"tests positionals with specific values")
|
|
|
|
(@arg multvals: --multvals +takes_value value_name[one two]
|
|
|
|
"Tests mutliple values, not mult occs")
|
|
|
|
(@arg multvalsmo: --multvalsmo ... +takes_value value_name[one two]
|
|
|
|
"Tests mutliple values, not mult occs")
|
|
|
|
(@arg minvals: --minvals2 min_values(1) ... +takes_value "Tests 2 min vals")
|
|
|
|
(@arg maxvals: --maxvals3 ... +takes_value max_values(3) "Tests 3 max vals")
|
|
|
|
(@subcommand subcmd =>
|
|
|
|
(about: "tests subcommands")
|
|
|
|
(version: "0.1")
|
|
|
|
(author: "Kevin K. <kbknapp@gmail.com>")
|
|
|
|
(@arg scoption: -o --option ... +takes_value "tests options")
|
|
|
|
(@arg scpositional: index(1) "tests positionals"))
|
|
|
|
);
|
|
|
|
|
2018-08-02 03:13:51 +00:00
|
|
|
let matches = app
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec!["bin_name", "value1", "value2", "--long-option-2"])
|
2018-08-02 03:13:51 +00:00
|
|
|
.expect("Expected to successfully match the given args.");
|
2017-04-05 04:57:40 +00:00
|
|
|
assert!(matches.is_present("option2"));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn quoted_arg_name() {
|
|
|
|
let app = clap_app!(claptests =>
|
|
|
|
(version: "0.1")
|
|
|
|
(about: "tests clap library")
|
|
|
|
(author: "Kevin K. <kbknapp@gmail.com>")
|
2020-04-09 16:19:05 +00:00
|
|
|
(@arg option: -o --option +takes_value ... "tests options")
|
2017-04-05 04:57:40 +00:00
|
|
|
(@arg ("positional-arg"): index(1) "tests positionals")
|
|
|
|
(@arg flag: -f --flag ... +global "tests flags")
|
|
|
|
(@arg flag2: -F conflicts_with[flag] requires[option2]
|
|
|
|
"tests flags with exclusions")
|
|
|
|
(@arg option2: --("long-option-2") conflicts_with[option] requires[positional2]
|
|
|
|
"tests long options with exclusions")
|
|
|
|
(@arg positional2: index(2) "tests positionals with exclusions")
|
|
|
|
(@arg option3: -O --Option +takes_value possible_value[fast slow]
|
|
|
|
"tests options with specific value sets")
|
|
|
|
(@arg ("positional-3"): index(3) ... possible_value[vi emacs]
|
2016-12-13 06:42:11 +00:00
|
|
|
"tests positionals with specific values")
|
|
|
|
(@arg multvals: --multvals +takes_value value_name[one two]
|
|
|
|
"Tests mutliple values, not mult occs")
|
|
|
|
(@arg multvalsmo: --multvalsmo ... +takes_value value_name[one two]
|
|
|
|
"Tests mutliple values, not mult occs")
|
|
|
|
(@arg minvals: --minvals2 min_values(1) ... +takes_value "Tests 2 min vals")
|
|
|
|
(@arg maxvals: --maxvals3 ... +takes_value max_values(3) "Tests 3 max vals")
|
|
|
|
(@subcommand subcmd =>
|
|
|
|
(about: "tests subcommands")
|
|
|
|
(version: "0.1")
|
|
|
|
(author: "Kevin K. <kbknapp@gmail.com>")
|
|
|
|
(@arg scoption: -o --option ... +takes_value "tests options")
|
|
|
|
(@arg scpositional: index(1) "tests positionals"))
|
|
|
|
);
|
|
|
|
|
2018-08-02 03:13:51 +00:00
|
|
|
let matches = app
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec!["bin_name", "value1", "value2", "--long-option-2"])
|
2018-08-02 03:13:51 +00:00
|
|
|
.expect("Expected to successfully match the given args.");
|
2016-12-13 06:42:11 +00:00
|
|
|
assert!(matches.is_present("option2"));
|
|
|
|
}
|
2017-11-07 00:59:41 +00:00
|
|
|
|
2019-07-20 23:39:30 +00:00
|
|
|
#[test]
|
|
|
|
fn quoted_subcommand_name() {
|
|
|
|
clap_app!(claptests =>
|
|
|
|
(version: "0.1")
|
|
|
|
(about: "tests clap library")
|
|
|
|
(author: "Kevin K. <kbknapp@gmail.com>")
|
|
|
|
(@arg opt: -o --option +takes_value ... "tests options")
|
|
|
|
(@arg positional: index(1) "tests positionals")
|
|
|
|
(@arg flag: -f --flag ... +global "tests flags")
|
|
|
|
(@arg flag2: -F conflicts_with[flag] requires[option2]
|
|
|
|
"tests flags with exclusions")
|
|
|
|
(@arg option2: --long_option_2 conflicts_with[option] requires[positional2]
|
|
|
|
"tests long options with exclusions")
|
|
|
|
(@arg positional2: index(2) "tests positionals with exclusions")
|
|
|
|
(@arg option3: -O --Option +takes_value possible_value[fast slow]
|
|
|
|
"tests options with specific value sets")
|
|
|
|
(@arg positional3: index(3) ... possible_value[vi emacs]
|
|
|
|
"tests positionals with specific values")
|
|
|
|
(@arg multvals: --multvals +takes_value value_name[one two]
|
|
|
|
"Tests multiple values, not mult occs")
|
|
|
|
(@arg multvalsmo: --multvalsmo ... +takes_value value_name[one two]
|
|
|
|
"Tests multiple values, not mult occs")
|
|
|
|
(@arg minvals: --minvals2 min_values(1) ... +takes_value "Tests 2 min vals")
|
|
|
|
(@arg maxvals: --maxvals3 ... +takes_value max_values(3) "Tests 3 max vals")
|
|
|
|
(@subcommand subcmd =>
|
|
|
|
(about: "tests subcommands")
|
|
|
|
(version: "0.1")
|
|
|
|
(author: "Kevin K. <kbknapp@gmail.com>")
|
|
|
|
(@arg scoption: -o --option ... +takes_value "tests options")
|
|
|
|
(@arg scpositional: index(1) "tests positionals"))
|
|
|
|
(@subcommand ("other-subcmd") =>
|
|
|
|
(about: "some other subcommand"))
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-03-30 20:49:49 +00:00
|
|
|
#[test]
|
|
|
|
fn group_macro() {
|
|
|
|
let app = clap_app!(claptests =>
|
|
|
|
(version: "0.1")
|
|
|
|
(about: "tests clap library")
|
|
|
|
(author: "Kevin K. <kbknapp@gmail.com>")
|
|
|
|
(@group difficulty =>
|
|
|
|
(@arg hard: -h --hard "Sets hard mode")
|
|
|
|
(@arg normal: -n --normal "Sets normal mode")
|
|
|
|
(@arg easy: -e --easy "Sets easy mode")
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
2018-10-19 20:42:13 +00:00
|
|
|
let result = app.try_get_matches_from(vec!["bin_name", "--hard"]);
|
2018-03-30 20:49:49 +00:00
|
|
|
assert!(result.is_ok());
|
|
|
|
let matches = result.expect("Expected to successfully match the given args.");
|
|
|
|
assert!(matches.is_present("difficulty"));
|
|
|
|
assert!(matches.is_present("hard"));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn group_macro_set_multiple() {
|
|
|
|
let app = clap_app!(claptests =>
|
|
|
|
(version: "0.1")
|
|
|
|
(about: "tests clap library")
|
|
|
|
(author: "Kevin K. <kbknapp@gmail.com>")
|
|
|
|
(@group difficulty +multiple =>
|
|
|
|
(@arg hard: -h --hard "Sets hard mode")
|
|
|
|
(@arg normal: -n --normal "Sets normal mode")
|
|
|
|
(@arg easy: -e --easy "Sets easy mode")
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
2018-10-19 20:42:13 +00:00
|
|
|
let result = app.try_get_matches_from(vec!["bin_name", "--hard", "--easy"]);
|
2018-03-30 20:49:49 +00:00
|
|
|
assert!(result.is_ok());
|
|
|
|
let matches = result.expect("Expected to successfully match the given args.");
|
|
|
|
assert!(matches.is_present("difficulty"));
|
|
|
|
assert!(matches.is_present("hard"));
|
|
|
|
assert!(matches.is_present("easy"));
|
|
|
|
assert!(!matches.is_present("normal"));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn group_macro_set_not_multiple() {
|
|
|
|
let app = clap_app!(claptests =>
|
|
|
|
(version: "0.1")
|
|
|
|
(about: "tests clap library")
|
|
|
|
(author: "Kevin K. <kbknapp@gmail.com>")
|
|
|
|
(@group difficulty !multiple =>
|
|
|
|
(@arg hard: -h --hard "Sets hard mode")
|
|
|
|
(@arg normal: -n --normal "Sets normal mode")
|
|
|
|
(@arg easy: -e --easy "Sets easy mode")
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
2018-10-19 20:42:13 +00:00
|
|
|
let result = app.try_get_matches_from(vec!["bin_name", "--hard", "--easy"]);
|
2018-03-30 20:49:49 +00:00
|
|
|
assert!(result.is_err());
|
|
|
|
let err = result.unwrap_err();
|
|
|
|
assert_eq!(err.kind, ErrorKind::ArgumentConflict);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn group_macro_set_required() {
|
|
|
|
let app = clap_app!(claptests =>
|
|
|
|
(version: "0.1")
|
|
|
|
(about: "tests clap library")
|
|
|
|
(author: "Kevin K. <kbknapp@gmail.com>")
|
|
|
|
(@group difficulty +required =>
|
|
|
|
(@arg hard: -h --hard "Sets hard mode")
|
|
|
|
(@arg normal: -n --normal "Sets normal mode")
|
|
|
|
(@arg easy: -e --easy "Sets easy mode")
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
2018-10-19 20:42:13 +00:00
|
|
|
let result = app.try_get_matches_from(vec!["bin_name"]);
|
2018-03-30 20:49:49 +00:00
|
|
|
assert!(result.is_err());
|
|
|
|
let err = result.unwrap_err();
|
|
|
|
assert_eq!(err.kind, ErrorKind::MissingRequiredArgument);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn group_macro_set_not_required() {
|
|
|
|
let app = clap_app!(claptests =>
|
|
|
|
(version: "0.1")
|
|
|
|
(about: "tests clap library")
|
|
|
|
(author: "Kevin K. <kbknapp@gmail.com>")
|
|
|
|
(@group difficulty !required =>
|
|
|
|
(@arg hard: -h --hard "Sets hard mode")
|
|
|
|
(@arg normal: -n --normal "Sets normal mode")
|
|
|
|
(@arg easy: -e --easy "Sets easy mode")
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
2018-10-19 20:42:13 +00:00
|
|
|
let result = app.try_get_matches_from(vec!["bin_name"]);
|
2018-03-30 20:49:49 +00:00
|
|
|
assert!(result.is_ok());
|
|
|
|
let matches = result.expect("Expected to successfully match the given args.");
|
|
|
|
assert!(!matches.is_present("difficulty"));
|
|
|
|
}
|
|
|
|
|
2017-11-07 00:59:41 +00:00
|
|
|
#[test]
|
|
|
|
fn arg_enum() {
|
2018-04-07 11:30:58 +00:00
|
|
|
// Helper macros to avoid repetition
|
|
|
|
macro_rules! test_greek {
|
|
|
|
($arg_enum:item, $tests:block) => {{
|
|
|
|
$arg_enum
|
|
|
|
// FromStr implementation
|
|
|
|
assert!("Charlie".parse::<Greek>().is_err());
|
|
|
|
// Display implementation
|
|
|
|
assert_eq!(format!("{}", Greek::Alpha), "Alpha");
|
|
|
|
assert_eq!(format!("{}", Greek::Bravo), "Bravo");
|
|
|
|
// fn variants()
|
|
|
|
assert_eq!(Greek::variants(), ["Alpha", "Bravo"]);
|
|
|
|
// rest of tests
|
|
|
|
$tests
|
|
|
|
}};
|
|
|
|
}
|
|
|
|
macro_rules! test_greek_no_meta {
|
|
|
|
{$arg_enum:item} => {
|
|
|
|
test_greek!($arg_enum, {
|
|
|
|
// FromStr implementation
|
|
|
|
assert!("Alpha".parse::<Greek>().is_ok());
|
|
|
|
assert!("Bravo".parse::<Greek>().is_ok());
|
|
|
|
})
|
|
|
|
};
|
|
|
|
}
|
|
|
|
macro_rules! test_greek_meta {
|
|
|
|
{$arg_enum:item} => {
|
|
|
|
test_greek!($arg_enum, {
|
|
|
|
// FromStr implementation
|
|
|
|
assert_eq!("Alpha".parse::<Greek>(), Ok(Greek::Alpha));
|
|
|
|
assert_eq!("Bravo".parse::<Greek>(), Ok(Greek::Bravo));
|
|
|
|
})
|
|
|
|
};
|
2017-11-07 00:59:41 +00:00
|
|
|
}
|
2017-12-07 00:53:55 +00:00
|
|
|
|
2018-04-07 11:30:58 +00:00
|
|
|
// Tests for each pattern
|
|
|
|
// meta NO, pub NO, trailing comma NO
|
|
|
|
test_greek_no_meta! {
|
|
|
|
arg_enum!{
|
|
|
|
enum Greek {
|
|
|
|
Alpha,
|
|
|
|
Bravo
|
|
|
|
}
|
2017-12-07 00:53:55 +00:00
|
|
|
}
|
2018-04-07 11:30:58 +00:00
|
|
|
};
|
|
|
|
// meta NO, pub NO, trailing comma YES
|
|
|
|
test_greek_no_meta! {
|
|
|
|
arg_enum!{
|
|
|
|
enum Greek {
|
|
|
|
Alpha,
|
|
|
|
Bravo,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
// meta NO, pub YES, trailing comma NO
|
|
|
|
test_greek_no_meta! {
|
|
|
|
arg_enum!{
|
|
|
|
pub enum Greek {
|
|
|
|
Alpha,
|
|
|
|
Bravo
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
// meta NO, pub YES, trailing comma YES
|
|
|
|
test_greek_no_meta! {
|
|
|
|
arg_enum!{
|
|
|
|
pub enum Greek {
|
|
|
|
Alpha,
|
|
|
|
Bravo,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
// meta YES, pub NO, trailing comma NO
|
|
|
|
test_greek_meta! {
|
|
|
|
arg_enum!{
|
|
|
|
#[derive(Debug, PartialEq, Copy, Clone)]
|
|
|
|
enum Greek {
|
|
|
|
Alpha,
|
|
|
|
Bravo
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
// meta YES, pub NO, trailing comma YES
|
|
|
|
test_greek_meta! {
|
|
|
|
arg_enum!{
|
|
|
|
#[derive(Debug, PartialEq, Copy, Clone)]
|
|
|
|
enum Greek {
|
|
|
|
Alpha,
|
|
|
|
Bravo,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
// meta YES, pub YES, trailing comma NO
|
|
|
|
test_greek_meta! {
|
|
|
|
arg_enum!{
|
|
|
|
#[derive(Debug, PartialEq, Copy, Clone)]
|
|
|
|
pub enum Greek {
|
|
|
|
Alpha,
|
|
|
|
Bravo
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
// meta YES, pub YES, trailing comma YES
|
|
|
|
test_greek_meta! {
|
|
|
|
arg_enum!{
|
|
|
|
#[derive(Debug, PartialEq, Copy, Clone)]
|
|
|
|
pub enum Greek {
|
|
|
|
Alpha,
|
|
|
|
Bravo,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2017-12-07 00:53:55 +00:00
|
|
|
}
|