Skip to content

Commit ed86337

Browse files
committed
Better suggestion for unsupported mode in build command
1 parent f262297 commit ed86337

File tree

3 files changed

+22
-2
lines changed

3 files changed

+22
-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_unsupported_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/cargo/util/command_prelude.rs

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

115+
fn arg_unsupported_mode(
116+
self,
117+
want: &'static str,
118+
command: &'static str,
119+
actual: &'static str,
120+
) -> Self {
121+
let msg = format!(
122+
"There is no `--{want}` for `cargo {command}`. Only `--{actual}` is supported."
123+
);
124+
let value_parser = UnknownArgumentValueParser::suggest(msg);
125+
self._arg(flag(want, "").value_parser(value_parser).hide(true))
126+
}
127+
115128
fn arg_targets_all(
116129
self,
117130
lib: &'static str,
@@ -486,7 +499,7 @@ Run `{cmd}` to see possible targets."
486499
(Some(name @ ("dev" | "test" | "bench" | "check")), ProfileChecking::LegacyRustc)
487500
// `cargo fix` and `cargo check` has legacy handling of this profile name
488501
| (Some(name @ "test"), ProfileChecking::LegacyTestOnly) => {
489-
if self.flag("release") {
502+
if self.maybe_flag("release") {
490503
config.shell().warn(
491504
"the `--release` flag should not be specified with the `--profile` flag\n\
492505
The `--release` flag will be ignored.\n\
@@ -510,7 +523,11 @@ Run `{cmd}` to see possible targets."
510523
)
511524
};
512525

513-
let name = match (self.flag("release"), self.flag("debug"), specified_profile) {
526+
let name = match (
527+
self.maybe_flag("release"),
528+
self.flag("debug"),
529+
specified_profile,
530+
) {
514531
(false, false, None) => default,
515532
(true, _, None | Some("release")) => "release",
516533
(true, _, Some(name)) => return Err(conflict("release", "release", name)),

tests/testsuite/build.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,8 @@ fn cargo_compile_with_unsupported_mode() {
147147
"\
148148
error: unexpected argument '--debug' found
149149
150+
tip: There is no `--debug` for `cargo build`. Only `--release` is supported.
151+
150152
Usage: cargo[EXE] build [OPTIONS]
151153
152154
For more information, try '--help'.

0 commit comments

Comments
 (0)