Skip to content

Commit 4ae21bd

Browse files
committed
Auto merge of #12805 - hi-rustin:rustin-patch-target, r=epage
Add unsupported short flag suggestion for `--target` and `--exclude` flags
2 parents 6fa6fdc + b514ca5 commit 4ae21bd

File tree

2 files changed

+81
-0
lines changed

2 files changed

+81
-0
lines changed

Diff for: src/cargo/util/command_prelude.rs

+20
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,19 @@ pub trait CommandExt: Sized {
6464
all: &'static str,
6565
exclude: &'static str,
6666
) -> Self {
67+
let unsupported_short_arg = {
68+
let value_parser = UnknownArgumentValueParser::suggest_arg("--exclude");
69+
Arg::new("unsupported-short-exclude-flag")
70+
.help("")
71+
.short('x')
72+
.value_parser(value_parser)
73+
.action(ArgAction::SetTrue)
74+
.hide(true)
75+
};
6776
self.arg_package_spec_simple(package)
6877
._arg(flag("workspace", all).help_heading(heading::PACKAGE_SELECTION))
6978
._arg(multi_opt("exclude", "SPEC", exclude).help_heading(heading::PACKAGE_SELECTION))
79+
._arg(unsupported_short_arg)
7080
}
7181

7282
fn arg_package_spec_simple(self, package: &'static str) -> Self {
@@ -232,10 +242,20 @@ pub trait CommandExt: Sized {
232242
}
233243

234244
fn arg_target_triple(self, target: &'static str) -> Self {
245+
let unsupported_short_arg = {
246+
let value_parser = UnknownArgumentValueParser::suggest_arg("--target");
247+
Arg::new("unsupported-short-target-flag")
248+
.help("")
249+
.short('t')
250+
.value_parser(value_parser)
251+
.action(ArgAction::SetTrue)
252+
.hide(true)
253+
};
235254
self._arg(
236255
optional_multi_opt("target", "TRIPLE", target)
237256
.help_heading(heading::COMPILATION_OPTIONS),
238257
)
258+
._arg(unsupported_short_arg)
239259
}
240260

241261
fn arg_target_dir(self) -> Self {

Diff for: tests/testsuite/build.rs

+61
Original file line numberDiff line numberDiff line change
@@ -4239,6 +4239,30 @@ fn cargo_build_empty_target() {
42394239
.run();
42404240
}
42414241

4242+
#[cargo_test]
4243+
fn cargo_build_with_unsupported_short_target_flag() {
4244+
let p = project()
4245+
.file("Cargo.toml", &basic_bin_manifest("foo"))
4246+
.file("src/main.rs", "fn main() {}")
4247+
.build();
4248+
4249+
p.cargo("build -t")
4250+
.arg("")
4251+
.with_stderr(
4252+
"\
4253+
error: unexpected argument '-t' found
4254+
4255+
tip: a similar argument exists: '--target'
4256+
4257+
Usage: cargo[EXE] build [OPTIONS]
4258+
4259+
For more information, try '--help'.
4260+
",
4261+
)
4262+
.with_status(1)
4263+
.run();
4264+
}
4265+
42424266
#[cargo_test]
42434267
fn build_all_workspace() {
42444268
let p = project()
@@ -4304,6 +4328,43 @@ fn build_all_exclude() {
43044328
.run();
43054329
}
43064330

4331+
#[cargo_test]
4332+
fn cargo_build_with_unsupported_short_exclude_flag() {
4333+
let p = project()
4334+
.file(
4335+
"Cargo.toml",
4336+
r#"
4337+
[package]
4338+
name = "foo"
4339+
version = "0.1.0"
4340+
4341+
[workspace]
4342+
members = ["bar", "baz"]
4343+
"#,
4344+
)
4345+
.file("src/main.rs", "fn main() {}")
4346+
.file("bar/Cargo.toml", &basic_manifest("bar", "0.1.0"))
4347+
.file("bar/src/lib.rs", "pub fn bar() {}")
4348+
.file("baz/Cargo.toml", &basic_manifest("baz", "0.1.0"))
4349+
.file("baz/src/lib.rs", "pub fn baz() { break_the_build(); }")
4350+
.build();
4351+
4352+
p.cargo("build --workspace -x baz")
4353+
.with_stderr(
4354+
"\
4355+
error: unexpected argument '-x' found
4356+
4357+
tip: a similar argument exists: '--exclude'
4358+
4359+
Usage: cargo[EXE] build [OPTIONS]
4360+
4361+
For more information, try '--help'.
4362+
",
4363+
)
4364+
.with_status(1)
4365+
.run();
4366+
}
4367+
43074368
#[cargo_test]
43084369
fn build_all_exclude_not_found() {
43094370
let p = project()

0 commit comments

Comments
 (0)