From ad85ef4151563d289922bdcec9fe51a1774c95da Mon Sep 17 00:00:00 2001 From: Ed Page Date: Wed, 21 Feb 2024 21:19:45 -0600 Subject: [PATCH] refactor(cli): Shrink main I'm going to add more `config_configure`s and this will make it more obvious that these are done in early returns. --- src/bin/cargo/cli.rs | 202 ++++++++++++++++++++++--------------------- 1 file changed, 105 insertions(+), 97 deletions(-) diff --git a/src/bin/cargo/cli.rs b/src/bin/cargo/cli.rs index f6c3f5f3497..9d021884664 100644 --- a/src/bin/cargo/cli.rs +++ b/src/bin/cargo/cli.rs @@ -53,50 +53,7 @@ pub fn main(gctx: &mut GlobalContext) -> CliResult { .map(String::as_str) == Some("help") { - let header = style::HEADER; - let literal = style::LITERAL; - let placeholder = style::PLACEHOLDER; - - let options = CliUnstable::help(); - let max_length = options - .iter() - .filter(|(_, help)| help.is_some()) - .map(|(option_name, _)| option_name.len()) - .max() - .unwrap_or(0); - let z_flags = options - .iter() - .filter(|(_, help)| help.is_some()) - .map(|(opt, help)| { - let opt = opt.replace("_", "-"); - let help = help.unwrap(); - format!(" {literal}-Z {opt:Installed Commands:") - ); - for (name, command) in list_commands(gctx) { - let known_external_desc = known_external_command_descriptions.get(name.as_str()); - let literal = style::LITERAL; - match command { - CommandInfo::BuiltIn { about } => { - assert!( - known_external_desc.is_none(), - "known_external_commands shouldn't contain builtin `{name}`", - ); - let summary = about.unwrap_or_default(); - let summary = summary.lines().next().unwrap_or(&summary); // display only the first line - drop_println!(gctx, " {literal}{name:<20}{literal:#} {summary}"); - } - CommandInfo::External { path } => { - if let Some(desc) = known_external_desc { - drop_println!(gctx, " {literal}{name:<20}{literal:#} {desc}"); - } else if is_verbose { - drop_println!( - gctx, - " {literal}{name:<20}{literal:#} {}", - path.display() - ); - } else { - drop_println!(gctx, " {literal}{name}{literal:#}"); - } - } - CommandInfo::Alias { target } => { - drop_println!( - gctx, - " {literal}{name:<20}{literal:#} alias: {}", - target.iter().join(" ") - ); - } - } - } + print_list(gctx, is_verbose); return Ok(()); } @@ -185,6 +90,109 @@ Run with `{literal}cargo -Z{literal:#} {placeholder}[FLAG] [COMMAND]{placeholder exec.exec(gctx, subcommand_args) } +fn print_zhelp(gctx: &GlobalContext) { + let header = style::HEADER; + let literal = style::LITERAL; + let placeholder = style::PLACEHOLDER; + + let options = CliUnstable::help(); + let max_length = options + .iter() + .filter(|(_, help)| help.is_some()) + .map(|(option_name, _)| option_name.len()) + .max() + .unwrap_or(0); + let z_flags = options + .iter() + .filter(|(_, help)| help.is_some()) + .map(|(opt, help)| { + let opt = opt.replace("_", "-"); + let help = help.unwrap(); + format!(" {literal}-Z {opt:Installed Commands:") + ); + for (name, command) in list_commands(gctx) { + let known_external_desc = known_external_command_descriptions.get(name.as_str()); + let literal = style::LITERAL; + match command { + CommandInfo::BuiltIn { about } => { + assert!( + known_external_desc.is_none(), + "known_external_commands shouldn't contain builtin `{name}`", + ); + let summary = about.unwrap_or_default(); + let summary = summary.lines().next().unwrap_or(&summary); // display only the first line + drop_println!(gctx, " {literal}{name:<20}{literal:#} {summary}"); + } + CommandInfo::External { path } => { + if let Some(desc) = known_external_desc { + drop_println!(gctx, " {literal}{name:<20}{literal:#} {desc}"); + } else if is_verbose { + drop_println!( + gctx, + " {literal}{name:<20}{literal:#} {}", + path.display() + ); + } else { + drop_println!(gctx, " {literal}{name}{literal:#}"); + } + } + CommandInfo::Alias { target } => { + drop_println!( + gctx, + " {literal}{name:<20}{literal:#} alias: {}", + target.iter().join(" ") + ); + } + } + } +} + pub fn get_version_string(is_verbose: bool) -> String { let version = cargo::version(); let mut version_string = format!("cargo {}\n", version);