/// 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
}
}
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,
#[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();
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]