fix: fixes a bug where using AppSettings::AllowHyphenValues would allow invalid arguments even when there is no way for them to be valid

Prior to this commit, using `AppSettings::AllowHyphenValues` would allow
ANY argument to pass, even if there was no way it could be valid.

Imagine a CLI with only a single flag (i.e. *no value*) `--flag`, but this setting
is set. The following was valid:

```
$ prog hello
```

This commit fixes that by creating an UnknownArgument error unless the
unknown argument/value in question could legally be parsed as a value
to a valid argument.

Closes #1066
This commit is contained in:
Kevin K 2017-10-24 15:18:56 -07:00
parent 9435b2a589
commit 77ed46841f
No known key found for this signature in database
GPG key ID: 17218E4B3692F01A

View file

@ -1015,8 +1015,8 @@ impl<'a, 'b> Parser<'a, 'b>
name: sc_name,
matches: sc_m.into(),
});
} else if !(self.is_set(AS::AllowLeadingHyphen) ||
self.is_set(AS::AllowNegativeNumbers)) &&
} else if !((self.is_set(AS::AllowLeadingHyphen) ||
self.is_set(AS::AllowNegativeNumbers)) && arg_os.starts_with(b"-")) &&
!self.is_set(AS::InferSubcommands) {
return Err(Error::unknown_argument(&*arg_os.to_string_lossy(),
"",
@ -1047,6 +1047,13 @@ impl<'a, 'b> Parser<'a, 'b>
.unwrap_or(&self.meta.name),
self.color()));
}
} else {
return Err(Error::unknown_argument(&*arg_os.to_string_lossy(),
"",
&*usage::create_error_usage(self,
matcher,
None),
self.color()));
}
}