X-Git-Url: https://git.proxmox.com/?a=blobdiff_plain;f=src%2Fbin%2Fcargo%2Fcommands%2Fhelp.rs;h=fa0d98fd81ac79fdf8df193eb1c0397ec60a511e;hb=d674c2294ba8392ddbd9897ad9a285615f97994b;hp=caaa0c5a49687c56a3e38543bf4924d3a3cda133;hpb=180f599f6078e4bb237ca5ab6104f724e3aab39c;p=cargo.git diff --git a/src/bin/cargo/commands/help.rs b/src/bin/cargo/commands/help.rs index caaa0c5a4..fa0d98fd8 100644 --- a/src/bin/cargo/commands/help.rs +++ b/src/bin/cargo/commands/help.rs @@ -1,4 +1,5 @@ use crate::aliased_command; +use crate::command_prelude::*; use cargo::util::errors::CargoResult; use cargo::{drop_println, Config}; use cargo_util::paths::resolve_executable; @@ -10,43 +11,26 @@ use std::path::Path; const COMPRESSED_MAN: &[u8] = include_bytes!(concat!(env!("OUT_DIR"), "/man.tgz")); -/// Checks if the `help` command is being issued. -/// -/// This runs before clap processing, because it needs to intercept the `help` -/// command if a man page is available. -/// -/// Returns `true` if help information was successfully displayed to the user. -/// In this case, Cargo should exit. -pub fn handle_embedded_help(config: &Config) -> bool { - match try_help(config) { - Ok(true) => true, - Ok(false) => false, - Err(e) => { - log::warn!("help failed: {:?}", e); - false - } - } +pub fn cli() -> App { + subcommand("help") + .about("Displays help for a cargo subcommand") + .arg(Arg::new("SUBCOMMAND")) } -fn try_help(config: &Config) -> CargoResult { - let mut args = std::env::args_os() - .skip(1) - .skip_while(|arg| arg.to_str().map_or(false, |s| s.starts_with('-'))); - if !args - .next() - .map_or(false, |arg| arg.to_str() == Some("help")) - { - return Ok(false); +pub fn exec(config: &mut Config, args: &ArgMatches) -> CliResult { + let subcommand = args.get_one::("SUBCOMMAND"); + if let Some(subcommand) = subcommand { + if !try_help(config, subcommand)? { + crate::execute_external_subcommand(config, subcommand, &[subcommand, "--help"])?; + } + } else { + let mut cmd = crate::cli::cli(); + let _ = cmd.print_help(); } - let subcommand = match args.next() { - Some(arg) => arg, - None => return Ok(false), - }; - let subcommand = match subcommand.to_str() { - Some(s) => s, - None => return Ok(false), - }; + Ok(()) +} +fn try_help(config: &Config, subcommand: &str) -> CargoResult { let subcommand = match check_alias(config, subcommand) { // If this alias is more than a simple subcommand pass-through, show the alias. Some(argv) if argv.len() > 1 => { @@ -69,20 +53,20 @@ fn try_help(config: &Config) -> CargoResult { }; if resolve_executable(Path::new("man")).is_ok() { - let man = match extract_man(&subcommand, "1") { + let man = match extract_man(subcommand, "1") { Some(man) => man, None => return Ok(false), }; - write_and_spawn(&subcommand, &man, "man")?; + write_and_spawn(subcommand, &man, "man")?; } else { - let txt = match extract_man(&subcommand, "txt") { + let txt = match extract_man(subcommand, "txt") { Some(txt) => txt, None => return Ok(false), }; if resolve_executable(Path::new("less")).is_ok() { - write_and_spawn(&subcommand, &txt, "less")?; + write_and_spawn(subcommand, &txt, "less")?; } else if resolve_executable(Path::new("more")).is_ok() { - write_and_spawn(&subcommand, &txt, "more")?; + write_and_spawn(subcommand, &txt, "more")?; } else { drop(std::io::stdout().write_all(&txt)); }