Skip to content

Commit 7084570

Browse files
committed
Display alias target on 'cargo help <alias>`. Closes #10138
Previously, `cargo help <alias>` resolved the alias and displayed the help for the targeted subcommand. For example, if `br` were aliased to `build --release`, `cargo help br` would display the manpage for cargo-build. With this patch, it will print "'br' is aliased to 'build --release'".
1 parent 6d75259 commit 7084570

File tree

1 file changed

+25
-17
lines changed

1 file changed

+25
-17
lines changed

src/bin/cargo/commands/help.rs

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::aliased_command;
22
use cargo::util::errors::CargoResult;
3-
use cargo::Config;
3+
use cargo::{drop_println, Config};
44
use cargo_util::paths::resolve_executable;
55
use flate2::read::GzDecoder;
66
use std::ffi::OsString;
@@ -46,8 +46,16 @@ fn try_help(config: &Config) -> CargoResult<bool> {
4646
Some(s) => s,
4747
None => return Ok(false),
4848
};
49-
// Check if this is a built-in command (or alias);
50-
let subcommand = match check_alias(config, subcommand) {
49+
50+
// Check if this is an alias. If so, just display the alias information.
51+
if let Some(argv) = check_alias(config, subcommand) {
52+
let alias = argv.join(" ");
53+
drop_println!(config, "'{}' is aliased to '{}'", subcommand, alias);
54+
return Ok(true);
55+
}
56+
57+
// If not an alias, this should be a built-in subcommand.
58+
let subcommand = match check_builtin(subcommand) {
5159
Some(s) => s,
5260
None => return Ok(false),
5361
};
@@ -73,26 +81,26 @@ fn try_help(config: &Config) -> CargoResult<bool> {
7381
Ok(true)
7482
}
7583

76-
/// Checks if the given subcommand is a built-in command (possibly via an alias).
84+
/// Checks if the given subcommand is an alias.
7785
///
78-
/// Returns None if it is not a built-in command.
79-
fn check_alias(config: &Config, subcommand: &str) -> Option<String> {
80-
if super::builtin_exec(subcommand).is_some() {
81-
return Some(subcommand.to_string());
82-
}
86+
/// Returns None if it is not an alias.
87+
fn check_alias(config: &Config, subcommand: &str) -> Option<Vec<String>> {
8388
match aliased_command(config, subcommand) {
84-
Ok(Some(alias)) => {
85-
let alias = alias.into_iter().next()?;
86-
if super::builtin_exec(&alias).is_some() {
87-
Some(alias)
88-
} else {
89-
None
90-
}
91-
}
89+
Ok(Some(alias)) => Some(alias),
9290
_ => None,
9391
}
9492
}
9593

94+
/// Checks if the given subcommand is a built-in command (not via an alias).
95+
///
96+
/// Returns None if it is not a built-in command.
97+
fn check_builtin(subcommand: &str) -> Option<String> {
98+
match super::builtin_exec(subcommand) {
99+
Some(_) => Some(subcommand.to_string()),
100+
None => None,
101+
}
102+
}
103+
96104
/// Extracts the given man page from the compressed archive.
97105
///
98106
/// Returns None if the command wasn't found.

0 commit comments

Comments
 (0)