From 95d79094e0df1f59544c803d1bb774643ef14fd1 Mon Sep 17 00:00:00 2001 From: hi-rustin Date: Thu, 12 Oct 2023 09:01:34 +0800 Subject: [PATCH 1/3] Add test for unsupported `--path` 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 fdb8a4d7a4c..5c4c5cbc1d3 100644 --- a/tests/testsuite/build.rs +++ b/tests/testsuite/build.rs @@ -205,6 +205,28 @@ fn cargo_compile_manifest_path() { assert!(p.bin("foo").is_file()); } +#[cargo_test] +fn cargo_compile_with_wrong_manifest_path_flag() { + let p = project() + .file("Cargo.toml", &basic_bin_manifest("foo")) + .file("src/foo.rs", &main_file(r#""i am foo""#, &[])) + .build(); + + p.cargo("build --path foo/Cargo.toml") + .cwd(p.root().parent().unwrap()) + .with_stderr( + "\ +error: unexpected argument '--path' found + +Usage: cargo[EXE] build [OPTIONS] + +For more information, try '--help'. +", + ) + .with_status(1) + .run(); +} + #[cargo_test] fn chdir_gated() { let p = project() From 497e1ee3073860b82cd4e4d71add952392ec97ea Mon Sep 17 00:00:00 2001 From: hi-rustin Date: Thu, 12 Oct 2023 09:04:19 +0800 Subject: [PATCH 2/3] Better suggestion for unsupported `-path` flag Signed-off-by: hi-rustin --- src/bin/cargo/commands/add.rs | 2 +- src/cargo/util/command_prelude.rs | 14 ++++++++++++++ tests/testsuite/build.rs | 2 ++ 3 files changed, 17 insertions(+), 1 deletion(-) diff --git a/src/bin/cargo/commands/add.rs b/src/bin/cargo/commands/add.rs index 344cd2af3f8..e1ece14b811 100644 --- a/src/bin/cargo/commands/add.rs +++ b/src/bin/cargo/commands/add.rs @@ -77,7 +77,7 @@ Example uses: "Ignore `rust-version` specification in packages (unstable)" ), ]) - .arg_manifest_path() + .arg_manifest_path_without_unsupported_path_tip() .arg_package("Package to modify") .arg_dry_run("Don't actually write the manifest") .arg_quiet() diff --git a/src/cargo/util/command_prelude.rs b/src/cargo/util/command_prelude.rs index 25176cb22d1..105691e4353 100644 --- a/src/cargo/util/command_prelude.rs +++ b/src/cargo/util/command_prelude.rs @@ -267,6 +267,20 @@ pub trait CommandExt: Sized { } fn arg_manifest_path(self) -> Self { + // We use `--manifest-path` instead of `--path`. + let unsupported_path_arg = { + let value_parser = UnknownArgumentValueParser::suggest_arg("--manifest-path"); + flag("unsupported-path-flag", "") + .long("path") + .value_parser(value_parser) + .hide(true) + }; + self.arg_manifest_path_without_unsupported_path_tip() + ._arg(unsupported_path_arg) + } + + // `cargo add` has a `--path` flag to install a crate from a local path. + fn arg_manifest_path_without_unsupported_path_tip(self) -> Self { self._arg( opt("manifest-path", "Path to Cargo.toml") .value_name("PATH") diff --git a/tests/testsuite/build.rs b/tests/testsuite/build.rs index 5c4c5cbc1d3..23840ad9a60 100644 --- a/tests/testsuite/build.rs +++ b/tests/testsuite/build.rs @@ -218,6 +218,8 @@ fn cargo_compile_with_wrong_manifest_path_flag() { "\ error: unexpected argument '--path' found + tip: a similar argument exists: '--manifest-path' + Usage: cargo[EXE] build [OPTIONS] For more information, try '--help'. From ded92b17ca619dca333cedb8be18f930f00c9a7a Mon Sep 17 00:00:00 2001 From: hi-rustin Date: Thu, 12 Oct 2023 19:25:45 +0800 Subject: [PATCH 3/3] Call arg_quiet_without_unknown_silent_arg_tip in arg_quiet Signed-off-by: hi-rustin --- src/cargo/util/command_prelude.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cargo/util/command_prelude.rs b/src/cargo/util/command_prelude.rs index 105691e4353..f5b897ea710 100644 --- a/src/cargo/util/command_prelude.rs +++ b/src/cargo/util/command_prelude.rs @@ -372,7 +372,7 @@ pub trait CommandExt: Sized { .value_parser(value_parser) .hide(true) }; - self._arg(flag("quiet", "Do not print cargo log messages").short('q')) + self.arg_quiet_without_unknown_silent_arg_tip() ._arg(unsupported_silent_arg) }