Auto merge of #547 - kbknapp:issue-546, r=kbknapp

Issue 546
This commit is contained in:
Homu 2016-06-30 02:52:46 +09:00
commit dc6740a5ff
5 changed files with 55 additions and 5 deletions

View file

@ -1,3 +1,15 @@
<a name="v2.7.1"></a>
### v2.7.1 (2016-06-29)
#### Bug Fixes
* **Options:**
* options with multiple values and using delimiters no longer parse additional values after a trailing space ([cdc500bd](https://github.com/kbknapp/clap-rs/commit/cdc500bdde6abe238c36ade406ddafc2bafff583))
* using options with multiple values and with an = no longer parse args after the trailing space as values ([290f61d0](https://github.com/kbknapp/clap-rs/commit/290f61d07177413cf082ada55526d83405f6d011))
<a name="v2.7.0"></a>
## v2.7.0 (2016-06-28)
@ -1639,6 +1651,3 @@
#### Features
* **arg** allow lifetimes other than 'static in arguments ([9e8c1fb9](https://github.com/kbknapp/clap-rs/commit/9e8c1fb9406f8448873ca58bab07fe905f1551e5))

View file

@ -1,7 +1,7 @@
[package]
name = "clap"
version = "2.7.0"
version = "2.7.1"
authors = ["Kevin K. <kbknapp@gmail.com>"]
exclude = ["examples/*", "clap-test/*", "tests/*", "benches/*", "*.png", "clap-perf/*", "*.dot"]
description = "A simple to use, efficient, and full featured Command Line Argument Parser"

View file

@ -38,6 +38,12 @@ Created by [gh-md-toc](https://github.com/ekalinin/github-markdown-toc)
## What's New
Here's the highlights for v2.7.1
* **Options:**
* options using multiple values and delimiters no longer parse additional values after a trailing space (i.e. `prog -o 1,2 file.txt` parses as `1,2` for `-o` and `file.txt` for a positional arg)
* using options using multiple values and with an `=` no longer parse args after the trailing space as values (i.e. `prog -o=1 file.txt` parses as `1` for `-o` and `file.txt` for a positional arg)
Here's the highlights for v2.7.0
* **Usage Strings:** `[FLAGS]` and `[ARGS]` are no longer blindly added to usage strings, instead only when applicable

View file

@ -1211,15 +1211,18 @@ impl<'a, 'b> Parser<'a, 'b>
-> ClapResult<Option<&'a str>> {
debugln!("fn=parse_opt;");
validate_multiples!(self, opt, matcher);
let mut has_eq = false;
debug!("Checking for val...");
if let Some(fv) = val {
has_eq = fv.starts_with(&[b'=']);
let v = fv.trim_left_matches(b'=');
if !opt.is_set(ArgSettings::EmptyValues) && v.len_() == 0 {
sdebugln!("Found Empty - Error");
return Err(Error::empty_value(opt, &*self.create_current_usage(matcher), self.color()));
}
sdebugln!("Found - {:?}, len: {}", v, v.len_());
debugln!("{:?} contains '='...{:?}", fv, fv.starts_with(&[b'=']));
try!(self.add_val_to_arg(opt, v, matcher));
} else {
sdebugln!("None");
@ -1229,7 +1232,7 @@ impl<'a, 'b> Parser<'a, 'b>
// Increment or create the group "args"
self.groups_for_arg(opt.name).and_then(|vec| Some(matcher.inc_occurrences_of(&*vec)));
if val.is_none() || (opt.is_set(ArgSettings::Multiple) && matcher.needs_more_vals(opt)) {
if val.is_none() || !has_eq && (opt.is_set(ArgSettings::Multiple) && matcher.needs_more_vals(opt)) {
return Ok(Some(opt.name));
}
Ok(None)
@ -1252,6 +1255,10 @@ impl<'a, 'b> Parser<'a, 'b>
for v in val.split(delim as u32 as u8) {
ret = try!(self.add_single_val_to_arg(arg, v, matcher));
}
// If there was a delimiter used, we're not looking for more values
if val.contains_byte(delim as u32 as u8) {
ret = None;
}
}
} else {
ret = try!(self.add_single_val_to_arg(arg, val, matcher));

View file

@ -172,6 +172,34 @@ fn default_values_user_value() {
assert_eq!(m.value_of("o").unwrap(), "value");
}
#[test]
fn multiple_vals_pos_arg_equals() {
let r = App::new("mvae")
.arg( Arg::from_usage("-o [opt]... 'some opt'") )
.arg( Arg::from_usage("[file] 'some file'") )
.get_matches_from_safe(vec!["", "-o=1", "some"]);
assert!(r.is_ok());
let m = r.unwrap();
assert!(m.is_present("o"));
assert_eq!(m.value_of("o").unwrap(), "1");
assert!(m.is_present("file"));
assert_eq!(m.value_of("file").unwrap(), "some");
}
#[test]
fn multiple_vals_pos_arg_delim() {
let r = App::new("mvae")
.arg( Arg::from_usage("-o [opt]... 'some opt'") )
.arg( Arg::from_usage("[file] 'some file'") )
.get_matches_from_safe(vec!["", "-o", "1,2", "some"]);
assert!(r.is_ok());
let m = r.unwrap();
assert!(m.is_present("o"));
assert_eq!(m.values_of("o").unwrap().collect::<Vec<_>>(), &["1", "2"]);
assert!(m.is_present("file"));
assert_eq!(m.value_of("file").unwrap(), "some");
}
#[test]
fn did_you_mean() {
test::check_err_output(test::complex_app(), "clap-test --optio=foo",