Skip to content

Commit 4f1f342

Browse files
committed
refactor(arg): use UnknownArgumentValueParser to suggest arg for unknown args
1 parent ab31582 commit 4f1f342

File tree

4 files changed

+26
-54
lines changed

4 files changed

+26
-54
lines changed

src/bin/cargo/commands/bench.rs

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ pub fn cli() -> Command {
4343
)
4444
.arg_features()
4545
.arg_jobs()
46-
.arg(flag("keep-going", "Use `--no-fail-fast` instead").hide(true)) // See rust-lang/cargo#11702
46+
.arg_unsupported_keep_going()
4747
.arg_profile("Build artifacts with the specified profile")
4848
.arg_target_triple("Build for the target triple")
4949
.arg_target_dir()
@@ -56,16 +56,6 @@ pub fn cli() -> Command {
5656
pub fn exec(config: &mut Config, args: &ArgMatches) -> CliResult {
5757
let ws = args.workspace(config)?;
5858

59-
if args.keep_going() {
60-
return Err(anyhow::format_err!(
61-
"\
62-
unexpected argument `--keep-going` found
63-
64-
tip: to run as many benchmarks as possible without failing fast, use `--no-fail-fast`"
65-
)
66-
.into());
67-
}
68-
6959
let mut compile_opts = args.compile_options(
7060
config,
7161
CompileMode::Bench,

src/bin/cargo/commands/test.rs

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ pub fn cli() -> Command {
4949
)
5050
.arg_features()
5151
.arg_jobs()
52-
.arg(flag("keep-going", "Use `--no-fail-fast` instead").hide(true)) // See rust-lang/cargo#11702
52+
.arg_unsupported_keep_going()
5353
.arg_release("Build artifacts in release mode, with optimizations")
5454
.arg_profile("Build artifacts with the specified profile")
5555
.arg_target_triple("Build for the target triple")
@@ -66,16 +66,6 @@ pub fn cli() -> Command {
6666
pub fn exec(config: &mut Config, args: &ArgMatches) -> CliResult {
6767
let ws = args.workspace(config)?;
6868

69-
if args.keep_going() {
70-
return Err(anyhow::format_err!(
71-
"\
72-
unexpected argument `--keep-going` found
73-
74-
tip: to run as many tests as possible without failing fast, use `--no-fail-fast`"
75-
)
76-
.into());
77-
}
78-
7969
let mut compile_opts = args.compile_options(
8070
config,
8171
CompileMode::Test,

src/bin/cargo/commands/vendor.rs

Lines changed: 16 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,27 @@ pub fn cli() -> Command {
3232
"versioned-dirs",
3333
"Always include version in subdir name",
3434
))
35-
.arg(flag("no-merge-sources", "Not supported").hide(true))
36-
.arg(flag("relative-path", "Not supported").hide(true))
37-
.arg(flag("only-git-deps", "Not supported").hide(true))
38-
.arg(flag("disallow-duplicates", "Not supported").hide(true))
35+
.arg(unsupported("no-merge-sources"))
36+
.arg(unsupported("relative-path"))
37+
.arg(unsupported("only-git-deps"))
38+
.arg(unsupported("disallow-duplicates"))
3939
.arg_quiet()
4040
.arg_manifest_path()
4141
.after_help("Run `cargo help vendor` for more detailed information.\n")
4242
}
4343

44+
fn unsupported(name: &'static str) -> Arg {
45+
// When we moved `cargo vendor` into Cargo itself we didn't stabilize a few
46+
// flags, so try to provide a helpful error message in that case to ensure
47+
// that users currently using the flag aren't tripped up.
48+
let value_parser = clap::builder::UnknownArgumentValueParser::suggest("the crates.io `cargo vendor` command has been merged into Cargo")
49+
.and_suggest(format!("and the flag `--{name}` isn't supported currently"))
50+
.and_suggest("to continue using the flag, execute `cargo-vendor vendor ...`")
51+
.and_suggest("to suggest this flag supported in Cargo, file an issue at <https://github.com/rust-lang/cargo/issues/new>");
52+
53+
flag(name, "").value_parser(value_parser).hide(true)
54+
}
55+
4456
pub fn exec(config: &mut Config, args: &ArgMatches) -> CliResult {
4557
// We're doing the vendoring operation ourselves, so we don't actually want
4658
// to respect any of the `source` configuration in Cargo itself. That's
@@ -50,34 +62,6 @@ pub fn exec(config: &mut Config, args: &ArgMatches) -> CliResult {
5062
config.values_mut()?.remove("source");
5163
}
5264

53-
// When we moved `cargo vendor` into Cargo itself we didn't stabilize a few
54-
// flags, so try to provide a helpful error message in that case to ensure
55-
// that users currently using the flag aren't tripped up.
56-
let crates_io_cargo_vendor_flag = if args.flag("no-merge-sources") {
57-
Some("--no-merge-sources")
58-
} else if args.flag("relative-path") {
59-
Some("--relative-path")
60-
} else if args.flag("only-git-deps") {
61-
Some("--only-git-deps")
62-
} else if args.flag("disallow-duplicates") {
63-
Some("--disallow-duplicates")
64-
} else {
65-
None
66-
};
67-
if let Some(flag) = crates_io_cargo_vendor_flag {
68-
return Err(anyhow::format_err!(
69-
"\
70-
the crates.io `cargo vendor` command has now been merged into Cargo itself
71-
and does not support the flag `{}` currently; to continue using the flag you
72-
can execute `cargo-vendor vendor ...`, and if you would like to see this flag
73-
supported in Cargo itself please feel free to file an issue at
74-
https://github.com/rust-lang/cargo/issues/new
75-
",
76-
flag
77-
)
78-
.into());
79-
}
80-
8165
let ws = args.workspace(config)?;
8266
let path = args
8367
.get_one::<PathBuf>("path")

src/cargo/util/command_prelude.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ use crate::util::{
1414
use crate::CargoResult;
1515
use anyhow::bail;
1616
use cargo_util::paths;
17+
use clap::builder::UnknownArgumentValueParser;
1718
use std::ffi::{OsStr, OsString};
1819
use std::path::Path;
1920
use std::path::PathBuf;
@@ -102,6 +103,13 @@ pub trait CommandExt: Sized {
102103
)
103104
}
104105

106+
fn arg_unsupported_keep_going(self) -> Self {
107+
let msg = "use `--no-fail-fast` to run as many tests as possible regardless of failure";
108+
let value_parser =
109+
UnknownArgumentValueParser::suggest_arg("--no-fail-fast").and_suggest(msg);
110+
self._arg(flag("keep-going", "").value_parser(value_parser).hide(true))
111+
}
112+
105113
fn arg_targets_all(
106114
self,
107115
lib: &'static str,

0 commit comments

Comments
 (0)