Skip to content

Commit

Permalink
Show --help
Browse files Browse the repository at this point in the history
Signed-off-by: hi-rustin <[email protected]>
  • Loading branch information
Rustin170506 committed Dec 16, 2022
1 parent 760708d commit 8d6ea3a
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 9 deletions.
25 changes: 17 additions & 8 deletions src/bin/cargo/commands/help.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use crate::aliased_command;
use crate::command_prelude::*;
use anyhow::bail;
use cargo::util::errors::CargoResult;
use cargo::{drop_println, Config};
use cargo_util::paths::resolve_executable;
Expand All @@ -23,11 +22,21 @@ pub fn exec(config: &mut Config, args: &ArgMatches) -> CliResult {
let subcommand = args.get_one::<String>("COMMAND");
if let Some(subcommand) = subcommand {
if !try_help(config, subcommand)? {
crate::execute_external_subcommand(
config,
subcommand,
&[OsStr::new(subcommand), OsStr::new("--help")],
)?;
match check_builtin(&subcommand) {
Some(s) => {
crate::execute_internal_subcommand(
config,
&[OsStr::new(s), OsStr::new("--help")],
)?;
}
None => {
crate::execute_external_subcommand(
config,
subcommand,
&[OsStr::new(subcommand), OsStr::new("--help")],
)?;
}
}
}
} else {
let mut cmd = crate::cli::cli();
Expand Down Expand Up @@ -61,13 +70,13 @@ fn try_help(config: &Config, subcommand: &str) -> CargoResult<bool> {
if resolve_executable(Path::new("man")).is_ok() {
let man = match extract_man(subcommand, "1") {
Some(man) => man,
None => bail!("no man page for `{}`", subcommand),
None => return Ok(false),
};
write_and_spawn(subcommand, &man, "man")?;
} else {
let txt = match extract_man(subcommand, "txt") {
Some(txt) => txt,
None => bail!("no man page for `{}`", subcommand),
None => return Ok(false),
};
if resolve_executable(Path::new("less")).is_ok() {
write_and_spawn(subcommand, &txt, "less")?;
Expand Down
15 changes: 14 additions & 1 deletion src/bin/cargo/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,9 +189,22 @@ fn execute_external_subcommand(config: &Config, cmd: &str, args: &[&OsStr]) -> C
return Err(CliError::new(err, 101));
}
};
execute_subcommand(config, Some(&command), args)
}

fn execute_internal_subcommand(config: &Config, args: &[&OsStr]) -> CliResult {
execute_subcommand(config, None, args)
}

// This function is used to execute a subcommand. It is used to execute both
// internal and external subcommands.
// If `cmd_path` is `None`, then the subcommand is an internal subcommand.
fn execute_subcommand(config: &Config, cmd_path: Option<&PathBuf>, args: &[&OsStr]) -> CliResult {
let cargo_exe = config.cargo_exe()?;
let mut cmd = ProcessBuilder::new(&command);
let mut cmd = match cmd_path {
Some(cmd_path) => ProcessBuilder::new(cmd_path),
None => ProcessBuilder::new(&cargo_exe),
};
cmd.env(cargo::CARGO_ENV, cargo_exe).args(args);
if let Some(client) = config.jobserver_from_env() {
cmd.inherit_jobserver(client);
Expand Down

0 comments on commit 8d6ea3a

Please sign in to comment.