mirror of
https://github.com/clap-rs/clap
synced 2025-01-07 10:18:48 +00:00
commit
7eb949ba38
2 changed files with 106 additions and 81 deletions
|
@ -234,7 +234,8 @@ impl<'a, 'b> Parser<'a, 'b>
|
||||||
|
|
||||||
pub fn add_subcommand(&mut self, mut subcmd: App<'a, 'b>) {
|
pub fn add_subcommand(&mut self, mut subcmd: App<'a, 'b>) {
|
||||||
debugln!("Parser::add_subcommand;");
|
debugln!("Parser::add_subcommand;");
|
||||||
debugln!("Parser::add_subcommand: Term width...{:?}", self.meta.term_w);
|
debugln!("Parser::add_subcommand: Term width...{:?}",
|
||||||
|
self.meta.term_w);
|
||||||
subcmd.p.meta.term_w = self.meta.term_w;
|
subcmd.p.meta.term_w = self.meta.term_w;
|
||||||
debug!("Parser::add_subcommand: Is help...");
|
debug!("Parser::add_subcommand: Is help...");
|
||||||
if subcmd.p.meta.name == "help" {
|
if subcmd.p.meta.name == "help" {
|
||||||
|
@ -256,8 +257,10 @@ impl<'a, 'b> Parser<'a, 'b>
|
||||||
let vsc = self.settings.is_set(AppSettings::VersionlessSubcommands);
|
let vsc = self.settings.is_set(AppSettings::VersionlessSubcommands);
|
||||||
let gv = self.settings.is_set(AppSettings::GlobalVersion);
|
let gv = self.settings.is_set(AppSettings::GlobalVersion);
|
||||||
|
|
||||||
debugln!("Parser::propogate_settings:iter: VersionlessSubcommands set...{:?}", vsc);
|
debugln!("Parser::propogate_settings:iter: VersionlessSubcommands set...{:?}",
|
||||||
debugln!("Parser::propogate_settings:iter: GlobalVersion set...{:?}", gv);
|
vsc);
|
||||||
|
debugln!("Parser::propogate_settings:iter: GlobalVersion set...{:?}",
|
||||||
|
gv);
|
||||||
|
|
||||||
if vsc {
|
if vsc {
|
||||||
sc.p.settings.set(AppSettings::DisableVersion);
|
sc.p.settings.set(AppSettings::DisableVersion);
|
||||||
|
@ -1540,7 +1543,9 @@ impl<'a, 'b> Parser<'a, 'b>
|
||||||
}
|
}
|
||||||
if let Some(vtor) = arg.validator_os() {
|
if let Some(vtor) = arg.validator_os() {
|
||||||
if let Err(e) = vtor(val) {
|
if let Err(e) = vtor(val) {
|
||||||
return Err(Error::value_validation(Some(arg), (*e).to_string_lossy().to_string(), self.color()));
|
return Err(Error::value_validation(Some(arg),
|
||||||
|
(*e).to_string_lossy().to_string(),
|
||||||
|
self.color()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if matcher.needs_more_vals(arg) {
|
if matcher.needs_more_vals(arg) {
|
||||||
|
@ -1703,6 +1708,10 @@ impl<'a, 'b> Parser<'a, 'b>
|
||||||
self.color()));
|
self.color()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
// Issue 665 (https://github.com/kbknapp/clap-rs/issues/665)
|
||||||
|
if a.takes_value() && !a.is_set(ArgSettings::EmptyValues) && ma.vals.is_empty() {
|
||||||
|
return Err(Error::empty_value(a, &*self.create_current_usage(matcher), self.color()));
|
||||||
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1739,8 +1748,7 @@ impl<'a, 'b> Parser<'a, 'b>
|
||||||
Some(matcher))
|
Some(matcher))
|
||||||
.iter()
|
.iter()
|
||||||
.fold(String::new(), |acc, s| {
|
.fold(String::new(), |acc, s| {
|
||||||
acc +
|
acc + &format!("\n {}", c.error(s))[..]
|
||||||
&format!("\n {}", c.error(s))[..]
|
|
||||||
}),
|
}),
|
||||||
&*self.create_current_usage(matcher),
|
&*self.create_current_usage(matcher),
|
||||||
self.color()))
|
self.color()))
|
||||||
|
|
131
tests/opts.rs
131
tests/opts.rs
|
@ -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();
|
||||||
|
@ -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(),
|
||||||
|
"clap-test --optio=foo",
|
||||||
"error: Found argument '--optio' which wasn't expected, or isn't valid in this context
|
"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