diff --git a/src/bin/cargo/commands/help.rs b/src/bin/cargo/commands/help.rs index cd4e686a3..1adc129db 100644 --- a/src/bin/cargo/commands/help.rs +++ b/src/bin/cargo/commands/help.rs @@ -15,14 +15,14 @@ const COMPRESSED_MAN: &[u8] = include_bytes!(concat!(env!("OUT_DIR"), "/man.tgz" /// This runs before clap processing, because it needs to intercept the `help` /// command if a man page is available. /// -/// Returns `true` if a man page was displayed. In this case, Cargo should -/// exit. +/// 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!("man failed: {:?}", e); + log::warn!("help failed: {:?}", e); false } } @@ -47,18 +47,23 @@ fn try_help(config: &Config) -> CargoResult { None => return Ok(false), }; - // Check if this is an alias. If so, just display the alias information. - if let Some(argv) = check_alias(config, subcommand) { - let alias = argv.join(" "); - drop_println!(config, "'{}' is aliased to '{}'", subcommand, alias); - return Ok(true); - } + 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 => { + let alias = argv.join(" "); + drop_println!(config, "`{}` is aliased to `{}`", subcommand, alias); + return Ok(true); + } + // Otherwise, resolve the alias into its subcommand. + Some(argv) => argv[0].clone(), + None => subcommand.to_string(), + }; - // If not an alias, this should be a built-in subcommand. - let subcommand = match check_builtin(subcommand) { + let subcommand = match check_builtin(&subcommand) { Some(s) => s, None => return Ok(false), }; + if resolve_executable(Path::new("man")).is_ok() { let man = match extract_man(&subcommand, "1") { Some(man) => man, diff --git a/tests/testsuite/help.rs b/tests/testsuite/help.rs index 0871d7f4b..7cad8ba25 100644 --- a/tests/testsuite/help.rs +++ b/tests/testsuite/help.rs @@ -138,8 +138,7 @@ fn help_man() { #[cargo_test] fn help_alias() { // Check that `help some_alias` will resolve. - let out = help_with_stdout_and_path("b", Path::new("")); - assert_eq!(out, "'b' is aliased to 'build'\n"); + help_with_man_and_path("", "b", "build", Path::new("")); let config = paths::root().join(".cargo/config"); fs::create_dir_all(config.parent().unwrap()).unwrap(); @@ -147,12 +146,18 @@ fn help_alias() { config, r#" [alias] - my-alias = ["build", "--release"] + simple-alias = ["build"] + complex-alias = ["build", "--release"] "#, ) .unwrap(); - let out = help_with_stdout_and_path("my-alias", Path::new("")); - assert_eq!(out, "'my-alias' is aliased to 'build --release'\n"); + + // Because `simple-alias` aliases a subcommand with no arguments, help shows the manpage. + help_with_man_and_path("", "simple-alias", "build", Path::new("")); + + // Help for `complex-alias` displays the full alias command. + let out = help_with_stdout_and_path("complex-alias", Path::new("")); + assert_eq!(out, "`complex-alias` is aliased to `build --release`\n"); } #[cargo_test]