From 5dd346094ef0b6bbd3c84814b573e29d65220ef1 Mon Sep 17 00:00:00 2001 From: utam0k Date: Thu, 4 Jun 2020 16:09:43 +0900 Subject: [PATCH] Cut out a function to generate a pharase in the Flags section. (#1930) --- crates/nu-cli/src/commands/help.rs | 140 +++++++++++++++-------------- 1 file changed, 73 insertions(+), 67 deletions(-) diff --git a/crates/nu-cli/src/commands/help.rs b/crates/nu-cli/src/commands/help.rs index b17333b849..dcd5711398 100644 --- a/crates/nu-cli/src/commands/help.rs +++ b/crates/nu-cli/src/commands/help.rs @@ -182,8 +182,8 @@ pub fn get_help(cmd: &dyn WholeStreamCommand, registry: &CommandRegistry) -> Str if !signature.positional.is_empty() || signature.rest_positional.is_some() { long_desc.push_str("\nParameters:\n"); - for positional in signature.positional { - match positional.0 { + for positional in &signature.positional { + match &positional.0 { PositionalType::Mandatory(name, _m) => { long_desc.push_str(&format!(" <{}> {}\n", name, positional.1)); } @@ -193,75 +193,12 @@ pub fn get_help(cmd: &dyn WholeStreamCommand, registry: &CommandRegistry) -> Str } } - if let Some(rest_positional) = signature.rest_positional { + if let Some(rest_positional) = &signature.rest_positional { long_desc.push_str(&format!(" ...args: {}\n", rest_positional.1)); } } if !signature.named.is_empty() { - long_desc.push_str("\nFlags:\n"); - for (flag, ty) in signature.named { - let msg = match ty.0 { - NamedType::Switch(s) => { - if let Some(c) = s { - format!( - " -{}, --{}{} {}\n", - c, - flag, - if !ty.1.is_empty() { ":" } else { "" }, - ty.1 - ) - } else { - format!( - " --{}{} {}\n", - flag, - if !ty.1.is_empty() { ":" } else { "" }, - ty.1 - ) - } - } - NamedType::Mandatory(s, m) => { - if let Some(c) = s { - format!( - " -{}, --{} <{}> (required parameter){} {}\n", - c, - flag, - m.display(), - if !ty.1.is_empty() { ":" } else { "" }, - ty.1 - ) - } else { - format!( - " --{} <{}> (required parameter){} {}\n", - flag, - m.display(), - if !ty.1.is_empty() { ":" } else { "" }, - ty.1 - ) - } - } - NamedType::Optional(s, o) => { - if let Some(c) = s { - format!( - " -{}, --{} <{}>{} {}\n", - c, - flag, - o.display(), - if !ty.1.is_empty() { ":" } else { "" }, - ty.1 - ) - } else { - format!( - " --{} <{}>{} {}\n", - flag, - o.display(), - if !ty.1.is_empty() { ":" } else { "" }, - ty.1 - ) - } - } - }; - long_desc.push_str(&msg); - } + long_desc.push_str(&get_flags_section(&signature)) } let palette = crate::shell::palette::DefaultPalette {}; @@ -283,6 +220,75 @@ pub fn get_help(cmd: &dyn WholeStreamCommand, registry: &CommandRegistry) -> Str long_desc } +fn get_flags_section(signature: &Signature) -> String { + let mut long_desc = String::new(); + long_desc.push_str("\nFlags:\n"); + for (flag, ty) in &signature.named { + let msg = match ty.0 { + NamedType::Switch(s) => { + if let Some(c) = s { + format!( + " -{}, --{}{} {}\n", + c, + flag, + if !ty.1.is_empty() { ":" } else { "" }, + ty.1 + ) + } else { + format!( + " --{}{} {}\n", + flag, + if !ty.1.is_empty() { ":" } else { "" }, + ty.1 + ) + } + } + NamedType::Mandatory(s, m) => { + if let Some(c) = s { + format!( + " -{}, --{} <{}> (required parameter){} {}\n", + c, + flag, + m.display(), + if !ty.1.is_empty() { ":" } else { "" }, + ty.1 + ) + } else { + format!( + " --{} <{}> (required parameter){} {}\n", + flag, + m.display(), + if !ty.1.is_empty() { ":" } else { "" }, + ty.1 + ) + } + } + NamedType::Optional(s, o) => { + if let Some(c) = s { + format!( + " -{}, --{} <{}>{} {}\n", + c, + flag, + o.display(), + if !ty.1.is_empty() { ":" } else { "" }, + ty.1 + ) + } else { + format!( + " --{} <{}>{} {}\n", + flag, + o.display(), + if !ty.1.is_empty() { ":" } else { "" }, + ty.1 + ) + } + } + }; + long_desc.push_str(&msg); + } + long_desc +} + #[cfg(test)] mod tests { use super::Help;