diff --git a/src/bin/cargo/cli.rs b/src/bin/cargo/cli.rs index 06b4a20e231..7c0e3b5ee85 100644 --- a/src/bin/cargo/cli.rs +++ b/src/bin/cargo/cli.rs @@ -618,7 +618,7 @@ See 'cargo help <>' for more information on a sp .help_heading(heading::MANIFEST_OPTIONS) .global(true), ) - .arg(multi_opt("config", "KEY=VALUE", "Override a configuration value").global(true)) + .arg_config() .arg( Arg::new("unstable-features") .help("Unstable (nightly-only) flags to Cargo, see 'cargo -Z help' for details") diff --git a/src/cargo/util/command_prelude.rs b/src/cargo/util/command_prelude.rs index a8b8e31c7eb..bfc2c45cd05 100644 --- a/src/cargo/util/command_prelude.rs +++ b/src/cargo/util/command_prelude.rs @@ -378,6 +378,21 @@ pub trait CommandExt: Sized { ) ._arg(unsupported_short_arg) } + + fn arg_config(self) -> Self { + let unsupported_short_arg = { + let value_parser = UnknownArgumentValueParser::suggest_arg("--config"); + Arg::new("unsupported-short-config-flag") + .help("") + .short('c') + .value_parser(value_parser) + .action(ArgAction::SetTrue) + .global(true) + .hide(true) + }; + self._arg(unsupported_short_arg) + ._arg(multi_opt("config", "KEY=VALUE", "Override a configuration value").global(true)) + } } impl CommandExt for Command { diff --git a/tests/testsuite/build.rs b/tests/testsuite/build.rs index 1afa83918f0..bb6404692bc 100644 --- a/tests/testsuite/build.rs +++ b/tests/testsuite/build.rs @@ -158,6 +158,29 @@ For more information, try '--help'. .run(); } +#[cargo_test] +fn cargo_compile_with_unsupported_short_config_flag() { + let p = project() + .file("Cargo.toml", &basic_bin_manifest("foo")) + .file("src/foo.rs", &main_file(r#""i am foo""#, &[])) + .build(); + + p.cargo("build -c net.git-fetch-with-cli=true") + .with_stderr( + "\ +error: unexpected argument '-c' found + + tip: a similar argument exists: '--config' + +Usage: cargo[EXE] build [OPTIONS] + +For more information, try '--help'. +", + ) + .with_status(1) + .run(); +} + #[cargo_test] fn cargo_compile_with_workspace_excluded() { let p = project().file("src/main.rs", "fn main() {}").build();