Skip to content

Commit 5bf83d8

Browse files
committed
Auto merge of #12693 - hi-rustin:rustin-patch-unsupported-flags, r=epage
Better suggestion for unsupported mode in build command
2 parents da73cc6 + 97656fb commit 5bf83d8

File tree

5 files changed

+62
-2
lines changed

5 files changed

+62
-2
lines changed

src/bin/cargo/commands/build.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ pub fn cli() -> Command {
3030
)
3131
.arg_features()
3232
.arg_release("Build artifacts in release mode, with optimizations")
33+
.arg_redundant_default_mode("debug", "build", "release")
3334
.arg_profile("Build artifacts with the specified profile")
3435
.arg_parallel()
3536
.arg_target_triple("Build for the target triple")

src/bin/cargo/commands/install.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ pub fn cli() -> Command {
8383
"debug",
8484
"Build in debug mode (with the 'dev' profile) instead of release mode",
8585
))
86+
.arg_redundant_default_mode("release", "install", "debug")
8687
.arg_profile("Install artifacts with the specified profile")
8788
.arg_target_triple("Build for the target triple")
8889
.arg_target_dir()

src/cargo/util/command_prelude.rs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,17 @@ pub trait CommandExt: Sized {
112112
self._arg(flag("keep-going", "").value_parser(value_parser).hide(true))
113113
}
114114

115+
fn arg_redundant_default_mode(
116+
self,
117+
default_mode: &'static str,
118+
command: &'static str,
119+
supported_mode: &'static str,
120+
) -> Self {
121+
let msg = format!("`--{default_mode}` is the default for `cargo {command}`; instead `--{supported_mode}` is supported");
122+
let value_parser = UnknownArgumentValueParser::suggest(msg);
123+
self._arg(flag(default_mode, "").value_parser(value_parser).hide(true))
124+
}
125+
115126
fn arg_targets_all(
116127
self,
117128
lib: &'static str,
@@ -486,7 +497,7 @@ Run `{cmd}` to see possible targets."
486497
(Some(name @ ("dev" | "test" | "bench" | "check")), ProfileChecking::LegacyRustc)
487498
// `cargo fix` and `cargo check` has legacy handling of this profile name
488499
| (Some(name @ "test"), ProfileChecking::LegacyTestOnly) => {
489-
if self.flag("release") {
500+
if self.maybe_flag("release") {
490501
config.shell().warn(
491502
"the `--release` flag should not be specified with the `--profile` flag\n\
492503
The `--release` flag will be ignored.\n\
@@ -510,7 +521,11 @@ Run `{cmd}` to see possible targets."
510521
)
511522
};
512523

513-
let name = match (self.flag("release"), self.flag("debug"), specified_profile) {
524+
let name = match (
525+
self.maybe_flag("release"),
526+
self.maybe_flag("debug"),
527+
specified_profile,
528+
) {
514529
(false, false, None) => default,
515530
(true, _, None | Some("release")) => "release",
516531
(true, _, Some(name)) => return Err(conflict("release", "release", name)),

tests/testsuite/build.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,29 @@ fn incremental_config() {
135135
.run();
136136
}
137137

138+
#[cargo_test]
139+
fn cargo_compile_with_redundant_default_mode() {
140+
let p = project()
141+
.file("Cargo.toml", &basic_bin_manifest("foo"))
142+
.file("src/foo.rs", &main_file(r#""i am foo""#, &[]))
143+
.build();
144+
145+
p.cargo("build --debug")
146+
.with_stderr(
147+
"\
148+
error: unexpected argument '--debug' found
149+
150+
tip: `--debug` is the default for `cargo build`; instead `--release` is supported
151+
152+
Usage: cargo[EXE] build [OPTIONS]
153+
154+
For more information, try '--help'.
155+
",
156+
)
157+
.with_status(1)
158+
.run();
159+
}
160+
138161
#[cargo_test]
139162
fn cargo_compile_with_workspace_excluded() {
140163
let p = project().file("src/main.rs", "fn main() {}").build();

tests/testsuite/install.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2443,3 +2443,23 @@ fn ambiguous_registry_vs_local_package() {
24432443
.run();
24442444
assert_has_installed_exe(cargo_home(), "foo");
24452445
}
2446+
2447+
#[cargo_test]
2448+
fn install_with_redundant_default_mode() {
2449+
pkg("foo", "0.0.1");
2450+
2451+
cargo_process("install foo --release")
2452+
.with_stderr(
2453+
"\
2454+
error: unexpected argument '--release' found
2455+
2456+
tip: `--release` is the default for `cargo install`; instead `--debug` is supported
2457+
2458+
Usage: cargo[EXE] install [OPTIONS] [crate]...
2459+
2460+
For more information, try '--help'.
2461+
",
2462+
)
2463+
.with_status(1)
2464+
.run();
2465+
}

0 commit comments

Comments
 (0)