Skip to content

Commit

Permalink
fix(Options): fixes a critical bug where options weren't forced to ha…
Browse files Browse the repository at this point in the history
…ve a value

For instance imagine --opt <val> and --opt2 <val>

Running:

```
$ prog --opt --opt2 val
```

Would pass. This has been fixed.

Closes #665
  • Loading branch information
kbknapp committed Dec 30, 2016
1 parent 03f7233 commit 5a5f2b1
Showing 1 changed file with 17 additions and 9 deletions.
26 changes: 17 additions & 9 deletions src/app/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,8 @@ impl<'a, 'b> Parser<'a, 'b>

pub fn add_subcommand(&mut self, mut subcmd: App<'a, 'b>) {
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;
debug!("Parser::add_subcommand: Is help...");
if subcmd.p.meta.name == "help" {
Expand All @@ -256,8 +257,10 @@ impl<'a, 'b> Parser<'a, 'b>
let vsc = self.settings.is_set(AppSettings::VersionlessSubcommands);
let gv = self.settings.is_set(AppSettings::GlobalVersion);

debugln!("Parser::propogate_settings:iter: VersionlessSubcommands set...{:?}", vsc);
debugln!("Parser::propogate_settings:iter: GlobalVersion set...{:?}", gv);
debugln!("Parser::propogate_settings:iter: VersionlessSubcommands set...{:?}",
vsc);
debugln!("Parser::propogate_settings:iter: GlobalVersion set...{:?}",
gv);

if vsc {
sc.p.settings.set(AppSettings::DisableVersion);
Expand Down Expand Up @@ -438,7 +441,7 @@ impl<'a, 'b> Parser<'a, 'b>
if grp.required {
// if it's part of a required group we don't want to count it
continue 'outer;
}
}
}
}
}
Expand Down Expand Up @@ -1540,7 +1543,9 @@ impl<'a, 'b> Parser<'a, 'b>
}
if let Some(vtor) = arg.validator_os() {
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) {
Expand Down Expand Up @@ -1703,6 +1708,10 @@ impl<'a, 'b> Parser<'a, 'b>
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(())
}

Expand Down Expand Up @@ -1731,16 +1740,15 @@ impl<'a, 'b> Parser<'a, 'b>
let c = Colorizer {
use_stderr: true,
when: self.color(),
};
let mut reqs = self.required.iter().map(|&r| &*r).collect::<Vec<_>>();
};
let mut reqs = self.required.iter().map(|&r| &*r).collect::<Vec<_>>();
reqs.retain(|n| !matcher.contains(n));
reqs.dedup();
Err(Error::missing_required_argument(&*self.get_required_from(&self.required[..],
Some(matcher))
.iter()
.fold(String::new(), |acc, s| {
acc +
&format!("\n {}", c.error(s))[..]
acc + &format!("\n {}", c.error(s))[..]
}),
&*self.create_current_usage(matcher),
self.color()))
Expand Down

0 comments on commit 5a5f2b1

Please sign in to comment.