From d1fcce0cd3b878686360d9020806981d45f8e95b Mon Sep 17 00:00:00 2001 From: rabisg0 <61725668+rabisg0@users.noreply.github.com> Date: Tue, 10 Mar 2020 22:29:50 +0530 Subject: [PATCH] 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. --- crates/nu-parser/src/hir.rs | 6 +----- crates/nu-parser/src/hir/named.rs | 10 ++++++++++ crates/nu-parser/src/parse_command.rs | 2 +- 3 files changed, 12 insertions(+), 6 deletions(-) diff --git a/crates/nu-parser/src/hir.rs b/crates/nu-parser/src/hir.rs index 6afcc9dfea..bb70adc3bb 100644 --- a/crates/nu-parser/src/hir.rs +++ b/crates/nu-parser/src/hir.rs @@ -74,11 +74,7 @@ impl Call { pub fn switch_preset(&self, switch: &str) -> bool { self.named .as_ref() - .and_then(|n| n.get(switch)) - .map(|t| match t { - NamedValue::PresentSwitch(_) => true, - _ => false, - }) + .map(|n| n.switch_present(switch)) .unwrap_or(false) } } diff --git a/crates/nu-parser/src/hir/named.rs b/crates/nu-parser/src/hir/named.rs index 49ae3125c0..81b2c31fef 100644 --- a/crates/nu-parser/src/hir/named.rs +++ b/crates/nu-parser/src/hir/named.rs @@ -82,6 +82,16 @@ impl NamedArguments { pub fn insert_mandatory(&mut self, name: impl Into, expr: SpannedExpression) { 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 { diff --git a/crates/nu-parser/src/parse_command.rs b/crates/nu-parser/src/parse_command.rs index 198372abb1..da29b2af4d 100644 --- a/crates/nu-parser/src/parse_command.rs +++ b/crates/nu-parser/src/parse_command.rs @@ -114,7 +114,7 @@ pub fn parse_command_tail( positional = positionals; } Err(reason) => { - if found_error.is_none() && !tail.source().contains("help") { + if found_error.is_none() && !named.switch_present("help") { found_error = Some(reason); } }