Skip to content

Commit

Permalink
refactor(arg): use UnknownArgumentValueParser to suggest arg for un…
Browse files Browse the repository at this point in the history
…known args
  • Loading branch information
weihanglo committed Aug 19, 2023
1 parent ab31582 commit 91a2faa
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 54 deletions.
12 changes: 1 addition & 11 deletions src/bin/cargo/commands/bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ pub fn cli() -> Command {
)
.arg_features()
.arg_jobs()
.arg(flag("keep-going", "Use `--no-fail-fast` instead").hide(true)) // See rust-lang/cargo#11702
.arg_unsupported_keep_going()
.arg_profile("Build artifacts with the specified profile")
.arg_target_triple("Build for the target triple")
.arg_target_dir()
Expand All @@ -56,16 +56,6 @@ pub fn cli() -> Command {
pub fn exec(config: &mut Config, args: &ArgMatches) -> CliResult {
let ws = args.workspace(config)?;

if args.keep_going() {
return Err(anyhow::format_err!(
"\
unexpected argument `--keep-going` found
tip: to run as many benchmarks as possible without failing fast, use `--no-fail-fast`"
)
.into());
}

let mut compile_opts = args.compile_options(
config,
CompileMode::Bench,
Expand Down
12 changes: 1 addition & 11 deletions src/bin/cargo/commands/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ pub fn cli() -> Command {
)
.arg_features()
.arg_jobs()
.arg(flag("keep-going", "Use `--no-fail-fast` instead").hide(true)) // See rust-lang/cargo#11702
.arg_unsupported_keep_going()
.arg_release("Build artifacts in release mode, with optimizations")
.arg_profile("Build artifacts with the specified profile")
.arg_target_triple("Build for the target triple")
Expand All @@ -66,16 +66,6 @@ pub fn cli() -> Command {
pub fn exec(config: &mut Config, args: &ArgMatches) -> CliResult {
let ws = args.workspace(config)?;

if args.keep_going() {
return Err(anyhow::format_err!(
"\
unexpected argument `--keep-going` found
tip: to run as many tests as possible without failing fast, use `--no-fail-fast`"
)
.into());
}

let mut compile_opts = args.compile_options(
config,
CompileMode::Test,
Expand Down
48 changes: 16 additions & 32 deletions src/bin/cargo/commands/vendor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,27 @@ pub fn cli() -> Command {
"versioned-dirs",
"Always include version in subdir name",
))
.arg(flag("no-merge-sources", "Not supported").hide(true))
.arg(flag("relative-path", "Not supported").hide(true))
.arg(flag("only-git-deps", "Not supported").hide(true))
.arg(flag("disallow-duplicates", "Not supported").hide(true))
.arg(unsupported("no-merge-sources"))
.arg(unsupported("relative-path"))
.arg(unsupported("only-git-deps"))
.arg(unsupported("disallow-duplicates"))
.arg_quiet()
.arg_manifest_path()
.after_help("Run `cargo help vendor` for more detailed information.\n")
}

fn unsupported(name: &'static str) -> Arg {
// When we moved `cargo vendor` into Cargo itself we didn't stabilize a few
// flags, so try to provide a helpful error message in that case to ensure
// that users currently using the flag aren't tripped up.
let value_parser = clap::builder::UnknownArgumentValueParser::suggest("the crates.io `cargo vendor` command has been merged into Cargo")
.and_suggest(format!("and the flag `--{name}` isn't supported currently"))
.and_suggest("to continue using the flag, execute `cargo-vendor vendor ...`")
.and_suggest("to suggest this flag supported in Cargo, file an issue at <https://github.com/rust-lang/cargo/issues/new>");

flag(name, "").value_parser(value_parser).hide(true)
}

pub fn exec(config: &mut Config, args: &ArgMatches) -> CliResult {
// We're doing the vendoring operation ourselves, so we don't actually want
// to respect any of the `source` configuration in Cargo itself. That's
Expand All @@ -50,34 +62,6 @@ pub fn exec(config: &mut Config, args: &ArgMatches) -> CliResult {
config.values_mut()?.remove("source");
}

// When we moved `cargo vendor` into Cargo itself we didn't stabilize a few
// flags, so try to provide a helpful error message in that case to ensure
// that users currently using the flag aren't tripped up.
let crates_io_cargo_vendor_flag = if args.flag("no-merge-sources") {
Some("--no-merge-sources")
} else if args.flag("relative-path") {
Some("--relative-path")
} else if args.flag("only-git-deps") {
Some("--only-git-deps")
} else if args.flag("disallow-duplicates") {
Some("--disallow-duplicates")
} else {
None
};
if let Some(flag) = crates_io_cargo_vendor_flag {
return Err(anyhow::format_err!(
"\
the crates.io `cargo vendor` command has now been merged into Cargo itself
and does not support the flag `{}` currently; to continue using the flag you
can execute `cargo-vendor vendor ...`, and if you would like to see this flag
supported in Cargo itself please feel free to file an issue at
https://github.com/rust-lang/cargo/issues/new
",
flag
)
.into());
}

let ws = args.workspace(config)?;
let path = args
.get_one::<PathBuf>("path")
Expand Down
7 changes: 7 additions & 0 deletions src/cargo/util/command_prelude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use crate::util::{
use crate::CargoResult;
use anyhow::bail;
use cargo_util::paths;
use clap::builder::UnknownArgumentValueParser;
use std::ffi::{OsStr, OsString};
use std::path::Path;
use std::path::PathBuf;
Expand Down Expand Up @@ -102,6 +103,12 @@ pub trait CommandExt: Sized {
)
}

fn arg_unsupported_keep_going(self) -> Self {
let msg = "use `--no-fail-fast` to run as many tests as possible regardless of failure";
let value_parser = UnknownArgumentValueParser::suggest_arg("--no-fail-fast").and_suggest(msg);
self._arg(flag("keep-going", "").value_parser(value_parser).hide(true))
}

fn arg_targets_all(
self,
lib: &'static str,
Expand Down

0 comments on commit 91a2faa

Please sign in to comment.