Skip to content

Commit

Permalink
fix: fixes a bug where using AppSettings::AllowHyphenValues would all…
Browse files Browse the repository at this point in the history
…ow 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
  • Loading branch information
kbknapp committed Oct 24, 2017
1 parent 9435b2a commit 77ed468
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions src/app/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
"",
Expand Down Expand Up @@ -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()));
}
}

Expand Down

0 comments on commit 77ed468

Please sign in to comment.