Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion src/bin/cargo/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> = 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::<std::path::PathBuf>("directory") {
Expand Down
41 changes: 41 additions & 0 deletions tests/testsuite/cargo_command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}