Fixes #1427: Prints help message with -h switch (#1454)

For some commands like `which` -h flag would trigger an error asking for
missing required parameters instead of printing the help message as it
does with --help. This commit adds a check in the command parser to
avoid that.
This commit is contained in:
rabisg0 2020-03-10 22:29:50 +05:30 committed by GitHub
parent a2443fbe02
commit d1fcce0cd3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 12 additions and 6 deletions

View file

@ -74,11 +74,7 @@ impl Call {
pub fn switch_preset(&self, switch: &str) -> bool { pub fn switch_preset(&self, switch: &str) -> bool {
self.named self.named
.as_ref() .as_ref()
.and_then(|n| n.get(switch)) .map(|n| n.switch_present(switch))
.map(|t| match t {
NamedValue::PresentSwitch(_) => true,
_ => false,
})
.unwrap_or(false) .unwrap_or(false)
} }
} }

View file

@ -82,6 +82,16 @@ impl NamedArguments {
pub fn insert_mandatory(&mut self, name: impl Into<String>, expr: SpannedExpression) { pub fn insert_mandatory(&mut self, name: impl Into<String>, expr: SpannedExpression) {
self.named.insert(name.into(), NamedValue::Value(expr)); self.named.insert(name.into(), NamedValue::Value(expr));
} }
pub fn switch_present(&self, switch: &str) -> bool {
self.named
.get(switch)
.map(|t| match t {
NamedValue::PresentSwitch(_) => true,
_ => false,
})
.unwrap_or(false)
}
} }
impl PrettyDebugWithSource for NamedArguments { impl PrettyDebugWithSource for NamedArguments {

View file

@ -114,7 +114,7 @@ pub fn parse_command_tail(
positional = positionals; positional = positionals;
} }
Err(reason) => { Err(reason) => {
if found_error.is_none() && !tail.source().contains("help") { if found_error.is_none() && !named.switch_present("help") {
found_error = Some(reason); found_error = Some(reason);
} }
} }