mirror of
https://github.com/clap-rs/clap
synced 2025-03-04 23:37:32 +00:00
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:
parent
9435b2a589
commit
77ed46841f
1 changed files with 9 additions and 2 deletions
|
@ -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()));
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue