From fe4ad5f77e3f4bb259f3a40db35ff7a61e519df8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s=20N=2E=20Robalino?= Date: Wed, 22 Jan 2020 19:36:48 -0500 Subject: [PATCH] Color named type help especial case. (#1263) Refactored out help named type as switch. --- crates/nu-parser/src/parse_command.rs | 86 ++++++++++++++------------- crates/nu-protocol/src/signature.rs | 11 +--- src/commands/help.rs | 2 +- src/data/command.rs | 1 - 4 files changed, 48 insertions(+), 52 deletions(-) diff --git a/crates/nu-parser/src/parse_command.rs b/crates/nu-parser/src/parse_command.rs index 76159593ac..30e425872d 100644 --- a/crates/nu-parser/src/parse_command.rs +++ b/crates/nu-parser/src/parse_command.rs @@ -45,17 +45,6 @@ pub fn parse_command_tail( } } } - NamedType::Help => { - let switch = extract_switch(name, tail); - - match switch { - None => named.insert_switch(name, None), - Some((_, flag)) => { - named.insert_switch(name, Some(*flag)); - return Ok(Some((None, Some(named)))); - } - } - } NamedType::Mandatory(syntax_type) => { match extract_mandatory(config, name, tail, command_span) { Err(err) => { @@ -115,35 +104,13 @@ pub fn parse_command_tail( let mut positional = vec![]; - for arg in &config.positional { - trace!(target: "nu::parse::trace_remaining", "Processing positional {:?}", arg); - - tail.move_to(0); - - let result = expand_spaced_expr(arg.0.syntax_type(), tail); - - match result { - Err(_) => match &arg.0 { - PositionalType::Mandatory(..) => { - if found_error.is_none() { - found_error = Some(ParseError::argument_error( - config.name.clone().spanned(command_span), - ArgumentError::MissingMandatoryPositional(arg.0.name().to_string()), - )); - } - - break; - } - - PositionalType::Optional(..) => { - if tail.expand_syntax(MaybeWhitespaceEof).is_ok() { - break; - } - } - }, - Ok(result) => { - rest_signature.shift_positional(); - positional.push(result); + match continue_parsing_positionals(&config, tail, &mut rest_signature, command_span) { + Ok(positionals) => { + positional = positionals; + } + Err(reason) => { + if found_error.is_none() && !tail.source().contains("help") { + found_error = Some(reason); } } } @@ -213,6 +180,45 @@ pub fn parse_command_tail( Ok(Some((positional, named))) } +pub fn continue_parsing_positionals( + config: &Signature, + tail: &mut TokensIterator, + rest_signature: &mut Signature, + command_span: Span, +) -> Result, ParseError> { + let mut positional = vec![]; + + for arg in &config.positional { + trace!(target: "nu::parse::trace_remaining", "Processing positional {:?}", arg); + + tail.move_to(0); + + let result = expand_spaced_expr(arg.0.syntax_type(), tail); + + match result { + Err(_) => match &arg.0 { + PositionalType::Mandatory(..) => { + return Err(ParseError::argument_error( + config.name.clone().spanned(command_span), + ArgumentError::MissingMandatoryPositional(arg.0.name().to_string()), + )) + } + PositionalType::Optional(..) => { + if tail.expand_syntax(MaybeWhitespaceEof).is_ok() { + break; + } + } + }, + Ok(result) => { + rest_signature.shift_positional(); + positional.push(result); + } + } + } + + Ok(positional) +} + fn eat_any_whitespace(tail: &mut TokensIterator) { loop { match tail.expand_infallible(MaybeSpaceShape) { diff --git a/crates/nu-protocol/src/signature.rs b/crates/nu-protocol/src/signature.rs index b92510d4e3..5697632879 100644 --- a/crates/nu-protocol/src/signature.rs +++ b/crates/nu-protocol/src/signature.rs @@ -13,7 +13,6 @@ pub enum NamedType { Mandatory(SyntaxShape), /// An optional flag, with associated argument. eg) `foo --optional abc` Optional(SyntaxShape), - Help, } /// The type of positional arguments @@ -165,7 +164,7 @@ impl Signature { usage: String::new(), positional: vec![], rest_positional: None, - named: indexmap::indexmap! {"help".into() => (NamedType::Help, "Display this help message".into())}, + named: indexmap::indexmap! {"help".into() => (NamedType::Switch, "Display this help message".into())}, is_filter: false, yields: None, input: None, @@ -243,14 +242,6 @@ impl Signature { pub fn switch(mut self, name: impl Into, desc: impl Into) -> Signature { self.named .insert(name.into(), (NamedType::Switch, desc.into())); - - self - } - - /// Remove the default help switch - pub fn remove_help(mut self) -> Signature { - self.named.remove("help"); - self } diff --git a/src/commands/help.rs b/src/commands/help.rs index 9299b3a6aa..fa4c357887 100644 --- a/src/commands/help.rs +++ b/src/commands/help.rs @@ -159,7 +159,7 @@ pub(crate) fn get_help( long_desc.push_str("\nflags:\n"); for (flag, ty) in signature.named { match ty.0 { - NamedType::Switch | NamedType::Help => { + NamedType::Switch => { long_desc.push_str(&format!( " --{}{} {}\n", flag, diff --git a/src/data/command.rs b/src/data/command.rs index bec6074cf7..91c52bc0e5 100644 --- a/src/data/command.rs +++ b/src/data/command.rs @@ -63,7 +63,6 @@ fn signature_dict(signature: Signature, tag: impl Into) -> Value { NamedType::Mandatory(_) => sig.push_value(for_spec(name, "flag", true, &tag)), NamedType::Optional(_) => sig.push_value(for_spec(name, "flag", false, &tag)), NamedType::Switch => sig.push_value(for_spec(name, "switch", false, &tag)), - NamedType::Help => sig.push_value(for_spec("help", "switch", false, &tag)), } }