diff --git a/src/bin/cargo/commands/build.rs b/src/bin/cargo/commands/build.rs index d503f43ddcc..083b43e45ea 100644 --- a/src/bin/cargo/commands/build.rs +++ b/src/bin/cargo/commands/build.rs @@ -35,14 +35,7 @@ pub fn cli() -> Command { .arg_parallel() .arg_target_triple("Build for the target triple") .arg_target_dir() - .arg( - opt( - "out-dir", - "Copy final artifacts to this directory (unstable)", - ) - .value_name("PATH") - .help_heading(heading::COMPILATION_OPTIONS), - ) + .arg_out_dir() .arg_build_plan() .arg_unit_graph() .arg_timings() diff --git a/src/cargo/util/command_prelude.rs b/src/cargo/util/command_prelude.rs index bd8889bef2c..a8b8e31c7eb 100644 --- a/src/cargo/util/command_prelude.rs +++ b/src/cargo/util/command_prelude.rs @@ -357,6 +357,27 @@ pub trait CommandExt: Sized { .help_heading(heading::COMPILATION_OPTIONS), ) } + + fn arg_out_dir(self) -> Self { + let unsupported_short_arg = { + let value_parser = UnknownArgumentValueParser::suggest_arg("--out-dir"); + Arg::new("unsupported-short-out-dir-flag") + .help("") + .short('O') + .value_parser(value_parser) + .action(ArgAction::SetTrue) + .hide(true) + }; + self._arg( + opt( + "out-dir", + "Copy final artifacts to this directory (unstable)", + ) + .value_name("PATH") + .help_heading(heading::COMPILATION_OPTIONS), + ) + ._arg(unsupported_short_arg) + } } impl CommandExt for Command { diff --git a/tests/testsuite/out_dir.rs b/tests/testsuite/out_dir.rs index fe647f56e8a..83621a2d2d9 100644 --- a/tests/testsuite/out_dir.rs +++ b/tests/testsuite/out_dir.rs @@ -281,6 +281,29 @@ fn cargo_build_out_dir() { ); } +#[cargo_test] +fn unsupported_short_out_dir_flag() { + let p = project() + .file("src/main.rs", r#"fn main() { println!("Hello, World!") }"#) + .build(); + + p.cargo("build -Z unstable-options -O") + .masquerade_as_nightly_cargo(&["out-dir"]) + .with_stderr( + "\ +error: unexpected argument '-O' found + + tip: a similar argument exists: '--out-dir' + +Usage: cargo[EXE] build [OPTIONS] + +For more information, try '--help'. +", + ) + .with_status(1) + .run(); +} + fn check_dir_contents( out_dir: &Path, expected_linux: &[&str],