From f67b58f93f00f19663316636d57a63e844449743 Mon Sep 17 00:00:00 2001 From: hi-rustin Date: Sat, 30 Sep 2023 12:34:40 +0800 Subject: [PATCH 1/2] Add test for unsupported short out dir flag --- tests/testsuite/out_dir.rs | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/tests/testsuite/out_dir.rs b/tests/testsuite/out_dir.rs index fe647f56e8a..03add9ac860 100644 --- a/tests/testsuite/out_dir.rs +++ b/tests/testsuite/out_dir.rs @@ -281,6 +281,27 @@ 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 + +Usage: cargo[EXE] build [OPTIONS] + +For more information, try '--help'. +", + ) + .with_status(1) + .run(); +} + fn check_dir_contents( out_dir: &Path, expected_linux: &[&str], From 7bd0f8125709fd6875eb6de0f5e10b42acd15b33 Mon Sep 17 00:00:00 2001 From: hi-rustin Date: Sat, 30 Sep 2023 15:36:53 +0800 Subject: [PATCH 2/2] Add unsupported short suggestion for `--out-dir` flag --- src/bin/cargo/commands/build.rs | 9 +-------- src/cargo/util/command_prelude.rs | 21 +++++++++++++++++++++ tests/testsuite/out_dir.rs | 2 ++ 3 files changed, 24 insertions(+), 8 deletions(-) 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 03add9ac860..83621a2d2d9 100644 --- a/tests/testsuite/out_dir.rs +++ b/tests/testsuite/out_dir.rs @@ -293,6 +293,8 @@ fn unsupported_short_out_dir_flag() { "\ error: unexpected argument '-O' found + tip: a similar argument exists: '--out-dir' + Usage: cargo[EXE] build [OPTIONS] For more information, try '--help'.