Skip to content

Commit 0d3f77f

Browse files
committed
Auto merge of #12811 - hi-rustin:rustin-patch-path, r=epage
Better suggestion for unsupported `--path` flag
2 parents e6b24be + ded92b1 commit 0d3f77f

File tree

3 files changed

+40
-2
lines changed

3 files changed

+40
-2
lines changed

src/bin/cargo/commands/add.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ Example uses:
7777
"Ignore `rust-version` specification in packages (unstable)"
7878
),
7979
])
80-
.arg_manifest_path()
80+
.arg_manifest_path_without_unsupported_path_tip()
8181
.arg_package("Package to modify")
8282
.arg_dry_run("Don't actually write the manifest")
8383
.arg_quiet()

src/cargo/util/command_prelude.rs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,20 @@ pub trait CommandExt: Sized {
267267
}
268268

269269
fn arg_manifest_path(self) -> Self {
270+
// We use `--manifest-path` instead of `--path`.
271+
let unsupported_path_arg = {
272+
let value_parser = UnknownArgumentValueParser::suggest_arg("--manifest-path");
273+
flag("unsupported-path-flag", "")
274+
.long("path")
275+
.value_parser(value_parser)
276+
.hide(true)
277+
};
278+
self.arg_manifest_path_without_unsupported_path_tip()
279+
._arg(unsupported_path_arg)
280+
}
281+
282+
// `cargo add` has a `--path` flag to install a crate from a local path.
283+
fn arg_manifest_path_without_unsupported_path_tip(self) -> Self {
270284
self._arg(
271285
opt("manifest-path", "Path to Cargo.toml")
272286
.value_name("PATH")
@@ -358,7 +372,7 @@ pub trait CommandExt: Sized {
358372
.value_parser(value_parser)
359373
.hide(true)
360374
};
361-
self._arg(flag("quiet", "Do not print cargo log messages").short('q'))
375+
self.arg_quiet_without_unknown_silent_arg_tip()
362376
._arg(unsupported_silent_arg)
363377
}
364378

tests/testsuite/build.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,30 @@ fn cargo_compile_manifest_path() {
205205
assert!(p.bin("foo").is_file());
206206
}
207207

208+
#[cargo_test]
209+
fn cargo_compile_with_wrong_manifest_path_flag() {
210+
let p = project()
211+
.file("Cargo.toml", &basic_bin_manifest("foo"))
212+
.file("src/foo.rs", &main_file(r#""i am foo""#, &[]))
213+
.build();
214+
215+
p.cargo("build --path foo/Cargo.toml")
216+
.cwd(p.root().parent().unwrap())
217+
.with_stderr(
218+
"\
219+
error: unexpected argument '--path' found
220+
221+
tip: a similar argument exists: '--manifest-path'
222+
223+
Usage: cargo[EXE] build [OPTIONS]
224+
225+
For more information, try '--help'.
226+
",
227+
)
228+
.with_status(1)
229+
.run();
230+
}
231+
208232
#[cargo_test]
209233
fn chdir_gated() {
210234
let p = project()

0 commit comments

Comments
 (0)