From 2f904301102747c509d37632e805ab3d31aaa1fa Mon Sep 17 00:00:00 2001 From: hi-rustin Date: Wed, 11 Oct 2023 09:13:55 +0800 Subject: [PATCH 1/4] Add test for unsupported short target triple flag Signed-off-by: hi-rustin --- tests/testsuite/build.rs | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/tests/testsuite/build.rs b/tests/testsuite/build.rs index 1169ecc6c93..3b267559c93 100644 --- a/tests/testsuite/build.rs +++ b/tests/testsuite/build.rs @@ -4239,6 +4239,28 @@ fn cargo_build_empty_target() { .run(); } +#[cargo_test] +fn cargo_build_with_unsupported_short_target_flag() { + let p = project() + .file("Cargo.toml", &basic_bin_manifest("foo")) + .file("src/main.rs", "fn main() {}") + .build(); + + p.cargo("build -t") + .arg("") + .with_stderr( + "\ +error: unexpected argument '-t' found + +Usage: cargo[EXE] build [OPTIONS] + +For more information, try '--help'. +", + ) + .with_status(1) + .run(); +} + #[cargo_test] fn build_all_workspace() { let p = project() From 819a836e9a7d38d255cf2184281d75c8362d1b41 Mon Sep 17 00:00:00 2001 From: hi-rustin Date: Wed, 11 Oct 2023 09:16:26 +0800 Subject: [PATCH 2/4] Add unsupported short flag suggestion for `--target` flag Signed-off-by: hi-rustin --- src/cargo/util/command_prelude.rs | 10 ++++++++++ tests/testsuite/build.rs | 2 ++ 2 files changed, 12 insertions(+) diff --git a/src/cargo/util/command_prelude.rs b/src/cargo/util/command_prelude.rs index a8b8e31c7eb..01686c8e79e 100644 --- a/src/cargo/util/command_prelude.rs +++ b/src/cargo/util/command_prelude.rs @@ -232,10 +232,20 @@ pub trait CommandExt: Sized { } fn arg_target_triple(self, target: &'static str) -> Self { + let unsupported_short_arg = { + let value_parser = UnknownArgumentValueParser::suggest_arg("--target"); + Arg::new("unsupported-short-target-flag") + .help("") + .short('t') + .value_parser(value_parser) + .action(ArgAction::SetTrue) + .hide(true) + }; self._arg( optional_multi_opt("target", "TRIPLE", target) .help_heading(heading::COMPILATION_OPTIONS), ) + ._arg(unsupported_short_arg) } fn arg_target_dir(self) -> Self { diff --git a/tests/testsuite/build.rs b/tests/testsuite/build.rs index 3b267559c93..b706e4eb6d1 100644 --- a/tests/testsuite/build.rs +++ b/tests/testsuite/build.rs @@ -4252,6 +4252,8 @@ fn cargo_build_with_unsupported_short_target_flag() { "\ error: unexpected argument '-t' found + tip: a similar argument exists: '--target' + Usage: cargo[EXE] build [OPTIONS] For more information, try '--help'. From 718b69ce192f0d2262ff3f8f004419698102f114 Mon Sep 17 00:00:00 2001 From: hi-rustin Date: Wed, 11 Oct 2023 09:39:13 +0800 Subject: [PATCH 3/4] Add test for unsupported short exclude flag Signed-off-by: hi-rustin --- tests/testsuite/build.rs | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/tests/testsuite/build.rs b/tests/testsuite/build.rs index b706e4eb6d1..405922412d8 100644 --- a/tests/testsuite/build.rs +++ b/tests/testsuite/build.rs @@ -4328,6 +4328,41 @@ fn build_all_exclude() { .run(); } +#[cargo_test] +fn cargo_build_with_unsupported_short_exclude_flag() { + let p = project() + .file( + "Cargo.toml", + r#" + [package] + name = "foo" + version = "0.1.0" + + [workspace] + members = ["bar", "baz"] + "#, + ) + .file("src/main.rs", "fn main() {}") + .file("bar/Cargo.toml", &basic_manifest("bar", "0.1.0")) + .file("bar/src/lib.rs", "pub fn bar() {}") + .file("baz/Cargo.toml", &basic_manifest("baz", "0.1.0")) + .file("baz/src/lib.rs", "pub fn baz() { break_the_build(); }") + .build(); + + p.cargo("build --workspace -x baz") + .with_stderr( + "\ +error: unexpected argument '-x' found + +Usage: cargo[EXE] build [OPTIONS] + +For more information, try '--help'. +", + ) + .with_status(1) + .run(); +} + #[cargo_test] fn build_all_exclude_not_found() { let p = project() From b514ca5e246c82999c9db7a220f9987aa4f5412a Mon Sep 17 00:00:00 2001 From: hi-rustin Date: Wed, 11 Oct 2023 09:41:05 +0800 Subject: [PATCH 4/4] Add unsupported short flag suggestion for `--exclude` flag Signed-off-by: hi-rustin --- src/cargo/util/command_prelude.rs | 10 ++++++++++ tests/testsuite/build.rs | 2 ++ 2 files changed, 12 insertions(+) diff --git a/src/cargo/util/command_prelude.rs b/src/cargo/util/command_prelude.rs index 01686c8e79e..25176cb22d1 100644 --- a/src/cargo/util/command_prelude.rs +++ b/src/cargo/util/command_prelude.rs @@ -64,9 +64,19 @@ pub trait CommandExt: Sized { all: &'static str, exclude: &'static str, ) -> Self { + let unsupported_short_arg = { + let value_parser = UnknownArgumentValueParser::suggest_arg("--exclude"); + Arg::new("unsupported-short-exclude-flag") + .help("") + .short('x') + .value_parser(value_parser) + .action(ArgAction::SetTrue) + .hide(true) + }; self.arg_package_spec_simple(package) ._arg(flag("workspace", all).help_heading(heading::PACKAGE_SELECTION)) ._arg(multi_opt("exclude", "SPEC", exclude).help_heading(heading::PACKAGE_SELECTION)) + ._arg(unsupported_short_arg) } fn arg_package_spec_simple(self, package: &'static str) -> Self { diff --git a/tests/testsuite/build.rs b/tests/testsuite/build.rs index 405922412d8..fdb8a4d7a4c 100644 --- a/tests/testsuite/build.rs +++ b/tests/testsuite/build.rs @@ -4354,6 +4354,8 @@ fn cargo_build_with_unsupported_short_exclude_flag() { "\ error: unexpected argument '-x' found + tip: a similar argument exists: '--exclude' + Usage: cargo[EXE] build [OPTIONS] For more information, try '--help'.