Skip to content

Commit 6df4b1e

Browse files
committed
Update behavior to pass-through simple aliases
This changes the behavior so that simple aliases that directly alias a subcommand (with no arguments) pass-through to that subcommand, while complex aliases (with arguments) show the alias. So for example, `cargo help b` will show the manpage for `cargo-build`, while `cargo help my-alias`, aliased to `build --release`, will show "`my-alias` is aliased to `build --release`".
1 parent 8a1af70 commit 6df4b1e

File tree

2 files changed

+26
-16
lines changed

2 files changed

+26
-16
lines changed

src/bin/cargo/commands/help.rs

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,14 @@ const COMPRESSED_MAN: &[u8] = include_bytes!(concat!(env!("OUT_DIR"), "/man.tgz"
1515
/// This runs before clap processing, because it needs to intercept the `help`
1616
/// command if a man page is available.
1717
///
18-
/// Returns `true` if a man page was displayed. In this case, Cargo should
19-
/// exit.
18+
/// Returns `true` if help information was successfully displayed to the user.
19+
/// In this case, Cargo should exit.
2020
pub fn handle_embedded_help(config: &Config) -> bool {
2121
match try_help(config) {
2222
Ok(true) => true,
2323
Ok(false) => false,
2424
Err(e) => {
25-
log::warn!("man failed: {:?}", e);
25+
log::warn!("help failed: {:?}", e);
2626
false
2727
}
2828
}
@@ -47,18 +47,23 @@ fn try_help(config: &Config) -> CargoResult<bool> {
4747
None => return Ok(false),
4848
};
4949

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-
}
50+
let subcommand = match check_alias(config, subcommand) {
51+
// If this alias is more than a simple subcommand pass-through, show the alias.
52+
Some(argv) if argv.len() > 1 => {
53+
let alias = argv.join(" ");
54+
drop_println!(config, "`{}` is aliased to `{}`", subcommand, alias);
55+
return Ok(true);
56+
}
57+
// Otherwise, resolve the alias into its subcommand.
58+
Some(argv) => argv[0].clone(),
59+
None => subcommand.to_string(),
60+
};
5661

57-
// If not an alias, this should be a built-in subcommand.
58-
let subcommand = match check_builtin(subcommand) {
62+
let subcommand = match check_builtin(&subcommand) {
5963
Some(s) => s,
6064
None => return Ok(false),
6165
};
66+
6267
if resolve_executable(Path::new("man")).is_ok() {
6368
let man = match extract_man(&subcommand, "1") {
6469
Some(man) => man,

tests/testsuite/help.rs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -138,21 +138,26 @@ fn help_man() {
138138
#[cargo_test]
139139
fn help_alias() {
140140
// Check that `help some_alias` will resolve.
141-
let out = help_with_stdout_and_path("b", Path::new(""));
142-
assert_eq!(out, "'b' is aliased to 'build'\n");
141+
help_with_man_and_path("", "b", "build", Path::new(""));
143142

144143
let config = paths::root().join(".cargo/config");
145144
fs::create_dir_all(config.parent().unwrap()).unwrap();
146145
fs::write(
147146
config,
148147
r#"
149148
[alias]
150-
my-alias = ["build", "--release"]
149+
simple-alias = ["build"]
150+
complex-alias = ["build", "--release"]
151151
"#,
152152
)
153153
.unwrap();
154-
let out = help_with_stdout_and_path("my-alias", Path::new(""));
155-
assert_eq!(out, "'my-alias' is aliased to 'build --release'\n");
154+
155+
// Because `simple-alias` aliases a subcommand with no arguments, help shows the manpage.
156+
help_with_man_and_path("", "simple-alias", "build", Path::new(""));
157+
158+
// Help for `complex-alias` displays the full alias command.
159+
let out = help_with_stdout_and_path("complex-alias", Path::new(""));
160+
assert_eq!(out, "`complex-alias` is aliased to `build --release`\n");
156161
}
157162

158163
#[cargo_test]

0 commit comments

Comments
 (0)