mirror of
https://github.com/clap-rs/clap
synced 2024-12-13 22:32:33 +00:00
test(Options): adds a test against issue 665
This commit is contained in:
parent
5a5f2b1e9f
commit
327c514d7a
1 changed files with 89 additions and 72 deletions
161
tests/opts.rs
161
tests/opts.rs
|
@ -8,7 +8,7 @@ use clap::{App, Arg, ErrorKind};
|
||||||
#[test]
|
#[test]
|
||||||
fn stdin_char() {
|
fn stdin_char() {
|
||||||
let r = App::new("opts")
|
let r = App::new("opts")
|
||||||
.arg( Arg::from_usage("-f [flag] 'some flag'") )
|
.arg(Arg::from_usage("-f [flag] 'some flag'"))
|
||||||
.get_matches_from_safe(vec!["", "-f", "-"]);
|
.get_matches_from_safe(vec!["", "-f", "-"]);
|
||||||
assert!(r.is_ok());
|
assert!(r.is_ok());
|
||||||
let m = r.unwrap();
|
let m = r.unwrap();
|
||||||
|
@ -19,10 +19,8 @@ fn stdin_char() {
|
||||||
#[test]
|
#[test]
|
||||||
fn opts_using_short() {
|
fn opts_using_short() {
|
||||||
let r = App::new("opts")
|
let r = App::new("opts")
|
||||||
.args(&[
|
.args(&[Arg::from_usage("-f [flag] 'some flag'"),
|
||||||
Arg::from_usage("-f [flag] 'some flag'"),
|
Arg::from_usage("-c [color] 'some other flag'")])
|
||||||
Arg::from_usage("-c [color] 'some other flag'")
|
|
||||||
])
|
|
||||||
.get_matches_from_safe(vec!["", "-f", "some", "-c", "other"]);
|
.get_matches_from_safe(vec!["", "-f", "some", "-c", "other"]);
|
||||||
assert!(r.is_ok());
|
assert!(r.is_ok());
|
||||||
let m = r.unwrap();
|
let m = r.unwrap();
|
||||||
|
@ -35,38 +33,50 @@ fn opts_using_short() {
|
||||||
#[test]
|
#[test]
|
||||||
fn lots_o_vals() {
|
fn lots_o_vals() {
|
||||||
let r = App::new("opts")
|
let r = App::new("opts")
|
||||||
.arg(
|
.arg(Arg::from_usage("-o [opt]... 'some opt'"))
|
||||||
Arg::from_usage("-o [opt]... 'some opt'"),
|
.get_matches_from_safe(vec!["", "-o", "some", "some", "some", "some", "some", "some",
|
||||||
)
|
"some", "some", "some", "some", "some", "some", "some",
|
||||||
.get_matches_from_safe(vec!["", "-o",
|
"some", "some", "some", "some", "some", "some", "some",
|
||||||
"some", "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
|
"some", "some", "some", "some", "some", "some", "some",
|
||||||
"some", "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
|
"some", "some", "some", "some", "some", "some", "some",
|
||||||
"some", "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
|
"some", "some", "some", "some", "some", "some", "some",
|
||||||
"some", "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
|
"some", "some", "some", "some", "some", "some", "some",
|
||||||
"some", "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
|
"some", "some", "some", "some", "some", "some", "some",
|
||||||
"some", "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
|
"some", "some", "some", "some", "some", "some", "some",
|
||||||
"some", "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
|
"some", "some", "some", "some", "some", "some", "some",
|
||||||
"some", "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
|
"some", "some", "some", "some", "some", "some", "some",
|
||||||
"some", "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
|
"some", "some", "some", "some", "some", "some", "some",
|
||||||
"some", "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
|
"some", "some", "some", "some", "some", "some", "some",
|
||||||
"some", "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
|
"some", "some", "some", "some", "some", "some", "some",
|
||||||
"some", "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
|
"some", "some", "some", "some", "some", "some", "some",
|
||||||
"some", "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
|
"some", "some", "some", "some", "some", "some", "some",
|
||||||
"some", "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
|
"some", "some", "some", "some", "some", "some", "some",
|
||||||
"some", "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
|
"some", "some", "some", "some", "some", "some", "some",
|
||||||
"some", "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
|
"some", "some", "some", "some", "some", "some", "some",
|
||||||
"some", "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
|
"some", "some", "some", "some", "some", "some", "some",
|
||||||
"some", "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
|
"some", "some", "some", "some", "some", "some", "some",
|
||||||
"some", "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
|
"some", "some", "some", "some", "some", "some", "some",
|
||||||
"some", "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
|
"some", "some", "some", "some", "some", "some", "some",
|
||||||
"some", "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
|
"some", "some", "some", "some", "some", "some", "some",
|
||||||
"some", "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
|
"some", "some", "some", "some", "some", "some", "some",
|
||||||
"some", "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
|
"some", "some", "some", "some", "some", "some", "some",
|
||||||
"some", "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
|
"some", "some", "some", "some", "some", "some", "some",
|
||||||
"some", "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
|
"some", "some", "some", "some", "some", "some", "some",
|
||||||
"some", "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
|
"some", "some", "some", "some", "some", "some", "some",
|
||||||
"some", "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
|
"some", "some", "some", "some", "some", "some", "some",
|
||||||
]);
|
"some", "some", "some", "some", "some", "some", "some",
|
||||||
|
"some", "some", "some", "some", "some", "some", "some",
|
||||||
|
"some", "some", "some", "some", "some", "some", "some",
|
||||||
|
"some", "some", "some", "some", "some", "some", "some",
|
||||||
|
"some", "some", "some", "some", "some", "some", "some",
|
||||||
|
"some", "some", "some", "some", "some", "some", "some",
|
||||||
|
"some", "some", "some", "some", "some", "some", "some",
|
||||||
|
"some", "some", "some", "some", "some", "some", "some",
|
||||||
|
"some", "some", "some", "some", "some", "some", "some",
|
||||||
|
"some", "some", "some", "some", "some", "some", "some",
|
||||||
|
"some", "some", "some", "some", "some", "some", "some",
|
||||||
|
"some", "some", "some", "some", "some", "some", "some",
|
||||||
|
"some", "some", "some", "some"]);
|
||||||
assert!(r.is_ok());
|
assert!(r.is_ok());
|
||||||
let m = r.unwrap();
|
let m = r.unwrap();
|
||||||
assert!(m.is_present("o"));
|
assert!(m.is_present("o"));
|
||||||
|
@ -76,10 +86,8 @@ fn lots_o_vals() {
|
||||||
#[test]
|
#[test]
|
||||||
fn opts_using_long_space() {
|
fn opts_using_long_space() {
|
||||||
let r = App::new("opts")
|
let r = App::new("opts")
|
||||||
.args(&[
|
.args(&[Arg::from_usage("--flag [flag] 'some flag'"),
|
||||||
Arg::from_usage("--flag [flag] 'some flag'"),
|
Arg::from_usage("--color [color] 'some other flag'")])
|
||||||
Arg::from_usage("--color [color] 'some other flag'")
|
|
||||||
])
|
|
||||||
.get_matches_from_safe(vec!["", "--flag", "some", "--color", "other"]);
|
.get_matches_from_safe(vec!["", "--flag", "some", "--color", "other"]);
|
||||||
assert!(r.is_ok());
|
assert!(r.is_ok());
|
||||||
let m = r.unwrap();
|
let m = r.unwrap();
|
||||||
|
@ -97,16 +105,15 @@ fn opts_with_empty_values() {
|
||||||
assert!(r.is_ok());
|
assert!(r.is_ok());
|
||||||
let m = r.unwrap();
|
let m = r.unwrap();
|
||||||
assert!(m.is_present("flag"));
|
assert!(m.is_present("flag"));
|
||||||
assert_eq!(m.values_of("flag").unwrap().collect::<Vec<_>>(), ["", "test"]);
|
assert_eq!(m.values_of("flag").unwrap().collect::<Vec<_>>(),
|
||||||
|
["", "test"]);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn opts_using_long_equals() {
|
fn opts_using_long_equals() {
|
||||||
let r = App::new("opts")
|
let r = App::new("opts")
|
||||||
.args(&[
|
.args(&[Arg::from_usage("--flag [flag] 'some flag'"),
|
||||||
Arg::from_usage("--flag [flag] 'some flag'"),
|
Arg::from_usage("--color [color] 'some other flag'")])
|
||||||
Arg::from_usage("--color [color] 'some other flag'")
|
|
||||||
])
|
|
||||||
.get_matches_from_safe(vec!["", "--flag=some", "--color=other"]);
|
.get_matches_from_safe(vec!["", "--flag=some", "--color=other"]);
|
||||||
assert!(r.is_ok());
|
assert!(r.is_ok());
|
||||||
let m = r.unwrap();
|
let m = r.unwrap();
|
||||||
|
@ -119,10 +126,8 @@ fn opts_using_long_equals() {
|
||||||
#[test]
|
#[test]
|
||||||
fn opts_using_mixed() {
|
fn opts_using_mixed() {
|
||||||
let r = App::new("opts")
|
let r = App::new("opts")
|
||||||
.args(&[
|
.args(&[Arg::from_usage("-f, --flag [flag] 'some flag'"),
|
||||||
Arg::from_usage("-f, --flag [flag] 'some flag'"),
|
Arg::from_usage("-c, --color [color] 'some other flag'")])
|
||||||
Arg::from_usage("-c, --color [color] 'some other flag'")
|
|
||||||
])
|
|
||||||
.get_matches_from_safe(vec!["", "-f", "some", "--color", "other"]);
|
.get_matches_from_safe(vec!["", "-f", "some", "--color", "other"]);
|
||||||
assert!(r.is_ok());
|
assert!(r.is_ok());
|
||||||
let m = r.unwrap();
|
let m = r.unwrap();
|
||||||
|
@ -135,10 +140,8 @@ fn opts_using_mixed() {
|
||||||
#[test]
|
#[test]
|
||||||
fn opts_using_mixed2() {
|
fn opts_using_mixed2() {
|
||||||
let r = App::new("opts")
|
let r = App::new("opts")
|
||||||
.args(&[
|
.args(&[Arg::from_usage("-f, --flag [flag] 'some flag'"),
|
||||||
Arg::from_usage("-f, --flag [flag] 'some flag'"),
|
Arg::from_usage("-c, --color [color] 'some other flag'")])
|
||||||
Arg::from_usage("-c, --color [color] 'some other flag'")
|
|
||||||
])
|
|
||||||
.get_matches_from_safe(vec!["", "--flag=some", "-c", "other"]);
|
.get_matches_from_safe(vec!["", "--flag=some", "-c", "other"]);
|
||||||
assert!(r.is_ok());
|
assert!(r.is_ok());
|
||||||
let m = r.unwrap();
|
let m = r.unwrap();
|
||||||
|
@ -151,8 +154,7 @@ fn opts_using_mixed2() {
|
||||||
#[test]
|
#[test]
|
||||||
fn default_values_user_value() {
|
fn default_values_user_value() {
|
||||||
let r = App::new("df")
|
let r = App::new("df")
|
||||||
.arg( Arg::from_usage("-o [opt] 'some opt'")
|
.arg(Arg::from_usage("-o [opt] 'some opt'").default_value("default"))
|
||||||
.default_value("default"))
|
|
||||||
.get_matches_from_safe(vec!["", "-o", "value"]);
|
.get_matches_from_safe(vec!["", "-o", "value"]);
|
||||||
assert!(r.is_ok());
|
assert!(r.is_ok());
|
||||||
let m = r.unwrap();
|
let m = r.unwrap();
|
||||||
|
@ -163,8 +165,8 @@ fn default_values_user_value() {
|
||||||
#[test]
|
#[test]
|
||||||
fn multiple_vals_pos_arg_equals() {
|
fn multiple_vals_pos_arg_equals() {
|
||||||
let r = App::new("mvae")
|
let r = App::new("mvae")
|
||||||
.arg( Arg::from_usage("-o [opt]... 'some opt'") )
|
.arg(Arg::from_usage("-o [opt]... 'some opt'"))
|
||||||
.arg( Arg::from_usage("[file] 'some file'") )
|
.arg(Arg::from_usage("[file] 'some file'"))
|
||||||
.get_matches_from_safe(vec!["", "-o=1", "some"]);
|
.get_matches_from_safe(vec!["", "-o=1", "some"]);
|
||||||
assert!(r.is_ok());
|
assert!(r.is_ok());
|
||||||
let m = r.unwrap();
|
let m = r.unwrap();
|
||||||
|
@ -177,8 +179,8 @@ fn multiple_vals_pos_arg_equals() {
|
||||||
#[test]
|
#[test]
|
||||||
fn multiple_vals_pos_arg_delim() {
|
fn multiple_vals_pos_arg_delim() {
|
||||||
let r = App::new("mvae")
|
let r = App::new("mvae")
|
||||||
.arg( Arg::from_usage("-o [opt]... 'some opt'") )
|
.arg(Arg::from_usage("-o [opt]... 'some opt'"))
|
||||||
.arg( Arg::from_usage("[file] 'some file'") )
|
.arg(Arg::from_usage("[file] 'some file'"))
|
||||||
.get_matches_from_safe(vec!["", "-o", "1,2", "some"]);
|
.get_matches_from_safe(vec!["", "-o", "1,2", "some"]);
|
||||||
assert!(r.is_ok());
|
assert!(r.is_ok());
|
||||||
let m = r.unwrap();
|
let m = r.unwrap();
|
||||||
|
@ -191,8 +193,8 @@ fn multiple_vals_pos_arg_delim() {
|
||||||
#[test]
|
#[test]
|
||||||
fn require_delims_no_delim() {
|
fn require_delims_no_delim() {
|
||||||
let r = App::new("mvae")
|
let r = App::new("mvae")
|
||||||
.arg( Arg::from_usage("-o [opt]... 'some opt'").require_delimiter(true) )
|
.arg(Arg::from_usage("-o [opt]... 'some opt'").require_delimiter(true))
|
||||||
.arg( Arg::from_usage("[file] 'some file'") )
|
.arg(Arg::from_usage("[file] 'some file'"))
|
||||||
.get_matches_from_safe(vec!["mvae", "-o", "1", "2", "some"]);
|
.get_matches_from_safe(vec!["mvae", "-o", "1", "2", "some"]);
|
||||||
assert!(r.is_err());
|
assert!(r.is_err());
|
||||||
let err = r.unwrap_err();
|
let err = r.unwrap_err();
|
||||||
|
@ -202,8 +204,8 @@ fn require_delims_no_delim() {
|
||||||
#[test]
|
#[test]
|
||||||
fn require_delims() {
|
fn require_delims() {
|
||||||
let r = App::new("mvae")
|
let r = App::new("mvae")
|
||||||
.arg( Arg::from_usage("-o [opt]... 'some opt'").require_delimiter(true) )
|
.arg(Arg::from_usage("-o [opt]... 'some opt'").require_delimiter(true))
|
||||||
.arg( Arg::from_usage("[file] 'some file'") )
|
.arg(Arg::from_usage("[file] 'some file'"))
|
||||||
.get_matches_from_safe(vec!["", "-o", "1,2", "some"]);
|
.get_matches_from_safe(vec!["", "-o", "1,2", "some"]);
|
||||||
assert!(r.is_ok());
|
assert!(r.is_ok());
|
||||||
let m = r.unwrap();
|
let m = r.unwrap();
|
||||||
|
@ -216,7 +218,7 @@ fn require_delims() {
|
||||||
#[test]
|
#[test]
|
||||||
fn leading_hyphen_pass() {
|
fn leading_hyphen_pass() {
|
||||||
let r = App::new("mvae")
|
let r = App::new("mvae")
|
||||||
.arg( Arg::from_usage("-o [opt]... 'some opt'").allow_hyphen_values(true))
|
.arg(Arg::from_usage("-o [opt]... 'some opt'").allow_hyphen_values(true))
|
||||||
.get_matches_from_safe(vec!["", "-o", "-2", "3"]);
|
.get_matches_from_safe(vec!["", "-o", "-2", "3"]);
|
||||||
assert!(r.is_ok());
|
assert!(r.is_ok());
|
||||||
let m = r.unwrap();
|
let m = r.unwrap();
|
||||||
|
@ -227,7 +229,7 @@ fn leading_hyphen_pass() {
|
||||||
#[test]
|
#[test]
|
||||||
fn leading_hyphen_fail() {
|
fn leading_hyphen_fail() {
|
||||||
let r = App::new("mvae")
|
let r = App::new("mvae")
|
||||||
.arg( Arg::from_usage("-o [opt] 'some opt'"))
|
.arg(Arg::from_usage("-o [opt] 'some opt'"))
|
||||||
.get_matches_from_safe(vec!["", "-o", "-2"]);
|
.get_matches_from_safe(vec!["", "-o", "-2"]);
|
||||||
assert!(r.is_err());
|
assert!(r.is_err());
|
||||||
let m = r.unwrap_err();
|
let m = r.unwrap_err();
|
||||||
|
@ -237,7 +239,7 @@ fn leading_hyphen_fail() {
|
||||||
#[test]
|
#[test]
|
||||||
fn leading_hyphen_with_flag_after() {
|
fn leading_hyphen_with_flag_after() {
|
||||||
let r = App::new("mvae")
|
let r = App::new("mvae")
|
||||||
.arg( Arg::from_usage("-o [opt]... 'some opt'").allow_hyphen_values(true))
|
.arg(Arg::from_usage("-o [opt]... 'some opt'").allow_hyphen_values(true))
|
||||||
.arg_from_usage("-f 'some flag'")
|
.arg_from_usage("-f 'some flag'")
|
||||||
.get_matches_from_safe(vec!["", "-o", "-2", "-f"]);
|
.get_matches_from_safe(vec!["", "-o", "-2", "-f"]);
|
||||||
assert!(r.is_ok());
|
assert!(r.is_ok());
|
||||||
|
@ -250,7 +252,7 @@ fn leading_hyphen_with_flag_after() {
|
||||||
#[test]
|
#[test]
|
||||||
fn leading_hyphen_with_flag_before() {
|
fn leading_hyphen_with_flag_before() {
|
||||||
let r = App::new("mvae")
|
let r = App::new("mvae")
|
||||||
.arg( Arg::from_usage("-o [opt]... 'some opt'").allow_hyphen_values(true))
|
.arg(Arg::from_usage("-o [opt]... 'some opt'").allow_hyphen_values(true))
|
||||||
.arg_from_usage("-f 'some flag'")
|
.arg_from_usage("-f 'some flag'")
|
||||||
.get_matches_from_safe(vec!["", "-f", "-o", "-2"]);
|
.get_matches_from_safe(vec!["", "-f", "-o", "-2"]);
|
||||||
assert!(r.is_ok());
|
assert!(r.is_ok());
|
||||||
|
@ -263,7 +265,7 @@ fn leading_hyphen_with_flag_before() {
|
||||||
#[test]
|
#[test]
|
||||||
fn leading_hyphen_with_only_pos_follows() {
|
fn leading_hyphen_with_only_pos_follows() {
|
||||||
let r = App::new("mvae")
|
let r = App::new("mvae")
|
||||||
.arg( Arg::from_usage("-o [opt]... 'some opt'").allow_hyphen_values(true))
|
.arg(Arg::from_usage("-o [opt]... 'some opt'").allow_hyphen_values(true))
|
||||||
.arg_from_usage("[arg] 'some arg'")
|
.arg_from_usage("[arg] 'some arg'")
|
||||||
.get_matches_from_safe(vec!["", "-o", "-2", "--", "val"]);
|
.get_matches_from_safe(vec!["", "-o", "-2", "--", "val"]);
|
||||||
assert!(r.is_ok());
|
assert!(r.is_ok());
|
||||||
|
@ -276,12 +278,27 @@ fn leading_hyphen_with_only_pos_follows() {
|
||||||
#[test]
|
#[test]
|
||||||
#[cfg(feature="suggestions")]
|
#[cfg(feature="suggestions")]
|
||||||
fn did_you_mean() {
|
fn did_you_mean() {
|
||||||
test::check_err_output(test::complex_app(), "clap-test --optio=foo",
|
test::check_err_output(test::complex_app(),
|
||||||
"error: Found argument '--optio' which wasn't expected, or isn't valid in this context
|
"clap-test --optio=foo",
|
||||||
|
"error: Found argument '--optio' which wasn't expected, or isn't valid in this context
|
||||||
\tDid you mean --option?
|
\tDid you mean --option?
|
||||||
|
|
||||||
USAGE:
|
USAGE:
|
||||||
clap-test --option <opt>...
|
clap-test --option <opt>...
|
||||||
|
|
||||||
For more information try --help", true);
|
For more information try --help",
|
||||||
|
true);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn issue_665() {
|
||||||
|
let res = App::new("tester")
|
||||||
|
.arg_from_usage("-v, --reroll-count=[N] 'Mark the patch series as PATCH vN'")
|
||||||
|
.arg(Arg::from_usage(
|
||||||
|
"--subject-prefix [Subject-Prefix] 'Use [Subject-Prefix] instead of the standard [PATCH] prefix'")
|
||||||
|
.empty_values(false))
|
||||||
|
.get_matches_from_safe(vec!["test", "--subject-prefix", "-v", "2"]);
|
||||||
|
|
||||||
|
assert!(res.is_err());
|
||||||
|
assert_eq!(res.unwrap_err().kind, ErrorKind::EmptyValue);
|
||||||
}
|
}
|
Loading…
Reference in a new issue