Skip to content

Commit 50ba4bf

Browse files
committed
Auto merge of #12529 - weihanglo:clap-suggestions, r=epage
refactor: Use clap to suggest alternative argument for unsupported arguments
2 parents 4df5a17 + 77da514 commit 50ba4bf

30 files changed

+119
-111
lines changed

Cargo.lock

+4-4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ cargo-test-macro = { path = "crates/cargo-test-macro" }
2828
cargo-test-support = { path = "crates/cargo-test-support" }
2929
cargo-util = { version = "0.2.6", path = "crates/cargo-util" }
3030
cargo_metadata = "0.14.0"
31-
clap = "4.3.19"
31+
clap = "4.3.23"
3232
core-foundation = { version = "0.9.3", features = ["mac_os_10_7_support"] }
3333
crates-io = { version = "0.38.0", path = "crates/crates-io" }
3434
criterion = { version = "0.5.1", features = ["html_reports"] }

src/bin/cargo/commands/bench.rs

+2-12
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ pub fn cli() -> Command {
4242
"Benchmark all targets",
4343
)
4444
.arg_features()
45-
.arg_jobs_without_keep_going()
46-
.arg(flag("keep-going", "Use `--no-fail-fast` instead").hide(true)) // See rust-lang/cargo#11702
45+
.arg_jobs()
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/build.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ pub fn cli() -> Command {
3131
.arg_features()
3232
.arg_release("Build artifacts in release mode, with optimizations")
3333
.arg_profile("Build artifacts with the specified profile")
34-
.arg_jobs()
34+
.arg_parallel()
3535
.arg_target_triple("Build for the target triple")
3636
.arg_target_dir()
3737
.arg(

src/bin/cargo/commands/check.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ pub fn cli() -> Command {
2929
"Check all targets",
3030
)
3131
.arg_features()
32-
.arg_jobs()
32+
.arg_parallel()
3333
.arg_release("Check artifacts in release mode, with optimizations")
3434
.arg_profile("Check artifacts with the specified profile")
3535
.arg_target_triple("Check for the target triple")

src/bin/cargo/commands/doc.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ pub fn cli() -> Command {
3232
"Document only the specified example",
3333
"Document all examples",
3434
)
35-
.arg_jobs()
35+
.arg_parallel()
3636
.arg_release("Build artifacts in release mode, with optimizations")
3737
.arg_profile("Build artifacts with the specified profile")
3838
.arg_target_triple("Build for the target triple")

src/bin/cargo/commands/fix.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ pub fn cli() -> Command {
4747
"Fix all targets (default)",
4848
)
4949
.arg_features()
50-
.arg_jobs()
50+
.arg_parallel()
5151
.arg_release("Fix artifacts in release mode, with optimizations")
5252
.arg_profile("Build artifacts with the specified profile")
5353
.arg_target_triple("Fix for the target triple")

src/bin/cargo/commands/install.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ pub fn cli() -> Command {
7575
"Install all examples",
7676
)
7777
.arg_features()
78-
.arg_jobs()
78+
.arg_parallel()
7979
.arg(flag(
8080
"debug",
8181
"Build in debug mode (with the 'dev' profile) instead of release mode",

src/bin/cargo/commands/package.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ pub fn cli() -> Command {
3333
.arg_features()
3434
.arg_target_triple("Build for the target triple")
3535
.arg_target_dir()
36-
.arg_jobs()
36+
.arg_parallel()
3737
.arg_manifest_path()
3838
.after_help("Run `cargo help package` for more detailed information.\n")
3939
}

src/bin/cargo/commands/publish.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ pub fn cli() -> Command {
2020
.arg_quiet()
2121
.arg_package("Package to publish")
2222
.arg_features()
23-
.arg_jobs()
23+
.arg_parallel()
2424
.arg_target_triple("Build for the target triple")
2525
.arg_target_dir()
2626
.arg_manifest_path()

src/bin/cargo/commands/run.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ pub fn cli() -> Command {
3030
"Name of the example target to run",
3131
)
3232
.arg_features()
33-
.arg_jobs()
33+
.arg_parallel()
3434
.arg_release("Build artifacts in release mode, with optimizations")
3535
.arg_profile("Build artifacts with the specified profile")
3636
.arg_target_triple("Build for the target triple")

src/bin/cargo/commands/rustc.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ pub fn cli() -> Command {
4444
"Build all targets",
4545
)
4646
.arg_features()
47-
.arg_jobs()
47+
.arg_parallel()
4848
.arg_release("Build artifacts in release mode, with optimizations")
4949
.arg_profile("Build artifacts with the specified profile")
5050
.arg_target_triple("Target triple which compiles will be for")

src/bin/cargo/commands/rustdoc.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ pub fn cli() -> Command {
3232
"Build all targets",
3333
)
3434
.arg_features()
35-
.arg_jobs()
35+
.arg_parallel()
3636
.arg_release("Build artifacts in release mode, with optimizations")
3737
.arg_profile("Build artifacts with the specified profile")
3838
.arg_target_triple("Build for the target triple")

src/bin/cargo/commands/test.rs

+2-12
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ pub fn cli() -> Command {
4848
"Test all targets (does not include doctests)",
4949
)
5050
.arg_features()
51-
.arg_jobs_without_keep_going()
52-
.arg(flag("keep-going", "Use `--no-fail-fast` instead").hide(true)) // See rust-lang/cargo#11702
51+
.arg_jobs()
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

+16-32
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

+24-4
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;
@@ -82,8 +83,8 @@ pub trait CommandExt: Sized {
8283
)
8384
}
8485

85-
fn arg_jobs(self) -> Self {
86-
self.arg_jobs_without_keep_going()._arg(
86+
fn arg_parallel(self) -> Self {
87+
self.arg_jobs()._arg(
8788
flag(
8889
"keep-going",
8990
"Do not abort the build as soon as there is an error (unstable)",
@@ -92,7 +93,7 @@ pub trait CommandExt: Sized {
9293
)
9394
}
9495

95-
fn arg_jobs_without_keep_going(self) -> Self {
96+
fn arg_jobs(self) -> Self {
9697
self._arg(
9798
opt("jobs", "Number of parallel jobs, defaults to # of CPUs.")
9899
.short('j')
@@ -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

-18
Original file line numberDiff line numberDiff line change
@@ -1671,24 +1671,6 @@ fn json_artifact_includes_executable_for_benchmark() {
16711671
.run();
16721672
}
16731673

1674-
#[cargo_test]
1675-
fn cargo_bench_no_keep_going() {
1676-
let p = project()
1677-
.file("Cargo.toml", &basic_bin_manifest("foo"))
1678-
.file("src/main.rs", "")
1679-
.build();
1680-
1681-
p.cargo("bench --keep-going")
1682-
.with_stderr(
1683-
"\
1684-
error: unexpected argument `--keep-going` found
1685-
1686-
tip: to run as many benchmarks as possible without failing fast, use `--no-fail-fast`",
1687-
)
1688-
.with_status(101)
1689-
.run();
1690-
}
1691-
16921674
#[cargo_test(nightly, reason = "bench")]
16931675
fn cargo_bench_print_env_verbose() {
16941676
let p = project()

tests/testsuite/cargo_bench/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
mod help;
2+
mod no_keep_going;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[package]
2+
name = "foo"
3+
version = "0.1.0"

tests/testsuite/cargo_bench/no_keep_going/in/src/lib.rs

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
use cargo_test_support::curr_dir;
2+
use cargo_test_support::CargoCommand;
3+
use cargo_test_support::Project;
4+
5+
#[cargo_test]
6+
fn case() {
7+
let project = Project::from_template(curr_dir!().join("in"));
8+
let project_root = project.root();
9+
let cwd = &project_root;
10+
11+
snapbox::cmd::Command::cargo_ui()
12+
.arg("bench")
13+
.arg("--keep-going")
14+
.current_dir(cwd)
15+
.assert()
16+
.code(1)
17+
.stdout_matches_path(curr_dir!().join("stdout.log"))
18+
.stderr_matches_path(curr_dir!().join("stderr.log"));
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
error: unexpected argument '--keep-going' found
2+
3+
tip: use `--no-fail-fast` to run as many tests as possible regardless of failure
4+
5+
Usage: cargo[EXE] bench [OPTIONS] [BENCHNAME] [-- [args]...]
6+
7+
For more information, try '--help'.

tests/testsuite/cargo_bench/no_keep_going/stdout.log

Whitespace-only changes.

tests/testsuite/cargo_test/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
mod help;
2+
mod no_keep_going;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[package]
2+
name = "foo"
3+
version = "0.1.0"

tests/testsuite/cargo_test/no_keep_going/in/src/lib.rs

Whitespace-only changes.

0 commit comments

Comments
 (0)