1
1
use anyhow:: { anyhow, Context as _} ;
2
2
use cargo:: core:: { features, CliUnstable } ;
3
+ use cargo:: util:: config:: TermConfig ;
3
4
use cargo:: { self , drop_print, drop_println, CargoResult , CliResult , Config } ;
4
5
use clap:: { builder:: UnknownArgumentValueParser , Arg , ArgMatches } ;
5
6
use itertools:: Itertools ;
@@ -12,14 +13,15 @@ use super::commands;
12
13
use super :: list_commands;
13
14
use crate :: command_prelude:: * ;
14
15
use crate :: util:: is_rustup;
16
+ use cargo:: core:: shell:: ColorChoice ;
15
17
use cargo:: util:: style;
16
18
17
19
pub fn main ( config : & mut Config ) -> CliResult {
18
20
// CAUTION: Be careful with using `config` until it is configured below.
19
21
// In general, try to avoid loading config values unless necessary (like
20
22
// the [alias] table).
21
23
22
- let args = cli ( ) . try_get_matches ( ) ?;
24
+ let args = cli ( config ) . try_get_matches ( ) ?;
23
25
24
26
// Update the process-level notion of cwd
25
27
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
172
174
Some ( ( cmd, args) ) => ( cmd, args) ,
173
175
_ => {
174
176
// No subcommand provided.
175
- cli ( ) . print_help ( ) ?;
177
+ cli ( config ) . print_help ( ) ?;
176
178
return Ok ( ( ) ) ;
177
179
}
178
180
} ;
@@ -335,7 +337,9 @@ For more information, see issue #12207 <https://github.com/rust-lang/cargo/issue
335
337
// Note that an alias to an external command will not receive
336
338
// these arguments. That may be confusing, but such is life.
337
339
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 ( config)
341
+ . no_binary_name ( true )
342
+ . try_get_matches_from ( alias) ?;
339
343
340
344
let new_cmd = new_args. subcommand_name ( ) . expect ( "subcommand is required" ) ;
341
345
already_expanded. push ( cmd. to_string ( ) ) ;
@@ -511,7 +515,19 @@ impl GlobalArgs {
511
515
}
512
516
}
513
517
514
- pub fn cli ( ) -> Command {
518
+ pub fn cli ( config : & Config ) -> Command {
519
+ // Don't let config errors get in the way of parsing arguments
520
+ let term = config. get :: < TermConfig > ( "term" ) . unwrap_or_default ( ) ;
521
+ let color = term
522
+ . color
523
+ . and_then ( |c| c. parse ( ) . ok ( ) )
524
+ . unwrap_or ( ColorChoice :: CargoAuto ) ;
525
+ let color = match color {
526
+ ColorChoice :: Always => clap:: ColorChoice :: Always ,
527
+ ColorChoice :: Never => clap:: ColorChoice :: Never ,
528
+ ColorChoice :: CargoAuto => clap:: ColorChoice :: Auto ,
529
+ } ;
530
+
515
531
let usage = if is_rustup ( ) {
516
532
color_print:: cstr!( "<cyan,bold>cargo</> <cyan>[+toolchain] [OPTIONS] [COMMAND]</>\n <cyan,bold>cargo</> <cyan>[+toolchain] [OPTIONS]</> <cyan,bold>-Zscript</> <cyan><<MANIFEST_RS>> [ARGS]...</>" )
517
533
} else {
@@ -536,6 +552,7 @@ pub fn cli() -> Command {
536
552
// We also want these to come before auto-generated `--help`
537
553
. next_display_order ( 800 )
538
554
. allow_external_subcommands ( true )
555
+ . color ( color)
539
556
. styles ( styles)
540
557
// Provide a custom help subcommand for calling into man pages
541
558
. disable_help_subcommand ( true )
@@ -645,7 +662,8 @@ See '<cyan,bold>cargo help</> <cyan><<command>></>' for more information on a sp
645
662
646
663
#[ test]
647
664
fn verify_cli ( ) {
648
- cli ( ) . debug_assert ( ) ;
665
+ let config = Config :: default ( ) . unwrap ( ) ;
666
+ cli ( & config) . debug_assert ( ) ;
649
667
}
650
668
651
669
#[ test]
0 commit comments