test: Panic, rather than exit, on error

Working on a fix for #3217 and can't see what is failing because the
test process exits.
This commit is contained in:
Ed Page 2021-12-27 12:56:12 -06:00
parent b842dff443
commit bfc486501a
24 changed files with 355 additions and 176 deletions

View file

@ -202,7 +202,8 @@ fn sub_command_negate_required() {
.setting(AppSettings::SubcommandsNegateReqs)
.arg(Arg::new("test").required(true).index(1))
.subcommand(App::new("sub1"))
.get_matches_from(vec!["myprog", "sub1"]);
.try_get_matches_from(vec!["myprog", "sub1"])
.unwrap();
}
#[test]
@ -359,7 +360,8 @@ fn infer_subcommands_pass() {
let m = App::new("prog")
.setting(AppSettings::InferSubcommands)
.subcommand(App::new("test"))
.get_matches_from(vec!["prog", "te"]);
.try_get_matches_from(vec!["prog", "te"])
.unwrap();
assert_eq!(m.subcommand_name(), Some("test"));
}
@ -369,7 +371,8 @@ fn infer_subcommands_pass_close() {
.setting(AppSettings::InferSubcommands)
.subcommand(App::new("test"))
.subcommand(App::new("temp"))
.get_matches_from(vec!["prog", "tes"]);
.try_get_matches_from(vec!["prog", "tes"])
.unwrap();
assert_eq!(m.subcommand_name(), Some("test"));
}
@ -380,7 +383,8 @@ fn infer_subcommands_pass_exact_match() {
.subcommand(App::new("test"))
.subcommand(App::new("testa"))
.subcommand(App::new("testb"))
.get_matches_from(vec!["prog", "test"]);
.try_get_matches_from(vec!["prog", "test"])
.unwrap();
assert_eq!(m.subcommand_name(), Some("test"));
}
@ -466,7 +470,8 @@ fn dont_delim_values_trailingvararg() {
.setting(AppSettings::TrailingVarArg)
.setting(AppSettings::DontDelimitTrailingValues)
.arg(arg!([opt] ... "some pos"))
.get_matches_from(vec!["", "test", "--foo", "-Wl,-bar"]);
.try_get_matches_from(vec!["", "test", "--foo", "-Wl,-bar"])
.unwrap();
assert!(m.is_present("opt"));
assert_eq!(
m.values_of("opt").unwrap().collect::<Vec<_>>(),
@ -494,7 +499,8 @@ fn delim_values_trailingvararg() {
let m = App::new("positional")
.setting(AppSettings::TrailingVarArg)
.arg(arg!([opt] ... "some pos"))
.get_matches_from(vec!["", "test", "--foo", "-Wl,-bar"]);
.try_get_matches_from(vec!["", "test", "--foo", "-Wl,-bar"])
.unwrap();
assert!(m.is_present("opt"));
assert_eq!(
m.values_of("opt").unwrap().collect::<Vec<_>>(),
@ -525,7 +531,8 @@ fn delim_values_trailingvararg_with_delim() {
let m = App::new("positional")
.setting(AppSettings::TrailingVarArg)
.arg(arg!([opt] ... "some pos").use_delimiter(true))
.get_matches_from(vec!["", "test", "--foo", "-Wl,-bar"]);
.try_get_matches_from(vec!["", "test", "--foo", "-Wl,-bar"])
.unwrap();
assert!(m.is_present("opt"));
assert_eq!(
m.values_of("opt").unwrap().collect::<Vec<_>>(),
@ -605,7 +612,8 @@ fn leading_double_hyphen_trailingvararg() {
.setting(AppSettings::TrailingVarArg)
.setting(AppSettings::AllowHyphenValues)
.arg(arg!([opt] ... "some pos"))
.get_matches_from(vec!["", "--foo", "-Wl", "bar"]);
.try_get_matches_from(vec!["", "--foo", "-Wl", "bar"])
.unwrap();
assert!(m.is_present("opt"));
assert_eq!(
m.values_of("opt").unwrap().collect::<Vec<_>>(),
@ -859,7 +867,8 @@ fn issue_1437_allow_hyphen_values_for_positional_arg() {
.required(true)
.takes_value(true),
)
.get_matches_from(["tmp", "-file"]);
.try_get_matches_from(["tmp", "-file"])
.unwrap();
assert_eq!(m.value_of("pat"), Some("-file"));
}
@ -1104,7 +1113,8 @@ fn aaos_option_use_delim_false() {
let m = App::new("posix")
.setting(AppSettings::AllArgsOverrideSelf)
.arg(arg!(--opt <val> "some option").use_delimiter(false))
.get_matches_from(vec!["", "--opt=some,other", "--opt=one,two"]);
.try_get_matches_from(vec!["", "--opt=some,other", "--opt=one,two"])
.unwrap();
assert!(m.is_present("opt"));
assert_eq!(m.occurrences_of("opt"), 1);
assert_eq!(

View file

@ -144,7 +144,8 @@ fn alias_on_a_subcommand_option() {
),
)
.arg(Arg::new("other").long("other").aliases(&["o1", "o2", "o3"]))
.get_matches_from(vec!["test", "some", "--opt", "awesome"]);
.try_get_matches_from(vec!["test", "some", "--opt", "awesome"])
.unwrap();
assert!(m.subcommand_matches("some").is_some());
let sub_m = m.subcommand_matches("some").unwrap();

View file

@ -140,7 +140,8 @@ fn short_alias_on_a_subcommand_option() {
.long("other")
.short_aliases(&['1', '2', '3']),
)
.get_matches_from(vec!["test", "some", "-o", "awesome"]);
.try_get_matches_from(vec!["test", "some", "-o", "awesome"])
.unwrap();
assert!(m.subcommand_matches("some").is_some());
let sub_m = m.subcommand_matches("some").unwrap();

View file

@ -7,7 +7,8 @@ use clap::{App, Arg};
fn arg_matches_if_present_wrong_arg() {
let m = App::new("test")
.arg(Arg::new("flag").short('f'))
.get_matches_from(&["test", "-f"]);
.try_get_matches_from(&["test", "-f"])
.unwrap();
assert!(m.is_present("flag"));
m.is_present("f");
@ -19,7 +20,8 @@ fn arg_matches_if_present_wrong_arg() {
fn arg_matches_value_of_wrong_arg() {
let m = App::new("test")
.arg(Arg::new("opt").short('o').takes_value(true))
.get_matches_from(&["test", "-o", "val"]);
.try_get_matches_from(&["test", "-o", "val"])
.unwrap();
assert_eq!(m.value_of("opt"), Some("val"));
m.value_of("o");
@ -31,7 +33,8 @@ fn arg_matches_value_of_wrong_arg() {
fn arg_matches_subcommand_matches_wrong_sub() {
let m = App::new("test")
.subcommand(App::new("speed"))
.get_matches_from(&["test", "speed"]);
.try_get_matches_from(&["test", "speed"])
.unwrap();
assert!(m.subcommand_matches("speed").is_some());
m.subcommand_matches("seed");

View file

@ -126,17 +126,32 @@ fn default_missing_value_flag_value() {
}
}
assert_eq!(flag_value(app.clone().get_matches_from(&["test"])), false);
assert_eq!(
flag_value(app.clone().get_matches_from(&["test", "--flag"])),
flag_value(app.clone().try_get_matches_from(&["test"]).unwrap()),
false
);
assert_eq!(
flag_value(
app.clone()
.try_get_matches_from(&["test", "--flag"])
.unwrap()
),
true
);
assert_eq!(
flag_value(app.clone().get_matches_from(&["test", "--flag=true"])),
flag_value(
app.clone()
.try_get_matches_from(&["test", "--flag=true"])
.unwrap()
),
true
);
assert_eq!(
flag_value(app.clone().get_matches_from(&["test", "--flag=false"])),
flag_value(
app.clone()
.try_get_matches_from(&["test", "--flag=false"])
.unwrap()
),
false
);
}

View file

@ -641,7 +641,7 @@ fn with_value_delimiter() {
.default_value("first;second"),
);
let matches = app.get_matches_from(vec![""]);
let matches = app.try_get_matches_from(vec![""]).unwrap();
assert_eq!(
matches.values_of("option").unwrap().collect::<Vec<_>>(),
@ -658,7 +658,9 @@ fn missing_with_value_delimiter() {
.default_missing_values(&["value1;value2;value3", "value4;value5"]),
);
let matches = app.get_matches_from(vec!["program", "--option"]);
let matches = app
.try_get_matches_from(vec!["program", "--option"])
.unwrap();
assert_eq!(
matches.values_of("option").unwrap().collect::<Vec<_>>(),

View file

@ -6,7 +6,8 @@ use clap::{App, Arg, ErrorKind};
fn empty_values() {
let m = App::new("config")
.arg(Arg::new("config").long("config").takes_value(true))
.get_matches_from(&["config", "--config", ""]);
.try_get_matches_from(&["config", "--config", ""])
.unwrap();
assert_eq!(m.value_of("config"), Some(""));
}
@ -14,12 +15,14 @@ fn empty_values() {
fn empty_values_with_equals() {
let m = App::new("config")
.arg(Arg::new("config").long("config").takes_value(true))
.get_matches_from(&["config", "--config="]);
.try_get_matches_from(&["config", "--config="])
.unwrap();
assert_eq!(m.value_of("config"), Some(""));
let m = App::new("config")
.arg(Arg::new("config").short('c').takes_value(true))
.get_matches_from(&["config", "-c="]);
.try_get_matches_from(&["config", "-c="])
.unwrap();
assert_eq!(m.value_of("config"), Some(""))
}

View file

@ -323,7 +323,8 @@ fn validator_output() {
.takes_value(true)
.validator(|s| s.parse::<i32>()),
)
.get_matches_from(vec![""]);
.try_get_matches_from(vec![""])
.unwrap();
assert_eq!(m.value_of("arg").unwrap().parse(), Ok(42));
}

View file

@ -13,7 +13,8 @@ fn flag_subcommand_normal() {
.help("testing testing"),
),
)
.get_matches_from(vec!["myprog", "some", "--test"]);
.try_get_matches_from(vec!["myprog", "some", "--test"])
.unwrap();
assert_eq!(matches.subcommand_name().unwrap(), "some");
let sub_matches = matches.subcommand_matches("some").unwrap();
assert!(sub_matches.is_present("test"));
@ -34,7 +35,8 @@ fn flag_subcommand_normal_with_alias() {
)
.alias("result"),
)
.get_matches_from(vec!["myprog", "result", "--test"]);
.try_get_matches_from(vec!["myprog", "result", "--test"])
.unwrap();
assert_eq!(matches.subcommand_name().unwrap(), "some");
let sub_matches = matches.subcommand_matches("some").unwrap();
assert!(sub_matches.is_present("test"));
@ -51,7 +53,8 @@ fn flag_subcommand_short() {
.help("testing testing"),
),
)
.get_matches_from(vec!["myprog", "-S", "--test"]);
.try_get_matches_from(vec!["myprog", "-S", "--test"])
.unwrap();
assert_eq!(matches.subcommand_name().unwrap(), "some");
let sub_matches = matches.subcommand_matches("some").unwrap();
assert!(sub_matches.is_present("test"));
@ -68,7 +71,8 @@ fn flag_subcommand_short_with_args() {
.help("testing testing"),
),
)
.get_matches_from(vec!["myprog", "-St"]);
.try_get_matches_from(vec!["myprog", "-St"])
.unwrap();
assert_eq!(matches.subcommand_name().unwrap(), "some");
let sub_matches = matches.subcommand_matches("some").unwrap();
assert!(sub_matches.is_present("test"));
@ -89,7 +93,8 @@ fn flag_subcommand_short_with_alias() {
.short_flag_alias('M')
.short_flag_alias('B'),
)
.get_matches_from(vec!["myprog", "-Bt"]);
.try_get_matches_from(vec!["myprog", "-Bt"])
.unwrap();
assert_eq!(matches.subcommand_name().unwrap(), "some");
let sub_matches = matches.subcommand_matches("some").unwrap();
assert!(sub_matches.is_present("test"));
@ -99,7 +104,8 @@ fn flag_subcommand_short_with_alias() {
fn flag_subcommand_short_with_alias_same_as_short_flag() {
let matches = App::new("test")
.subcommand(App::new("some").short_flag('S').short_flag_alias('S'))
.get_matches_from(vec!["myprog", "-S"]);
.try_get_matches_from(vec!["myprog", "-S"])
.unwrap();
assert_eq!(matches.subcommand_name().unwrap(), "some");
}
@ -107,7 +113,8 @@ fn flag_subcommand_short_with_alias_same_as_short_flag() {
fn flag_subcommand_long_with_alias_same_as_long_flag() {
let matches = App::new("test")
.subcommand(App::new("some").long_flag("sync").long_flag_alias("sync"))
.get_matches_from(vec!["myprog", "--sync"]);
.try_get_matches_from(vec!["myprog", "--sync"])
.unwrap();
assert_eq!(matches.subcommand_name().unwrap(), "some");
}
@ -126,15 +133,15 @@ fn flag_subcommand_short_with_aliases_vis_and_hidden() {
.short_flag_alias('C'),
);
let app1 = app.clone();
let matches1 = app1.get_matches_from(vec!["test", "-M"]);
let matches1 = app1.try_get_matches_from(vec!["test", "-M"]).unwrap();
assert_eq!(matches1.subcommand_name().unwrap(), "some");
let app2 = app.clone();
let matches2 = app2.get_matches_from(vec!["test", "-C"]);
let matches2 = app2.try_get_matches_from(vec!["test", "-C"]).unwrap();
assert_eq!(matches2.subcommand_name().unwrap(), "some");
let app3 = app.clone();
let matches3 = app3.get_matches_from(vec!["test", "-B"]);
let matches3 = app3.try_get_matches_from(vec!["test", "-B"]).unwrap();
assert_eq!(matches3.subcommand_name().unwrap(), "some");
}
@ -152,7 +159,8 @@ fn flag_subcommand_short_with_aliases() {
)
.short_flag_aliases(&['M', 'B']),
)
.get_matches_from(vec!["myprog", "-Bt"]);
.try_get_matches_from(vec!["myprog", "-Bt"])
.unwrap();
assert_eq!(matches.subcommand_name().unwrap(), "some");
let sub_matches = matches.subcommand_matches("some").unwrap();
assert!(sub_matches.is_present("test"));
@ -173,7 +181,8 @@ fn flag_subcommand_short_with_alias_hyphen() {
)
.short_flag_alias('-'),
)
.get_matches_from(vec!["myprog", "-Bt"]);
.try_get_matches_from(vec!["myprog", "-Bt"])
.unwrap();
}
#[test]
@ -191,7 +200,8 @@ fn flag_subcommand_short_with_aliases_hyphen() {
)
.short_flag_aliases(&['-', '-', '-']),
)
.get_matches_from(vec!["myprog", "-Bt"]);
.try_get_matches_from(vec!["myprog", "-Bt"])
.unwrap();
}
#[test]
@ -203,7 +213,8 @@ fn flag_subcommand_short_after_long_arg() {
.arg(Arg::new("clean").short('c')),
)
.arg(Arg::new("arg").long("arg").takes_value(true))
.get_matches_from(vec!["pacman", "--arg", "foo", "-Sc"]);
.try_get_matches_from(vec!["pacman", "--arg", "foo", "-Sc"])
.unwrap();
let subm = m.subcommand_matches("sync");
assert!(subm.is_some());
let subm = subm.unwrap();
@ -221,7 +232,8 @@ fn flag_subcommand_long() {
.help("testing testing"),
),
)
.get_matches_from(vec!["myprog", "--some", "--test"]);
.try_get_matches_from(vec!["myprog", "--some", "--test"])
.unwrap();
assert_eq!(matches.subcommand_name().unwrap(), "some");
let sub_matches = matches.subcommand_matches("some").unwrap();
assert!(sub_matches.is_present("test"));
@ -241,7 +253,8 @@ fn flag_subcommand_long_with_alias() {
)
.long_flag_alias("result"),
)
.get_matches_from(vec!["myprog", "--result", "--test"]);
.try_get_matches_from(vec!["myprog", "--result", "--test"])
.unwrap();
assert_eq!(matches.subcommand_name().unwrap(), "some");
let sub_matches = matches.subcommand_matches("some").unwrap();
assert!(sub_matches.is_present("test"));
@ -261,7 +274,8 @@ fn flag_subcommand_long_with_aliases() {
)
.long_flag_aliases(&["result", "someall"]),
)
.get_matches_from(vec!["myprog", "--result", "--test"]);
.try_get_matches_from(vec!["myprog", "--result", "--test"])
.unwrap();
assert_eq!(matches.subcommand_name().unwrap(), "some");
let sub_matches = matches.subcommand_matches("some").unwrap();
assert!(sub_matches.is_present("test"));
@ -284,7 +298,8 @@ fn flag_subcommand_multiple() {
.arg(arg!(-p --print "print something")),
),
)
.get_matches_from(vec!["myprog", "-SfpRfp"]);
.try_get_matches_from(vec!["myprog", "-SfpRfp"])
.unwrap();
assert_eq!(matches.subcommand_name().unwrap(), "some");
let sub_matches = matches.subcommand_matches("some").unwrap();
assert!(sub_matches.is_present("flag"));
@ -302,7 +317,8 @@ fn flag_subcommand_short_conflict_with_arg() {
let _ = App::new("test")
.subcommand(App::new("some").short_flag('f').long_flag("some"))
.arg(Arg::new("test").short('f'))
.get_matches_from(vec!["myprog", "-f"]);
.try_get_matches_from(vec!["myprog", "-f"])
.unwrap();
}
#[cfg(debug_assertions)]
@ -312,7 +328,8 @@ fn flag_subcommand_short_conflict_with_alias() {
let _ = App::new("test")
.subcommand(App::new("some").short_flag('f').long_flag("some"))
.subcommand(App::new("result").short_flag('t').short_flag_alias('f'))
.get_matches_from(vec!["myprog", "-f"]);
.try_get_matches_from(vec!["myprog", "-f"])
.unwrap();
}
#[cfg(debug_assertions)]
@ -322,7 +339,8 @@ fn flag_subcommand_long_conflict_with_alias() {
let _ = App::new("test")
.subcommand(App::new("some").long_flag("flag"))
.subcommand(App::new("result").long_flag("test").long_flag_alias("flag"))
.get_matches_from(vec!["myprog", "--flag"]);
.try_get_matches_from(vec!["myprog", "--flag"])
.unwrap();
}
#[cfg(debug_assertions)]
@ -332,7 +350,8 @@ fn flag_subcommand_short_conflict_with_arg_alias() {
let _ = App::new("test")
.subcommand(App::new("some").short_flag('f').long_flag("some"))
.arg(Arg::new("test").short('t').short_alias('f'))
.get_matches_from(vec!["myprog", "-f"]);
.try_get_matches_from(vec!["myprog", "-f"])
.unwrap();
}
#[cfg(debug_assertions)]
@ -342,7 +361,8 @@ fn flag_subcommand_long_conflict_with_arg_alias() {
let _ = App::new("test")
.subcommand(App::new("some").short_flag('f').long_flag("some"))
.arg(Arg::new("test").long("test").alias("some"))
.get_matches_from(vec!["myprog", "--some"]);
.try_get_matches_from(vec!["myprog", "--some"])
.unwrap();
}
#[cfg(debug_assertions)]
@ -352,21 +372,24 @@ fn flag_subcommand_long_conflict_with_arg() {
let _ = App::new("test")
.subcommand(App::new("some").short_flag('a').long_flag("flag"))
.arg(Arg::new("flag").long("flag"))
.get_matches_from(vec!["myprog", "--flag"]);
.try_get_matches_from(vec!["myprog", "--flag"])
.unwrap();
}
#[test]
fn flag_subcommand_conflict_with_help() {
let _ = App::new("test")
.subcommand(App::new("help").short_flag('h').long_flag("help"))
.get_matches_from(vec!["myprog", "--help"]);
.try_get_matches_from(vec!["myprog", "--help"])
.unwrap();
}
#[test]
fn flag_subcommand_conflict_with_version() {
let _ = App::new("test")
.subcommand(App::new("ver").short_flag('V').long_flag("version"))
.get_matches_from(vec!["myprog", "--version"]);
.try_get_matches_from(vec!["myprog", "--version"])
.unwrap();
}
#[test]
@ -374,7 +397,8 @@ fn flag_subcommand_long_infer_pass() {
let m = App::new("prog")
.setting(AppSettings::InferSubcommands)
.subcommand(App::new("test").long_flag("test"))
.get_matches_from(vec!["prog", "--te"]);
.try_get_matches_from(vec!["prog", "--te"])
.unwrap();
assert_eq!(m.subcommand_name(), Some("test"));
}
@ -408,7 +432,8 @@ fn flag_subcommand_long_infer_pass_close() {
.setting(AppSettings::InferSubcommands)
.subcommand(App::new("test").long_flag("test"))
.subcommand(App::new("temp").long_flag("temp"))
.get_matches_from(vec!["prog", "--tes"]);
.try_get_matches_from(vec!["prog", "--tes"])
.unwrap();
assert_eq!(m.subcommand_name(), Some("test"));
}
@ -419,7 +444,8 @@ fn flag_subcommand_long_infer_exact_match() {
.subcommand(App::new("test").long_flag("test"))
.subcommand(App::new("testa").long_flag("testa"))
.subcommand(App::new("testb").long_flag("testb"))
.get_matches_from(vec!["prog", "--test"]);
.try_get_matches_from(vec!["prog", "--test"])
.unwrap();
assert_eq!(m.subcommand_name(), Some("test"));
}

View file

@ -19,7 +19,8 @@ fn flag_using_short() {
arg!(-f --flag "some flag"),
arg!(-c --color "some other flag"),
])
.get_matches_from(vec!["", "-f", "-c"]);
.try_get_matches_from(vec!["", "-f", "-c"])
.unwrap();
assert!(m.is_present("flag"));
assert!(m.is_present("color"));
}
@ -80,7 +81,8 @@ fn lots_o_flags_combined() {
fn flag_using_long() {
let m = App::new("flag")
.args(&[arg!(--flag "some flag"), arg!(--color "some other flag")])
.get_matches_from(vec!["", "--flag", "--color"]);
.try_get_matches_from(vec!["", "--flag", "--color"])
.unwrap();
assert!(m.is_present("flag"));
assert!(m.is_present("color"));
}
@ -103,7 +105,8 @@ fn flag_using_mixed() {
arg!(-f --flag "some flag"),
arg!(-c --color "some other flag"),
])
.get_matches_from(vec!["", "-f", "--color"]);
.try_get_matches_from(vec!["", "-f", "--color"])
.unwrap();
assert!(m.is_present("flag"));
assert!(m.is_present("color"));
@ -112,7 +115,8 @@ fn flag_using_mixed() {
arg!(-f --flag "some flag"),
arg!(-c --color "some other flag"),
])
.get_matches_from(vec!["", "--flag", "-c"]);
.try_get_matches_from(vec!["", "--flag", "-c"])
.unwrap();
assert!(m.is_present("flag"));
assert!(m.is_present("color"));
}
@ -125,7 +129,8 @@ fn multiple_flags_in_single() {
arg!(-c --color "some other flag"),
arg!(-d --debug "another other flag"),
])
.get_matches_from(vec!["", "-fcd"]);
.try_get_matches_from(vec!["", "-fcd"])
.unwrap();
assert!(m.is_present("flag"));
assert!(m.is_present("color"));
assert!(m.is_present("debug"));
@ -139,13 +144,20 @@ fn issue_1284_argument_in_flag_style() {
let m = app
.clone()
.get_matches_from(vec!["", "--", "--another-flag"]);
.try_get_matches_from(vec!["", "--", "--another-flag"])
.unwrap();
assert_eq!(m.value_of("filename"), Some("--another-flag"));
let m = app.clone().get_matches_from(vec!["", "--a-flag"]);
let m = app
.clone()
.try_get_matches_from(vec!["", "--a-flag"])
.unwrap();
assert!(m.is_present("a-flag"));
let m = app.clone().get_matches_from(vec!["", "--", "--a-flag"]);
let m = app
.clone()
.try_get_matches_from(vec!["", "--", "--a-flag"])
.unwrap();
assert_eq!(m.value_of("filename"), Some("--a-flag"));
assert!(utils::compare_output(

View file

@ -32,7 +32,8 @@ fn propagate_global_arg_in_subcommand_to_subsubcommand_1385() {
.arg(Arg::new("arg1").long("arg1").takes_value(true).global(true))
.subcommand(App::new("sub1a")),
)
.get_matches_from(&["foo", "sub1", "--arg1", "v1", "sub1a"]);
.try_get_matches_from(&["foo", "sub1", "--arg1", "v1", "sub1a"])
.unwrap();
assert_eq!(
"v1",
m1.subcommand_matches("sub1")
@ -55,7 +56,7 @@ fn propagate_global_arg_to_subcommand_in_subsubcommand_2053() {
.arg(arg!(--"sub-str" <str>).required(false).global(true))
.subcommand(App::new("test")),
)
.get_matches_from(&[
.try_get_matches_from(&[
"app",
"test",
"test",
@ -65,7 +66,8 @@ fn propagate_global_arg_to_subcommand_in_subsubcommand_2053() {
"--sub-flag",
"--sub-str",
"world",
]);
])
.unwrap();
assert_eq!(
Some("world"),
m.subcommand_matches("test").unwrap().value_of("sub-str")
@ -80,7 +82,8 @@ fn global_arg_available_in_subcommand() {
Arg::new("not").global(false).long("not"),
])
.subcommand(App::new("ping"))
.get_matches_from(&["opt", "ping", "--global"]);
.try_get_matches_from(&["opt", "ping", "--global"])
.unwrap();
assert!(m.is_present("global"));
assert!(m.subcommand_matches("ping").unwrap().is_present("global"));

View file

@ -12,7 +12,7 @@ fn grouped_value_works() {
.multiple_values(true)
.multiple_occurrences(true),
)
.get_matches_from(&[
.try_get_matches_from(&[
"cli",
"--option",
"fr_FR:mon option 1",
@ -20,7 +20,8 @@ fn grouped_value_works() {
"--option",
"fr_FR:mon option 2",
"en_US:my option 2",
]);
])
.unwrap();
let grouped_vals: Vec<_> = m.grouped_values_of("option").unwrap().collect();
assert_eq!(
grouped_vals,
@ -43,11 +44,12 @@ fn issue_1026() {
.multiple_values(true)
.multiple_occurrences(true),
)
.get_matches_from(&[
.try_get_matches_from(&[
"backup", "-s", "server", "-u", "user", "--target", "target1", "file1", "file2",
"file3", "--target", "target2", "file4", "file5", "file6", "file7", "--target",
"target3", "file8",
]);
])
.unwrap();
let grouped_vals: Vec<_> = m.grouped_values_of("target").unwrap().collect();
assert_eq!(
grouped_vals,
@ -70,13 +72,14 @@ fn grouped_value_long_flag_delimiter() {
.multiple_values(true)
.multiple_occurrences(true),
)
.get_matches_from(vec![
.try_get_matches_from(vec![
"myapp",
"--option=hmm",
"--option=val1,val2,val3",
"--option",
"alice,bob",
]);
])
.unwrap();
let grouped_vals: Vec<_> = m.grouped_values_of("option").unwrap().collect();
assert_eq!(
grouped_vals,
@ -99,7 +102,8 @@ fn grouped_value_short_flag_delimiter() {
.multiple_values(true)
.multiple_occurrences(true),
)
.get_matches_from(vec!["myapp", "-o=foo", "-o=val1,val2,val3", "-o=bar"]);
.try_get_matches_from(vec!["myapp", "-o=foo", "-o=val1,val2,val3", "-o=bar"])
.unwrap();
let grouped_vals: Vec<_> = m.grouped_values_of("option").unwrap().collect();
assert_eq!(
grouped_vals,
@ -116,9 +120,10 @@ fn grouped_value_positional_arg() {
.takes_value(true)
.multiple_values(true),
)
.get_matches_from(vec![
.try_get_matches_from(vec![
"myprog", "val1", "val2", "val3", "val4", "val5", "val6",
]);
])
.unwrap();
let grouped_vals: Vec<_> = m.grouped_values_of("pos").unwrap().collect();
assert_eq!(
grouped_vals,
@ -136,9 +141,10 @@ fn grouped_value_multiple_positional_arg() {
.takes_value(true)
.multiple_values(true),
)
.get_matches_from(vec![
.try_get_matches_from(vec![
"myprog", "val1", "val2", "val3", "val4", "val5", "val6",
]);
])
.unwrap();
let grouped_vals: Vec<_> = m.grouped_values_of("pos2").unwrap().collect();
assert_eq!(
grouped_vals,
@ -157,9 +163,10 @@ fn grouped_value_multiple_positional_arg_last_multiple() {
.multiple_values(true)
.last(true),
)
.get_matches_from(vec![
.try_get_matches_from(vec![
"myprog", "val1", "--", "val2", "val3", "val4", "val5", "val6",
]);
])
.unwrap();
let grouped_vals: Vec<_> = m.grouped_values_of("pos2").unwrap().collect();
assert_eq!(
grouped_vals,
@ -179,12 +186,14 @@ fn issue_1374() {
);
let matches = app
.clone()
.get_matches_from(&["MyApp", "--input", "a", "b", "c", "--input", "d"]);
.try_get_matches_from(&["MyApp", "--input", "a", "b", "c", "--input", "d"])
.unwrap();
let vs = matches.values_of("input").unwrap();
assert_eq!(vs.collect::<Vec<_>>(), vec!["a", "b", "c", "d"]);
let matches = app
.clone()
.get_matches_from(&["MyApp", "--input", "a", "b", "--input", "c", "d"]);
.try_get_matches_from(&["MyApp", "--input", "a", "b", "--input", "c", "d"])
.unwrap();
let vs = matches.values_of("input").unwrap();
assert_eq!(vs.collect::<Vec<_>>(), vec!["a", "b", "c", "d"]);
}

View file

@ -307,14 +307,14 @@ fn issue_1794() {
.required(true),
);
let m = app.clone().get_matches_from(&["app", "pos1", "pos2"]);
let m = app.clone().try_get_matches_from(&["app", "pos1", "pos2"]).unwrap();
assert_eq!(m.value_of("pos1"), Some("pos1"));
assert_eq!(m.value_of("pos2"), Some("pos2"));
assert!(!m.is_present("option1"));
let m = app
.clone()
.get_matches_from(&["app", "--option1", "positional"]);
.try_get_matches_from(&["app", "--option1", "positional"]).unwrap();
assert_eq!(m.value_of("pos1"), None);
assert_eq!(m.value_of("pos2"), Some("positional"));
assert!(m.is_present("option1"));

View file

@ -2009,7 +2009,8 @@ fn help_required_but_not_given() {
App::new("myapp")
.setting(AppSettings::HelpExpected)
.arg(Arg::new("foo"))
.get_matches_from(empty_args());
.try_get_matches_from(empty_args())
.unwrap();
}
#[cfg(debug_assertions)]
@ -2019,7 +2020,8 @@ fn help_required_but_not_given_settings_after_args() {
App::new("myapp")
.arg(Arg::new("foo"))
.setting(AppSettings::HelpExpected)
.get_matches_from(empty_args());
.try_get_matches_from(empty_args())
.unwrap();
}
#[cfg(debug_assertions)]
@ -2030,7 +2032,8 @@ fn help_required_but_not_given_for_one_of_two_arguments() {
.setting(AppSettings::HelpExpected)
.arg(Arg::new("foo"))
.arg(Arg::new("bar").help("It does bar stuff"))
.get_matches_from(empty_args());
.try_get_matches_from(empty_args())
.unwrap();
}
#[test]
@ -2043,7 +2046,8 @@ fn help_required_locally_but_not_given_for_subcommand() {
.arg(Arg::new("create").help("creates bar"))
.arg(Arg::new("delete")),
)
.get_matches_from(empty_args());
.try_get_matches_from(empty_args())
.unwrap();
}
#[cfg(debug_assertions)]
@ -2058,7 +2062,8 @@ fn help_required_globally_but_not_given_for_subcommand() {
.arg(Arg::new("create").help("creates bar"))
.arg(Arg::new("delete")),
)
.get_matches_from(empty_args());
.try_get_matches_from(empty_args())
.unwrap();
}
#[test]
@ -2071,7 +2076,8 @@ fn help_required_and_given_for_subcommand() {
.arg(Arg::new("create").help("creates bar"))
.arg(Arg::new("delete").help("deletes bar")),
)
.get_matches_from(empty_args());
.try_get_matches_from(empty_args())
.unwrap();
}
#[test]
@ -2079,14 +2085,16 @@ fn help_required_and_given() {
App::new("myapp")
.setting(AppSettings::HelpExpected)
.arg(Arg::new("foo").help("It does foo stuff"))
.get_matches_from(empty_args());
.try_get_matches_from(empty_args())
.unwrap();
}
#[test]
fn help_required_and_no_args() {
App::new("myapp")
.setting(AppSettings::HelpExpected)
.get_matches_from(empty_args());
.try_get_matches_from(empty_args())
.unwrap();
}
#[test]
@ -2627,7 +2635,7 @@ fn override_help_subcommand() {
.subcommand(App::new("help").arg(Arg::new("arg").takes_value(true)))
.subcommand(App::new("not_help").arg(Arg::new("arg").takes_value(true)))
.setting(AppSettings::DisableHelpSubcommand);
let matches = app.get_matches_from(&["bar", "help", "foo"]);
let matches = app.try_get_matches_from(&["bar", "help", "foo"]).unwrap();
assert_eq!(
matches.subcommand_matches("help").unwrap().value_of("arg"),
Some("foo")
@ -2639,7 +2647,7 @@ fn override_help_flag_using_long() {
let app = App::new("foo")
.subcommand(App::new("help").long_flag("help"))
.setting(AppSettings::DisableHelpFlag);
let matches = app.get_matches_from(&["foo", "--help"]);
let matches = app.try_get_matches_from(&["foo", "--help"]).unwrap();
assert!(matches.subcommand_matches("help").is_some());
}
@ -2648,7 +2656,7 @@ fn override_help_flag_using_short() {
let app = App::new("foo")
.setting(AppSettings::DisableHelpFlag)
.subcommand(App::new("help").short_flag('h'));
let matches = app.get_matches_from(&["foo", "-h"]);
let matches = app.try_get_matches_from(&["foo", "-h"]).unwrap();
assert!(matches.subcommand_matches("help").is_some());
}

View file

@ -96,13 +96,15 @@ fn subcommand() {
)
.arg(Arg::new("other").long("other"));
let m = app.get_matches_from(vec![
let m = app
.try_get_matches_from(vec![
"myprog",
"some",
"--test", /* missing: ,"some val" */
"-x",
"some other val",
]);
])
.unwrap();
assert_eq!(m.subcommand_name().unwrap(), "some");
let sub_m = m.subcommand_matches("some").unwrap();

View file

@ -16,7 +16,8 @@ fn indices_mult_opts() {
.takes_value(true)
.multiple_values(true),
)
.get_matches_from(vec!["ind", "-e", "A", "B", "-i", "B", "C", "-e", "C"]);
.try_get_matches_from(vec!["ind", "-e", "A", "B", "-i", "B", "C", "-e", "C"])
.unwrap();
assert_eq!(
m.indices_of("exclude").unwrap().collect::<Vec<_>>(),
@ -44,7 +45,8 @@ fn index_mult_opts() {
.takes_value(true)
.multiple_values(true),
)
.get_matches_from(vec!["ind", "-e", "A", "B", "-i", "B", "C", "-e", "C"]);
.try_get_matches_from(vec!["ind", "-e", "A", "B", "-i", "B", "C", "-e", "C"])
.unwrap();
assert_eq!(m.index_of("exclude"), Some(2));
assert_eq!(m.index_of("include"), Some(5));
@ -55,7 +57,8 @@ fn index_flag() {
let m = App::new("ind")
.arg(Arg::new("exclude").short('e'))
.arg(Arg::new("include").short('i'))
.get_matches_from(vec!["ind", "-e", "-i"]);
.try_get_matches_from(vec!["ind", "-e", "-i"])
.unwrap();
assert_eq!(m.index_of("exclude"), Some(1));
assert_eq!(m.index_of("include"), Some(2));
@ -66,7 +69,8 @@ fn index_flags() {
let m = App::new("ind")
.arg(Arg::new("exclude").short('e').multiple_occurrences(true))
.arg(Arg::new("include").short('i').multiple_occurrences(true))
.get_matches_from(vec!["ind", "-e", "-i", "-e", "-e", "-i"]);
.try_get_matches_from(vec!["ind", "-e", "-i", "-e", "-e", "-i"])
.unwrap();
assert_eq!(m.index_of("exclude"), Some(1));
assert_eq!(m.index_of("include"), Some(2));
@ -77,7 +81,8 @@ fn indices_mult_flags() {
let m = App::new("ind")
.arg(Arg::new("exclude").short('e').multiple_occurrences(true))
.arg(Arg::new("include").short('i').multiple_occurrences(true))
.get_matches_from(vec!["ind", "-e", "-i", "-e", "-e", "-i"]);
.try_get_matches_from(vec!["ind", "-e", "-i", "-e", "-e", "-i"])
.unwrap();
assert_eq!(
m.indices_of("exclude").unwrap().collect::<Vec<_>>(),
@ -94,7 +99,8 @@ fn indices_mult_flags_combined() {
let m = App::new("ind")
.arg(Arg::new("exclude").short('e').multiple_occurrences(true))
.arg(Arg::new("include").short('i').multiple_occurrences(true))
.get_matches_from(vec!["ind", "-eieei"]);
.try_get_matches_from(vec!["ind", "-eieei"])
.unwrap();
assert_eq!(
m.indices_of("exclude").unwrap().collect::<Vec<_>>(),
@ -112,7 +118,8 @@ fn indices_mult_flags_opt_combined() {
.arg(Arg::new("exclude").short('e').multiple_occurrences(true))
.arg(Arg::new("include").short('i').multiple_occurrences(true))
.arg(Arg::new("option").short('o').takes_value(true))
.get_matches_from(vec!["ind", "-eieeio", "val"]);
.try_get_matches_from(vec!["ind", "-eieeio", "val"])
.unwrap();
assert_eq!(
m.indices_of("exclude").unwrap().collect::<Vec<_>>(),
@ -131,7 +138,8 @@ fn indices_mult_flags_opt_combined_eq() {
.arg(Arg::new("exclude").short('e').multiple_occurrences(true))
.arg(Arg::new("include").short('i').multiple_occurrences(true))
.arg(Arg::new("option").short('o').takes_value(true))
.get_matches_from(vec!["ind", "-eieeio=val"]);
.try_get_matches_from(vec!["ind", "-eieeio=val"])
.unwrap();
assert_eq!(
m.indices_of("exclude").unwrap().collect::<Vec<_>>(),
@ -154,7 +162,8 @@ fn indices_mult_opt_value_delim_eq() {
.use_delimiter(true)
.multiple_values(true),
)
.get_matches_from(vec!["myapp", "-o=val1,val2,val3"]);
.try_get_matches_from(vec!["myapp", "-o=val1,val2,val3"])
.unwrap();
assert_eq!(
m.indices_of("option").unwrap().collect::<Vec<_>>(),
&[2, 3, 4]
@ -170,7 +179,8 @@ fn indices_mult_opt_value_no_delim_eq() {
.takes_value(true)
.multiple_values(true),
)
.get_matches_from(vec!["myapp", "-o=val1,val2,val3"]);
.try_get_matches_from(vec!["myapp", "-o=val1,val2,val3"])
.unwrap();
assert_eq!(m.indices_of("option").unwrap().collect::<Vec<_>>(), &[2]);
}
@ -184,7 +194,8 @@ fn indices_mult_opt_mult_flag() {
.multiple_occurrences(true),
)
.arg(Arg::new("flag").short('f').multiple_occurrences(true))
.get_matches_from(vec!["myapp", "-o", "val1", "-f", "-o", "val2", "-f"]);
.try_get_matches_from(vec!["myapp", "-o", "val1", "-f", "-o", "val2", "-f"])
.unwrap();
assert_eq!(m.indices_of("option").unwrap().collect::<Vec<_>>(), &[2, 5]);
assert_eq!(m.indices_of("flag").unwrap().collect::<Vec<_>>(), &[3, 6]);

View file

@ -5,7 +5,8 @@ fn multiple_occurrences_of_flags_long() {
let m = App::new("mo_flags_long")
.arg(arg!(--multflag "allowed multiple flag").multiple_occurrences(true))
.arg(arg!(--flag "disallowed multiple flag"))
.get_matches_from(vec!["", "--multflag", "--flag", "--multflag"]);
.try_get_matches_from(vec!["", "--multflag", "--flag", "--multflag"])
.unwrap();
assert!(m.is_present("multflag"));
assert_eq!(m.occurrences_of("multflag"), 2);
assert!(m.is_present("flag"));
@ -17,7 +18,8 @@ fn multiple_occurrences_of_flags_short() {
let m = App::new("mo_flags_short")
.arg(arg!(-m --multflag "allowed multiple flag").multiple_occurrences(true))
.arg(arg!(-f --flag "disallowed multiple flag"))
.get_matches_from(vec!["", "-m", "-f", "-m"]);
.try_get_matches_from(vec!["", "-m", "-f", "-m"])
.unwrap();
assert!(m.is_present("multflag"));
assert_eq!(m.occurrences_of("multflag"), 2);
assert!(m.is_present("flag"));
@ -30,7 +32,7 @@ fn multiple_occurrences_of_flags_mixed() {
.arg(arg!(-m --multflag1 "allowed multiple flag").multiple_occurrences(true))
.arg(arg!(-n --multflag2 "another allowed multiple flag").multiple_occurrences(true))
.arg(arg!(-f --flag "disallowed multiple flag"))
.get_matches_from(vec![
.try_get_matches_from(vec![
"",
"-m",
"-f",
@ -38,7 +40,8 @@ fn multiple_occurrences_of_flags_mixed() {
"--multflag1",
"-m",
"--multflag2",
]);
])
.unwrap();
assert!(m.is_present("multflag1"));
assert_eq!(m.occurrences_of("multflag1"), 3);
assert!(m.is_present("multflag2"));
@ -87,7 +90,8 @@ fn multiple_occurrences_of_flags_large_quantity() {
.collect();
let m = App::new("mo_flags_large_qty")
.arg(arg!(-m --multflag "allowed multiple flag").multiple_occurrences(true))
.get_matches_from(args);
.try_get_matches_from(args)
.unwrap();
assert!(m.is_present("multflag"));
assert_eq!(m.occurrences_of("multflag"), 1024);
}

View file

@ -447,7 +447,8 @@ fn issue_1047_min_zero_vals_default_val() {
.min_values(0)
.default_missing_value("default"),
)
.get_matches_from(vec!["foo", "-d"]);
.try_get_matches_from(vec!["foo", "-d"])
.unwrap();
assert_eq!(m.occurrences_of("del"), 1);
assert_eq!(m.value_of("del"), Some("default"));
}
@ -531,7 +532,8 @@ fn issue_1073_suboptimal_flag_suggestion() {
fn short_non_ascii_no_space() {
let matches = App::new("app")
.arg(arg!(opt: -'磨' <opt>))
.get_matches_from(&["test", "-磨VALUE"]);
.try_get_matches_from(&["test", "-磨VALUE"])
.unwrap();
assert_eq!("VALUE", matches.value_of("opt").unwrap());
}
@ -540,7 +542,8 @@ fn short_non_ascii_no_space() {
fn short_eq_val_starts_with_eq() {
let matches = App::new("app")
.arg(arg!(opt: -f <opt>))
.get_matches_from(&["test", "-f==value"]);
.try_get_matches_from(&["test", "-f==value"])
.unwrap();
assert_eq!("=value", matches.value_of("opt").unwrap());
}
@ -549,7 +552,8 @@ fn short_eq_val_starts_with_eq() {
fn long_eq_val_starts_with_eq() {
let matches = App::new("app")
.arg(arg!(opt: --foo <opt>))
.get_matches_from(&["test", "--foo==value"]);
.try_get_matches_from(&["test", "--foo==value"])
.unwrap();
assert_eq!("=value", matches.value_of("opt").unwrap());
}
@ -559,7 +563,7 @@ fn issue_2022_get_flags_misuse() {
let app = App::new("test")
.help_heading(Some("test"))
.arg(Arg::new("a").long("a").default_value("32"));
let matches = app.get_matches_from(&[""]);
let matches = app.try_get_matches_from(&[""]).unwrap();
assert!(matches.value_of("a").is_some())
}
@ -568,14 +572,16 @@ fn issue_2279() {
let before_help_heading = App::new("app")
.arg(Arg::new("foo").short('f').default_value("bar"))
.help_heading(Some("This causes default_value to be ignored"))
.get_matches_from(&[""]);
.try_get_matches_from(&[""])
.unwrap();
assert_eq!(before_help_heading.value_of("foo"), Some("bar"));
let after_help_heading = App::new("app")
.help_heading(Some("This causes default_value to be ignored"))
.arg(Arg::new("foo").short('f').default_value("bar"))
.get_matches_from(&[""]);
.try_get_matches_from(&[""])
.unwrap();
assert_eq!(after_help_heading.value_of("foo"), Some("bar"));
}
@ -587,15 +593,24 @@ fn infer_long_arg() {
.arg(Arg::new("racetrack").long("racetrack").alias("autobahn"))
.arg(Arg::new("racecar").long("racecar").takes_value(true));
let matches = app.clone().get_matches_from(&["test", "--racec=hello"]);
let matches = app
.clone()
.try_get_matches_from(&["test", "--racec=hello"])
.unwrap();
assert!(!matches.is_present("racetrack"));
assert_eq!(matches.value_of("racecar"), Some("hello"));
let matches = app.clone().get_matches_from(&["test", "--racet"]);
let matches = app
.clone()
.try_get_matches_from(&["test", "--racet"])
.unwrap();
assert!(matches.is_present("racetrack"));
assert_eq!(matches.value_of("racecar"), None);
let matches = app.clone().get_matches_from(&["test", "--auto"]);
let matches = app
.clone()
.try_get_matches_from(&["test", "--auto"])
.unwrap();
assert!(matches.is_present("racetrack"));
assert_eq!(matches.value_of("racecar"), None);
@ -603,9 +618,9 @@ fn infer_long_arg() {
.setting(AppSettings::InferLongArgs)
.arg(Arg::new("arg").long("arg"));
let matches = app.clone().get_matches_from(&["test", "--"]);
let matches = app.clone().try_get_matches_from(&["test", "--"]).unwrap();
assert!(!matches.is_present("arg"));
let matches = app.clone().get_matches_from(&["test", "--a"]);
let matches = app.clone().try_get_matches_from(&["test", "--a"]).unwrap();
assert!(matches.is_present("arg"));
}

View file

@ -44,7 +44,8 @@ fn positional() {
let m = App::new("positional")
.args(&[arg!(-f --flag "some flag"), Arg::new("positional").index(1)])
.get_matches_from(vec!["", "test", "--flag"]);
.try_get_matches_from(vec!["", "test", "--flag"])
.unwrap();
assert!(m.is_present("positional"));
assert!(m.is_present("flag"));
assert_eq!(m.value_of("positional").unwrap(), "test");
@ -164,14 +165,16 @@ fn positional_possible_values() {
fn create_positional() {
let _ = App::new("test")
.arg(Arg::new("test").index(1).help("testing testing"))
.get_matches_from(vec![""]);
.try_get_matches_from(vec![""])
.unwrap();
}
#[test]
fn positional_hyphen_does_not_panic() {
let _ = App::new("test")
.arg(Arg::new("dummy"))
.get_matches_from(vec!["test", "-"]);
.try_get_matches_from(vec!["test", "-"])
.unwrap();
}
#[test]

View file

@ -102,7 +102,8 @@ fn option_use_delim_false_override_itself() {
.required(false)
.overrides_with("opt"),
)
.get_matches_from(vec!["", "--opt=some,other", "--opt=one,two"]);
.try_get_matches_from(vec!["", "--opt=some,other", "--opt=one,two"])
.unwrap();
assert!(m.is_present("opt"));
assert_eq!(m.occurrences_of("opt"), 1);
assert_eq!(
@ -131,7 +132,8 @@ fn posix_compatible_flags_long() {
let m = App::new("posix")
.arg(arg!(--flag "some flag").overrides_with("color"))
.arg(arg!(--color "some other flag"))
.get_matches_from(vec!["", "--flag", "--color"]);
.try_get_matches_from(vec!["", "--flag", "--color"])
.unwrap();
assert!(m.is_present("color"));
assert!(!m.is_present("flag"));
}
@ -141,7 +143,8 @@ fn posix_compatible_flags_long_rev() {
let m = App::new("posix")
.arg(arg!(--flag "some flag").overrides_with("color"))
.arg(arg!(--color "some other flag"))
.get_matches_from(vec!["", "--color", "--flag"]);
.try_get_matches_from(vec!["", "--color", "--flag"])
.unwrap();
assert!(!m.is_present("color"));
assert!(m.is_present("flag"));
}
@ -151,7 +154,8 @@ fn posix_compatible_flags_short() {
let m = App::new("posix")
.arg(arg!(-f --flag "some flag").overrides_with("color"))
.arg(arg!(-c --color "some other flag"))
.get_matches_from(vec!["", "-f", "-c"]);
.try_get_matches_from(vec!["", "-f", "-c"])
.unwrap();
assert!(m.is_present("color"));
assert!(!m.is_present("flag"));
}
@ -161,7 +165,8 @@ fn posix_compatible_flags_short_rev() {
let m = App::new("posix")
.arg(arg!(-f --flag "some flag").overrides_with("color"))
.arg(arg!(-c --color "some other flag"))
.get_matches_from(vec!["", "-c", "-f"]);
.try_get_matches_from(vec!["", "-c", "-f"])
.unwrap();
assert!(!m.is_present("color"));
assert!(m.is_present("flag"));
}
@ -175,7 +180,8 @@ fn posix_compatible_opts_long() {
.overrides_with("color"),
)
.arg(arg!(--color <color> "some other flag").required(false))
.get_matches_from(vec!["", "--flag", "some", "--color", "other"]);
.try_get_matches_from(vec!["", "--flag", "some", "--color", "other"])
.unwrap();
assert!(m.is_present("color"));
assert_eq!(m.value_of("color").unwrap(), "other");
assert!(!m.is_present("flag"));
@ -190,7 +196,8 @@ fn posix_compatible_opts_long_rev() {
.overrides_with("color"),
)
.arg(arg!(--color <color> "some other flag").required(false))
.get_matches_from(vec!["", "--color", "some", "--flag", "other"]);
.try_get_matches_from(vec!["", "--color", "some", "--flag", "other"])
.unwrap();
assert!(!m.is_present("color"));
assert!(m.is_present("flag"));
assert_eq!(m.value_of("flag").unwrap(), "other");
@ -205,7 +212,8 @@ fn posix_compatible_opts_long_equals() {
.overrides_with("color"),
)
.arg(arg!(--color <color> "some other flag").required(false))
.get_matches_from(vec!["", "--flag=some", "--color=other"]);
.try_get_matches_from(vec!["", "--flag=some", "--color=other"])
.unwrap();
assert!(m.is_present("color"));
assert_eq!(m.value_of("color").unwrap(), "other");
assert!(!m.is_present("flag"));
@ -220,7 +228,8 @@ fn posix_compatible_opts_long_equals_rev() {
.overrides_with("color"),
)
.arg(arg!(--color <color> "some other flag").required(false))
.get_matches_from(vec!["", "--color=some", "--flag=other"]);
.try_get_matches_from(vec!["", "--color=some", "--flag=other"])
.unwrap();
assert!(!m.is_present("color"));
assert!(m.is_present("flag"));
assert_eq!(m.value_of("flag").unwrap(), "other");
@ -235,7 +244,8 @@ fn posix_compatible_opts_short() {
.overrides_with("c"),
)
.arg(arg!(c: -c <color> "some other flag").required(false))
.get_matches_from(vec!["", "-f", "some", "-c", "other"]);
.try_get_matches_from(vec!["", "-f", "some", "-c", "other"])
.unwrap();
assert!(m.is_present("c"));
assert_eq!(m.value_of("c").unwrap(), "other");
assert!(!m.is_present("f"));
@ -250,7 +260,8 @@ fn posix_compatible_opts_short_rev() {
.overrides_with("c"),
)
.arg(arg!(c: -c <color> "some other flag").required(false))
.get_matches_from(vec!["", "-c", "some", "-f", "other"]);
.try_get_matches_from(vec!["", "-c", "some", "-f", "other"])
.unwrap();
assert!(!m.is_present("c"));
assert!(m.is_present("f"));
assert_eq!(m.value_of("f").unwrap(), "other");
@ -262,7 +273,8 @@ fn conflict_overridden() {
.arg(arg!(-f --flag "some flag").conflicts_with("debug"))
.arg(arg!(-d --debug "other flag"))
.arg(arg!(-c --color "third flag").overrides_with("flag"))
.get_matches_from(vec!["", "-f", "-c", "-d"]);
.try_get_matches_from(vec!["", "-f", "-c", "-d"])
.unwrap();
assert!(m.is_present("color"));
assert!(!m.is_present("flag"));
assert!(m.is_present("debug"));
@ -300,7 +312,8 @@ fn conflict_overridden_4() {
.arg(arg!(-f --flag "some flag").conflicts_with("debug"))
.arg(arg!(-d --debug "other flag"))
.arg(arg!(-c --color "third flag").overrides_with("flag"))
.get_matches_from(vec!["", "-d", "-f", "-c"]);
.try_get_matches_from(vec!["", "-d", "-f", "-c"])
.unwrap();
assert!(m.is_present("color"));
assert!(!m.is_present("flag"));
assert!(m.is_present("debug"));
@ -320,7 +333,8 @@ fn require_overridden_2() {
let m = App::new("require_overridden")
.arg(Arg::new("req_pos").required(true))
.arg(arg!(-c --color "other flag").overrides_with("req_pos"))
.get_matches_from(vec!["", "-c", "req_pos"]);
.try_get_matches_from(vec!["", "-c", "req_pos"])
.unwrap();
assert!(!m.is_present("color"));
assert!(m.is_present("req_pos"));
}
@ -331,7 +345,8 @@ fn require_overridden_3() {
.arg(arg!(-f --flag "some flag").requires("debug"))
.arg(arg!(-d --debug "other flag"))
.arg(arg!(-c --color "third flag").overrides_with("flag"))
.get_matches_from(vec!["", "-f", "-c"]);
.try_get_matches_from(vec!["", "-f", "-c"])
.unwrap();
assert!(m.is_present("color"));
assert!(!m.is_present("flag"));
assert!(!m.is_present("debug"));
@ -360,11 +375,13 @@ fn issue_1374_overrides_self_with_multiple_values() {
);
let m = app
.clone()
.get_matches_from(&["test", "--input", "a", "b", "c", "--input", "d"]);
.try_get_matches_from(&["test", "--input", "a", "b", "c", "--input", "d"])
.unwrap();
assert_eq!(m.values_of("input").unwrap().collect::<Vec<_>>(), &["d"]);
let m = app
.clone()
.get_matches_from(&["test", "--input", "a", "b", "--input", "c", "d"]);
.try_get_matches_from(&["test", "--input", "a", "b", "--input", "c", "d"])
.unwrap();
assert_eq!(
m.values_of("input").unwrap().collect::<Vec<_>>(),
&["c", "d"]

View file

@ -21,7 +21,8 @@ fn get_app() -> App<'static> {
}
fn get_matches(app: App<'static>, argv: &'static str) -> ArgMatches {
app.get_matches_from(argv.split(' ').collect::<Vec<_>>())
app.try_get_matches_from(argv.split(' ').collect::<Vec<_>>())
.unwrap()
}
fn get_outer_matches(m: &ArgMatches) -> &ArgMatches {

View file

@ -65,7 +65,8 @@ fn flag_required_2() {
let m = App::new("flag_required")
.arg(arg!(-f --flag "some flag").requires("color"))
.arg(arg!(-c --color "third flag"))
.get_matches_from(vec!["", "-f", "-c"]);
.try_get_matches_from(vec!["", "-f", "-c"])
.unwrap();
assert!(m.is_present("color"));
assert!(m.is_present("flag"));
}
@ -86,7 +87,8 @@ fn option_required_2() {
let m = App::new("option_required")
.arg(arg!(f: -f <flag> "some flag").required(false).requires("c"))
.arg(arg!(c: -c <color> "third flag").required(false))
.get_matches_from(vec!["", "-f", "val", "-c", "other_val"]);
.try_get_matches_from(vec!["", "-f", "val", "-c", "other_val"])
.unwrap();
assert!(m.is_present("c"));
assert_eq!(m.value_of("c").unwrap(), "other_val");
assert!(m.is_present("f"));
@ -107,7 +109,8 @@ fn positional_required() {
fn positional_required_2() {
let m = App::new("positional_required")
.arg(Arg::new("flag").index(1).required(true))
.get_matches_from(vec!["", "someval"]);
.try_get_matches_from(vec!["", "someval"])
.unwrap();
assert!(m.is_present("flag"));
assert_eq!(m.value_of("flag").unwrap(), "someval");
}
@ -132,7 +135,8 @@ fn group_required_2() {
.group(ArgGroup::new("gr").required(true).arg("some").arg("other"))
.arg(arg!(--some "some arg"))
.arg(arg!(--other "other arg"))
.get_matches_from(vec!["", "-f", "--some"]);
.try_get_matches_from(vec!["", "-f", "--some"])
.unwrap();
assert!(m.is_present("some"));
assert!(!m.is_present("other"));
assert!(m.is_present("flag"));
@ -145,7 +149,8 @@ fn group_required_3() {
.group(ArgGroup::new("gr").required(true).arg("some").arg("other"))
.arg(arg!(--some "some arg"))
.arg(arg!(--other "other arg"))
.get_matches_from(vec!["", "-f", "--other"]);
.try_get_matches_from(vec!["", "-f", "--other"])
.unwrap();
assert!(!m.is_present("some"));
assert!(m.is_present("other"));
assert!(m.is_present("flag"));
@ -1048,7 +1053,8 @@ fn issue_1643_args_mutually_require_each_other() {
.requires("relation_id"),
);
app.get_matches_from(&["test", "-u", "hello", "-r", "farewell"]);
app.try_get_matches_from(&["test", "-u", "hello", "-r", "farewell"])
.unwrap();
}
#[test]
@ -1061,7 +1067,8 @@ fn short_flag_require_equals_with_minvals_zero() {
.require_equals(true),
)
.arg(Arg::new("unique").short('u'))
.get_matches_from(&["foo", "-cu"]);
.try_get_matches_from(&["foo", "-cu"])
.unwrap();
assert!(m.is_present("check"));
assert!(m.is_present("unique"));
}
@ -1078,7 +1085,8 @@ fn issue_2624() {
.possible_values(["silent", "quiet", "diagnose-first"]),
)
.arg(Arg::new("unique").short('u').long("unique"))
.get_matches_from(&["foo", "-cu"]);
.try_get_matches_from(&["foo", "-cu"])
.unwrap();
assert!(matches.is_present("check"));
assert!(matches.is_present("unique"));
}

View file

@ -110,7 +110,8 @@ fn subcommand() {
),
)
.arg(Arg::new("other").long("other"))
.get_matches_from(vec!["myprog", "some", "--test", "testing"]);
.try_get_matches_from(vec!["myprog", "some", "--test", "testing"])
.unwrap();
assert_eq!(m.subcommand_name().unwrap(), "some");
let sub_m = m.subcommand_matches("some").unwrap();
@ -131,7 +132,8 @@ fn subcommand_none_given() {
),
)
.arg(Arg::new("other").long("other"))
.get_matches_from(vec![""]);
.try_get_matches_from(vec![""])
.unwrap();
assert!(m.subcommand_name().is_none());
}
@ -150,7 +152,8 @@ fn subcommand_multiple() {
App::new("add").arg(Arg::new("roster").short('r')),
])
.arg(Arg::new("other").long("other"))
.get_matches_from(vec!["myprog", "some", "--test", "testing"]);
.try_get_matches_from(vec!["myprog", "some", "--test", "testing"])
.unwrap();
assert!(m.subcommand_matches("some").is_some());
assert!(m.subcommand_matches("add").is_none());
@ -202,7 +205,8 @@ fn subcommand_display_order() {
fn single_alias() {
let m = App::new("myprog")
.subcommand(App::new("test").alias("do-stuff"))
.get_matches_from(vec!["myprog", "do-stuff"]);
.try_get_matches_from(vec!["myprog", "do-stuff"])
.unwrap();
assert_eq!(m.subcommand_name(), Some("test"));
}
@ -210,7 +214,8 @@ fn single_alias() {
fn multiple_aliases() {
let m = App::new("myprog")
.subcommand(App::new("test").aliases(&["do-stuff", "test-stuff"]))
.get_matches_from(vec!["myprog", "test-stuff"]);
.try_get_matches_from(vec!["myprog", "test-stuff"])
.unwrap();
assert_eq!(m.subcommand_name(), Some("test"));
}
@ -334,7 +339,8 @@ fn replace() {
let m = App::new("prog")
.subcommand(App::new("module").subcommand(App::new("install").about("Install module")))
.replace("install", &["module", "install"])
.get_matches_from(vec!["prog", "install"]);
.try_get_matches_from(vec!["prog", "install"])
.unwrap();
assert_eq!(m.subcommand_name(), Some("module"));
assert_eq!(
@ -454,7 +460,8 @@ fn subcommand_after_argument() {
let m = App::new("myprog")
.arg(Arg::new("some_text"))
.subcommand(App::new("test"))
.get_matches_from(vec!["myprog", "teat", "test"]);
.try_get_matches_from(vec!["myprog", "teat", "test"])
.unwrap();
assert_eq!(m.value_of("some_text"), Some("teat"));
assert_eq!(m.subcommand().unwrap().0, "test");
}
@ -464,7 +471,8 @@ fn subcommand_after_argument_looks_like_help() {
let m = App::new("myprog")
.arg(Arg::new("some_text"))
.subcommand(App::new("test"))
.get_matches_from(vec!["myprog", "helt", "test"]);
.try_get_matches_from(vec!["myprog", "helt", "test"])
.unwrap();
assert_eq!(m.value_of("some_text"), Some("helt"));
assert_eq!(m.subcommand().unwrap().0, "test");
}
@ -475,15 +483,21 @@ fn issue_2494_subcommand_is_present() {
.arg(Arg::new("global").long("global"))
.subcommand(App::new("global"));
let m = app.clone().get_matches_from(&["opt", "--global", "global"]);
let m = app
.clone()
.try_get_matches_from(&["opt", "--global", "global"])
.unwrap();
assert_eq!(m.subcommand_name().unwrap(), "global");
assert!(m.is_present("global"));
let m = app.clone().get_matches_from(&["opt", "--global"]);
let m = app
.clone()
.try_get_matches_from(&["opt", "--global"])
.unwrap();
assert!(m.subcommand_name().is_none());
assert!(m.is_present("global"));
let m = app.get_matches_from(&["opt", "global"]);
let m = app.try_get_matches_from(&["opt", "global"]).unwrap();
assert_eq!(m.subcommand_name().unwrap(), "global");
assert!(!m.is_present("global"));
}
@ -519,11 +533,14 @@ fn busybox_like_multicall() {
.subcommand(App::new("busybox").subcommands(applet_commands()))
.subcommands(applet_commands());
let m = app.clone().get_matches_from(&["busybox", "true"]);
let m = app
.clone()
.try_get_matches_from(&["busybox", "true"])
.unwrap();
assert_eq!(m.subcommand_name(), Some("busybox"));
assert_eq!(m.subcommand().unwrap().1.subcommand_name(), Some("true"));
let m = app.clone().get_matches_from(&["true"]);
let m = app.clone().try_get_matches_from(&["true"]).unwrap();
assert_eq!(m.subcommand_name(), Some("true"));
let m = app.clone().try_get_matches_from(&["a.out"]);
@ -539,10 +556,13 @@ fn hostname_like_multicall() {
.subcommand(App::new("hostname"))
.subcommand(App::new("dnsdomainname"));
let m = app.clone().get_matches_from(&["hostname"]);
let m = app.clone().try_get_matches_from(&["hostname"]).unwrap();
assert_eq!(m.subcommand_name(), Some("hostname"));
let m = app.clone().get_matches_from(&["dnsdomainname"]);
let m = app
.clone()
.try_get_matches_from(&["dnsdomainname"])
.unwrap();
assert_eq!(m.subcommand_name(), Some("dnsdomainname"));
let m = app.clone().try_get_matches_from(&["a.out"]);

View file

@ -88,7 +88,9 @@ subcmd NOT present
pub fn check_complex_output(args: &str, out: &str) {
let mut w = vec![];
let matches = utils::complex_app().get_matches_from(args.split(' ').collect::<Vec<_>>());
let matches = utils::complex_app()
.try_get_matches_from(args.split(' ').collect::<Vec<_>>())
.unwrap();
if matches.is_present("flag") {
writeln!(w, "flag present {} times", matches.occurrences_of("flag")).unwrap();
} else {
@ -226,14 +228,16 @@ fn create_app() {
.version("1.0")
.author("kevin")
.about("does awesome things")
.get_matches_from(vec![""]);
.try_get_matches_from(vec![""])
.unwrap();
}
#[test]
fn add_multiple_arg() {
let _ = App::new("test")
.args(&[Arg::new("test").short('s'), Arg::new("test2").short('l')])
.get_matches_from(vec![""]);
.try_get_matches_from(vec![""])
.unwrap();
}
#[test]
fn flag_x2_opt() {