Skip to content

Commit 4d09585

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

File tree

6 files changed

+55
-61
lines changed

6 files changed

+55
-61
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: 21 additions & 1 deletion
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,12 @@ 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 = UnknownArgumentValueParser::suggest(msg);
109+
self._arg(flag("keep-going", "").value_parser(value_parser).hide(true))
110+
}
111+
105112
fn arg_targets_all(
106113
self,
107114
lib: &'static str,
@@ -431,7 +438,7 @@ pub trait ArgMatchesExt {
431438
}
432439

433440
fn keep_going(&self) -> bool {
434-
self.flag("keep-going")
441+
self.maybe_flag("keep-going")
435442
}
436443

437444
fn targets(&self) -> Vec<String> {
@@ -777,6 +784,8 @@ pub trait ArgMatchesExt {
777784

778785
fn flag(&self, name: &str) -> bool;
779786

787+
fn maybe_flag(&self, name: &str) -> bool;
788+
780789
fn _value_of(&self, name: &str) -> Option<&str>;
781790

782791
fn _values_of(&self, name: &str) -> Vec<String>;
@@ -797,6 +806,17 @@ impl<'a> ArgMatchesExt for ArgMatches {
797806
.unwrap_or(false)
798807
}
799808

809+
// This works around before an upstream fix in clap for `UnknownArgumentValueParser` accepting
810+
// generics arguments. `flag()` cannot be used with `--keep-going` at this moment due to
811+
// <https://github.com/clap-rs/clap/issues/5081>.
812+
fn maybe_flag(&self, name: &str) -> bool {
813+
self.try_get_one::<bool>(name)
814+
.ok()
815+
.flatten()
816+
.copied()
817+
.unwrap_or_default()
818+
}
819+
800820
fn _value_of(&self, name: &str) -> Option<&str> {
801821
ignore_unknown(self.try_get_one::<String>(name)).map(String::as_str)
802822
}

tests/testsuite/bench.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1681,11 +1681,16 @@ fn cargo_bench_no_keep_going() {
16811681
p.cargo("bench --keep-going")
16821682
.with_stderr(
16831683
"\
1684-
error: unexpected argument `--keep-going` found
1684+
error: unexpected argument '--keep-going' found
16851685
1686-
tip: to run as many benchmarks as possible without failing fast, use `--no-fail-fast`",
1686+
tip: use `--no-fail-fast` to run as many tests as possible regardless of failure
1687+
1688+
Usage: cargo bench [OPTIONS] [BENCHNAME] [-- [args]...]
1689+
1690+
For more information, try '--help'.
1691+
",
16871692
)
1688-
.with_status(101)
1693+
.with_status(1)
16891694
.run();
16901695
}
16911696

tests/testsuite/test.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4854,11 +4854,16 @@ fn cargo_test_no_keep_going() {
48544854
p.cargo("test --keep-going")
48554855
.with_stderr(
48564856
"\
4857-
error: unexpected argument `--keep-going` found
4857+
error: unexpected argument '--keep-going' found
48584858
4859-
tip: to run as many tests as possible without failing fast, use `--no-fail-fast`",
4859+
tip: use `--no-fail-fast` to run as many tests as possible regardless of failure
4860+
4861+
Usage: cargo test [OPTIONS] [TESTNAME] [-- [args]...]
4862+
4863+
For more information, try '--help'.
4864+
",
48604865
)
4861-
.with_status(101)
4866+
.with_status(1)
48624867
.run();
48634868
}
48644869

0 commit comments

Comments
 (0)