Skip to content

Commit 77ed468

Browse files
committed
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
1 parent 9435b2a commit 77ed468

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

src/app/parser.rs

+9-2
Original file line numberDiff line numberDiff line change
@@ -1015,8 +1015,8 @@ impl<'a, 'b> Parser<'a, 'b>
10151015
name: sc_name,
10161016
matches: sc_m.into(),
10171017
});
1018-
} else if !(self.is_set(AS::AllowLeadingHyphen) ||
1019-
self.is_set(AS::AllowNegativeNumbers)) &&
1018+
} else if !((self.is_set(AS::AllowLeadingHyphen) ||
1019+
self.is_set(AS::AllowNegativeNumbers)) && arg_os.starts_with(b"-")) &&
10201020
!self.is_set(AS::InferSubcommands) {
10211021
return Err(Error::unknown_argument(&*arg_os.to_string_lossy(),
10221022
"",
@@ -1047,6 +1047,13 @@ impl<'a, 'b> Parser<'a, 'b>
10471047
.unwrap_or(&self.meta.name),
10481048
self.color()));
10491049
}
1050+
} else {
1051+
return Err(Error::unknown_argument(&*arg_os.to_string_lossy(),
1052+
"",
1053+
&*usage::create_error_usage(self,
1054+
matcher,
1055+
None),
1056+
self.color()));
10501057
}
10511058
}
10521059

0 commit comments

Comments
 (0)