diff --git a/src/bin/cargo/cli.rs b/src/bin/cargo/cli.rs index 253de9a9692..e4838753d4d 100644 --- a/src/bin/cargo/cli.rs +++ b/src/bin/cargo/cli.rs @@ -25,7 +25,24 @@ pub fn main(gctx: &mut GlobalContext) -> CliResult { // In general, try to avoid loading config values unless necessary (like // the [alias] table). - let args = cli(gctx).try_get_matches()?; + let args = match cli(gctx).try_get_matches() { + Ok(args) => args, + Err(e) => { + // When --help is requested, clap returns DisplayHelp error. + // If --list was also provided, show the list instead since users + // may interpret "See all commands with --list" as meaning they + // should run `cargo --help --list`. + if e.kind() == clap::error::ErrorKind::DisplayHelp { + let raw_args: Vec = std::env::args().collect(); + if raw_args.iter().any(|arg| arg == "--list") { + let is_verbose = raw_args.iter().any(|arg| arg == "-v" || arg == "--verbose"); + print_list(gctx, is_verbose); + return Ok(()); + } + } + return Err(e.into()); + } + }; // Update the process-level notion of cwd if let Some(new_cwd) = args.get_one::("directory") { diff --git a/tests/testsuite/cargo_command.rs b/tests/testsuite/cargo_command.rs index e0fe47dd377..56065903600 100644 --- a/tests/testsuite/cargo_command.rs +++ b/tests/testsuite/cargo_command.rs @@ -654,3 +654,44 @@ fn overwrite_cargo_environment_variable() { .with_stderr_contains(stderr_other_cargo) .run(); } + +#[cargo_test] +fn help_list_combination() { + // Test that `--help --list` is treated as `--list`. + // When the help output says "See all commands with --list", users may + // naturally try `cargo --help --list`. This should show the list. + cargo_process("--help --list") + .with_stdout_data(str![[r#" +Installed Commands: +... + build Compile a local package and all of its dependencies +... +"#]]) + .run(); +} + +#[cargo_test] +fn help_list_reverse_order() { + // Test that `--list --help` is treated as `--list` (order shouldn't matter). + cargo_process("--list --help") + .with_stdout_data(str![[r#" +Installed Commands: +... + build Compile a local package and all of its dependencies +... +"#]]) + .run(); +} + +#[cargo_test] +fn help_short_flag_with_list() { + // Test that `-h --list` is treated as `--list`. + cargo_process("-h --list") + .with_stdout_data(str![[r#" +Installed Commands: +... + build Compile a local package and all of its dependencies +... +"#]]) + .run(); +}