diff --git a/src/bin/cargo/cli.rs b/src/bin/cargo/cli.rs index 056d54cb609d..91561d606792 100644 --- a/src/bin/cargo/cli.rs +++ b/src/bin/cargo/cli.rs @@ -1,5 +1,6 @@ use anyhow::{anyhow, Context as _}; use cargo::core::{features, CliUnstable}; +use cargo::util::config::TermConfig; use cargo::{self, drop_print, drop_println, CargoResult, CliResult, Config}; use clap::{builder::UnknownArgumentValueParser, Arg, ArgMatches}; use itertools::Itertools; @@ -12,6 +13,7 @@ use super::commands; use super::list_commands; use crate::command_prelude::*; use crate::util::is_rustup; +use cargo::core::shell::ColorChoice; use cargo::util::style; pub fn main(config: &mut Config) -> CliResult { @@ -19,7 +21,7 @@ pub fn main(config: &mut Config) -> CliResult { // In general, try to avoid loading config values unless necessary (like // the [alias] table). - let args = cli().try_get_matches()?; + let args = cli(config).try_get_matches()?; // Update the process-level notion of cwd if let Some(new_cwd) = args.get_one::("directory") { @@ -172,7 +174,7 @@ Run with `{literal}cargo -Z{literal:#} {placeholder}[FLAG] [COMMAND]{placeholder Some((cmd, args)) => (cmd, args), _ => { // No subcommand provided. - cli().print_help()?; + cli(config).print_help()?; return Ok(()); } }; @@ -335,7 +337,9 @@ For more information, see issue #12207 Command { +pub fn cli(config: &Config) -> Command { + // Don't let config errors get in the way of parsing arguments + let term = config.get::("term").unwrap_or_default(); + let color = term + .color + .and_then(|c| c.parse().ok()) + .unwrap_or(ColorChoice::CargoAuto); + let color = match color { + ColorChoice::Always => clap::ColorChoice::Always, + ColorChoice::Never => clap::ColorChoice::Never, + ColorChoice::CargoAuto => clap::ColorChoice::Auto, + }; + let usage = if is_rustup() { color_print::cstr!("cargo [+toolchain] [OPTIONS] [COMMAND]\n cargo [+toolchain] [OPTIONS] -Zscript <> [ARGS]...") } else { @@ -536,6 +552,7 @@ pub fn cli() -> Command { // We also want these to come before auto-generated `--help` .next_display_order(800) .allow_external_subcommands(true) + .color(color) .styles(styles) // Provide a custom help subcommand for calling into man pages .disable_help_subcommand(true) diff --git a/src/bin/cargo/commands/help.rs b/src/bin/cargo/commands/help.rs index 2839b931e247..8ff824b60d42 100644 --- a/src/bin/cargo/commands/help.rs +++ b/src/bin/cargo/commands/help.rs @@ -39,7 +39,7 @@ pub fn exec(config: &mut Config, args: &ArgMatches) -> CliResult { } } } else { - let mut cmd = crate::cli::cli(); + let mut cmd = crate::cli::cli(config); let _ = cmd.print_help(); } Ok(()) diff --git a/src/cargo/util/config/mod.rs b/src/cargo/util/config/mod.rs index 589e3c6ccfb1..654718329857 100644 --- a/src/cargo/util/config/mod.rs +++ b/src/cargo/util/config/mod.rs @@ -2626,14 +2626,14 @@ impl BuildTargetConfig { } #[derive(Deserialize, Default)] -struct TermConfig { - verbose: Option, - quiet: Option, - color: Option, - hyperlinks: Option, +pub struct TermConfig { + pub verbose: Option, + pub quiet: Option, + pub color: Option, + pub hyperlinks: Option, #[serde(default)] #[serde(deserialize_with = "progress_or_string")] - progress: Option, + pub progress: Option, } #[derive(Debug, Default, Deserialize)]