Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions src/cargo/util/command_prelude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -232,10 +242,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 {
Expand Down
61 changes: 61 additions & 0 deletions tests/testsuite/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4239,6 +4239,30 @@ 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

tip: a similar argument exists: '--target'

Usage: cargo[EXE] build [OPTIONS]

For more information, try '--help'.
",
)
.with_status(1)
.run();
}

#[cargo_test]
fn build_all_workspace() {
let p = project()
Expand Down Expand Up @@ -4304,6 +4328,43 @@ 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

tip: a similar argument exists: '--exclude'

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()
Expand Down