]> git.proxmox.com Git - cargo.git/commitdiff
Update behavior to pass-through simple aliases
authorSean Stangl <sean.stangl@gmail.com>
Sun, 12 Dec 2021 17:00:23 +0000 (10:00 -0700)
committerSean Stangl <sean.stangl@gmail.com>
Sun, 12 Dec 2021 17:18:19 +0000 (10:18 -0700)
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`".

src/bin/cargo/commands/help.rs
tests/testsuite/help.rs

index cd4e686a33d0c2c391b3a87435a1cd188036d6e8..1adc129db9620d52da3b46d4629009efbf3bdbf7 100644 (file)
@@ -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<bool> {
         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,
index 0871d7f4b73abfef56b8649e2557cd5762facdaf..7cad8ba25ae9545decd4a74804774ba0502d450f 100644 (file)
@@ -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]