11use anyhow:: { anyhow, Context as _} ;
22use cargo:: core:: { features, CliUnstable } ;
3+ use cargo:: util:: config:: TermConfig ;
34use cargo:: { drop_print, drop_println, CargoResult } ;
45use clap:: builder:: UnknownArgumentValueParser ;
56use itertools:: Itertools ;
@@ -12,14 +13,15 @@ use super::commands;
1213use super :: list_commands;
1314use crate :: command_prelude:: * ;
1415use crate :: util:: is_rustup;
16+ use cargo:: core:: shell:: ColorChoice ;
1517use cargo:: util:: style;
1618
1719pub fn main ( gctx : & mut GlobalContext ) -> CliResult {
1820 // CAUTION: Be careful with using `config` until it is configured below.
1921 // In general, try to avoid loading config values unless necessary (like
2022 // the [alias] table).
2123
22- let args = cli ( ) . try_get_matches ( ) ?;
24+ let args = cli ( gctx ) . try_get_matches ( ) ?;
2325
2426 // Update the process-level notion of cwd
2527 if let Some ( new_cwd) = args. get_one :: < std:: path:: PathBuf > ( "directory" ) {
@@ -172,7 +174,7 @@ Run with `{literal}cargo -Z{literal:#} {placeholder}[FLAG] [COMMAND]{placeholder
172174 Some ( ( cmd, args) ) => ( cmd, args) ,
173175 _ => {
174176 // No subcommand provided.
175- cli ( ) . print_help ( ) ?;
177+ cli ( gctx ) . print_help ( ) ?;
176178 return Ok ( ( ) ) ;
177179 }
178180 } ;
@@ -335,7 +337,7 @@ For more information, see issue #12207 <https://github.com/rust-lang/cargo/issue
335337 // Note that an alias to an external command will not receive
336338 // these arguments. That may be confusing, but such is life.
337339 let global_args = GlobalArgs :: new ( sub_args) ;
338- let new_args = cli ( ) . no_binary_name ( true ) . try_get_matches_from ( alias) ?;
340+ let new_args = cli ( gctx ) . no_binary_name ( true ) . try_get_matches_from ( alias) ?;
339341
340342 let new_cmd = new_args. subcommand_name ( ) . expect ( "subcommand is required" ) ;
341343 already_expanded. push ( cmd. to_string ( ) ) ;
@@ -511,7 +513,19 @@ impl GlobalArgs {
511513 }
512514}
513515
514- pub fn cli ( ) -> Command {
516+ pub fn cli ( gctx : & GlobalContext ) -> Command {
517+ // Don't let config errors get in the way of parsing arguments
518+ let term = gctx. get :: < TermConfig > ( "term" ) . unwrap_or_default ( ) ;
519+ let color = term
520+ . color
521+ . and_then ( |c| c. parse ( ) . ok ( ) )
522+ . unwrap_or ( ColorChoice :: CargoAuto ) ;
523+ let color = match color {
524+ ColorChoice :: Always => clap:: ColorChoice :: Always ,
525+ ColorChoice :: Never => clap:: ColorChoice :: Never ,
526+ ColorChoice :: CargoAuto => clap:: ColorChoice :: Auto ,
527+ } ;
528+
515529 let usage = if is_rustup ( ) {
516530 color_print:: cstr!( "<cyan,bold>cargo</> <cyan>[+toolchain] [OPTIONS] [COMMAND]</>\n <cyan,bold>cargo</> <cyan>[+toolchain] [OPTIONS]</> <cyan,bold>-Zscript</> <cyan><<MANIFEST_RS>> [ARGS]...</>" )
517531 } else {
@@ -536,6 +550,7 @@ pub fn cli() -> Command {
536550 // We also want these to come before auto-generated `--help`
537551 . next_display_order ( 800 )
538552 . allow_external_subcommands ( true )
553+ . color ( color)
539554 . styles ( styles)
540555 // Provide a custom help subcommand for calling into man pages
541556 . disable_help_subcommand ( true )
@@ -645,7 +660,8 @@ See '<cyan,bold>cargo help</> <cyan><<command>></>' for more information on a sp
645660
646661#[ test]
647662fn verify_cli ( ) {
648- cli ( ) . debug_assert ( ) ;
663+ let gctx = GlobalContext :: default ( ) . unwrap ( ) ;
664+ cli ( & gctx) . debug_assert ( ) ;
649665}
650666
651667#[ test]
0 commit comments