refactor(cli): Upgrade to clap v4

This commit is contained in:
Ed Page 2022-09-20 13:19:46 -05:00
parent a3e352e4b8
commit 96948f7a24
35 changed files with 105 additions and 114 deletions

View File

@ -62,7 +62,7 @@ toml_edit = { version = "0.14.3", features = ["serde", "easy", "perf"] }
unicode-xid = "0.2.0"
url = "2.2.2"
walkdir = "2.2"
clap = "3.2.18"
clap = "4.0.4"
unicode-width = "0.1.5"
openssl = { version = '0.10.11', optional = true }
im-rc = "15.0.0"

View File

@ -2,7 +2,7 @@ use anyhow::anyhow;
use cargo::core::shell::Shell;
use cargo::core::{features, CliUnstable};
use cargo::{self, drop_print, drop_println, CliResult, Config};
use clap::{AppSettings, Arg, ArgMatches};
use clap::{Arg, ArgMatches};
use itertools::Itertools;
use std::collections::HashMap;
use std::ffi::OsStr;
@ -69,7 +69,7 @@ Available unstable (nightly-only) flags:
{}
Run with 'cargo -Z [FLAG] [SUBCOMMAND]'",
Run with 'cargo -Z [FLAG] [COMMAND]'",
joined
);
if !config.nightly_features_allowed {
@ -406,14 +406,12 @@ impl GlobalArgs {
pub fn cli() -> Command {
let is_rustup = std::env::var_os("RUSTUP_HOME").is_some();
let usage = if is_rustup {
"cargo [+toolchain] [OPTIONS] [SUBCOMMAND]"
"cargo [+toolchain] [OPTIONS] [COMMAND]"
} else {
"cargo [OPTIONS] [SUBCOMMAND]"
"cargo [OPTIONS] [COMMAND]"
};
Command::new("cargo")
.allow_external_subcommands(true)
.allow_invalid_utf8_for_external_subcommands(true)
.setting(AppSettings::DeriveDisplayOrder)
// Doesn't mix well with our list of common cargo commands. See clap-rs/clap#3108 for
// opening clap up to allow us to style our help template
.disable_colored_help(true)
@ -424,10 +422,9 @@ pub fn cli() -> Command {
"\
Rust's package manager
USAGE:
{usage}
Usage: {usage}
OPTIONS:
Options:
{options}
Some common cargo commands are (see all commands with --list):

View File

@ -14,21 +14,19 @@ use cargo::CargoResult;
pub fn cli() -> Command {
clap::Command::new("add")
.setting(clap::AppSettings::DeriveDisplayOrder)
.about("Add dependencies to a Cargo.toml manifest file")
.override_usage(
"\
cargo add [OPTIONS] <DEP>[@<VERSION>] ...
cargo add [OPTIONS] --path <PATH> ...
cargo add [OPTIONS] --git <URL> ..."
cargo add [OPTIONS] <DEP>[@<VERSION>] ...
cargo add [OPTIONS] --path <PATH> ...
cargo add [OPTIONS] --git <URL> ..."
)
.after_help("Run `cargo help add` for more detailed information.\n")
.group(clap::ArgGroup::new("selected").multiple(true).required(true))
.args([
clap::Arg::new("crates")
.takes_value(true)
.value_name("DEP_ID")
.multiple_values(true)
.num_args(0..)
.help("Reference to a package to add as a dependency")
.long_help(
"Reference to a package to add as a dependency
@ -46,7 +44,6 @@ You can reference a package by:
clap::Arg::new("features")
.short('F')
.long("features")
.takes_value(true)
.value_name("FEATURES")
.action(ArgAction::Append)
.help("Space or comma separated list of features to activate"),
@ -65,7 +62,7 @@ The package will be removed from your features.")
.overrides_with("optional"),
clap::Arg::new("rename")
.long("rename")
.takes_value(true)
.action(ArgAction::Set)
.value_name("NAME")
.help("Rename the dependency")
.long_help("Rename the dependency
@ -79,7 +76,7 @@ Example uses:
clap::Arg::new("package")
.short('p')
.long("package")
.takes_value(true)
.action(ArgAction::Set)
.value_name("SPEC")
.help("Package to modify"),
])
@ -89,14 +86,14 @@ Example uses:
.args([
clap::Arg::new("path")
.long("path")
.takes_value(true)
.action(ArgAction::Set)
.value_name("PATH")
.help("Filesystem path to local crate to add")
.group("selected")
.conflicts_with("git"),
clap::Arg::new("git")
.long("git")
.takes_value(true)
.action(ArgAction::Set)
.value_name("URI")
.help("Git repository location")
.long_help("Git repository location
@ -105,21 +102,21 @@ Without any other information, cargo will use latest commit on the main branch."
.group("selected"),
clap::Arg::new("branch")
.long("branch")
.takes_value(true)
.action(ArgAction::Set)
.value_name("BRANCH")
.help("Git branch to download the crate from")
.requires("git")
.group("git-ref"),
clap::Arg::new("tag")
.long("tag")
.takes_value(true)
.action(ArgAction::Set)
.value_name("TAG")
.help("Git tag to download the crate from")
.requires("git")
.group("git-ref"),
clap::Arg::new("rev")
.long("rev")
.takes_value(true)
.action(ArgAction::Set)
.value_name("REV")
.help("Git reference to download the crate from")
.long_help("Git reference to download the crate from
@ -129,7 +126,7 @@ This is the catch all, handling hashes to named references in remote repositorie
.group("git-ref"),
clap::Arg::new("registry")
.long("registry")
.takes_value(true)
.action(ArgAction::Set)
.value_name("NAME")
.help("Package registry for this dependency"),
])
@ -151,7 +148,7 @@ Build-dependencies are the only dependencies available for use by build scripts
.group("section"),
clap::Arg::new("target")
.long("target")
.takes_value(true)
.action(ArgAction::Set)
.value_name("TARGET")
.value_parser(clap::builder::NonEmptyStringValueParser::new())
.help("Add as dependency to the given target platform")

View File

@ -8,13 +8,13 @@ pub fn cli() -> Command {
.arg_quiet()
.arg(
Arg::new("BENCHNAME")
.action(ArgAction::Set)
.help("If specified, only run benches containing this string in their names"),
)
.arg(
Arg::new("args")
.help("Arguments for the bench binary")
.multiple_values(true)
.last(true),
.num_args(0..),
)
.arg_targets_all(
"Benchmark only this package's library",

View File

@ -9,7 +9,11 @@ pub fn cli() -> Command {
.arg_required_else_help(true)
.subcommand(
subcommand("get")
.arg(Arg::new("key").help("The config key to display"))
.arg(
Arg::new("key")
.action(ArgAction::Set)
.help("The config key to display"),
)
.arg(
opt("format", "Display format")
.value_parser(cargo_config::ConfigFormat::POSSIBLE_VALUES)

View File

@ -1,10 +1,10 @@
use crate::command_prelude::*;
const REMOVED: &str = "The `git-checkout` subcommand has been removed.";
const REMOVED: &str = "The `git-checkout` command has been removed.";
pub fn cli() -> Command {
subcommand("git-checkout")
.about("This subcommand has been removed")
.about("This command has been removed")
.hide(true)
.override_help(REMOVED)
}

View File

@ -15,11 +15,11 @@ const COMPRESSED_MAN: &[u8] = include_bytes!(concat!(env!("OUT_DIR"), "/man.tgz"
pub fn cli() -> Command {
subcommand("help")
.about("Displays help for a cargo subcommand")
.arg(Arg::new("SUBCOMMAND"))
.arg(Arg::new("COMMAND").action(ArgAction::Set))
}
pub fn exec(config: &mut Config, args: &ArgMatches) -> CliResult {
let subcommand = args.get_one::<String>("SUBCOMMAND");
let subcommand = args.get_one::<String>("COMMAND");
if let Some(subcommand) = subcommand {
if !try_help(config, subcommand)? {
crate::execute_external_subcommand(

View File

@ -6,7 +6,7 @@ pub fn cli() -> Command {
subcommand("init")
.about("Create a new cargo package in an existing directory")
.arg_quiet()
.arg(Arg::new("path").default_value("."))
.arg(Arg::new("path").action(ArgAction::Set).default_value("."))
.arg(opt("registry", "Registry to use").value_name("REGISTRY"))
.arg_new_opts()
.after_help("Run `cargo help init` for more detailed information.\n")

View File

@ -13,7 +13,7 @@ pub fn cli() -> Command {
.arg(
Arg::new("crate")
.value_parser(clap::builder::NonEmptyStringValueParser::new())
.multiple_values(true),
.num_args(0..),
)
.arg(
opt("version", "Specify a version to install")

View File

@ -9,7 +9,7 @@ pub fn cli() -> Command {
If token is not specified, it will be read from stdin.",
)
.arg_quiet()
.arg(Arg::new("token"))
.arg(Arg::new("token").action(ArgAction::Set))
.arg(opt("registry", "Registry to use").value_name("REGISTRY"))
.after_help("Run `cargo help login` for more detailed information.\n")
}

View File

@ -6,7 +6,7 @@ pub fn cli() -> Command {
subcommand("new")
.about("Create a new cargo package at <path>")
.arg_quiet()
.arg(Arg::new("path").required(true))
.arg(Arg::new("path").action(ArgAction::Set).required(true))
.arg(opt("registry", "Registry to use").value_name("REGISTRY"))
.arg_new_opts()
.after_help("Run `cargo help new` for more detailed information.\n")

View File

@ -6,7 +6,7 @@ pub fn cli() -> Command {
subcommand("owner")
.about("Manage the owners of a crate on the registry")
.arg_quiet()
.arg(Arg::new("crate"))
.arg(Arg::new("crate").action(ArgAction::Set))
.arg(
multi_opt(
"add",

View File

@ -7,7 +7,7 @@ pub fn cli() -> Command {
subcommand("pkgid")
.about("Print a fully qualified package specification")
.arg_quiet()
.arg(Arg::new("spec"))
.arg(Arg::new("spec").action(ArgAction::Set))
.arg_package("Argument to get the package ID specifier for")
.arg_manifest_path()
.after_help("Run `cargo help pkgid` for more detailed information.\n")

View File

@ -14,7 +14,7 @@ pub fn cli() -> Command {
.arg(
Arg::new("args")
.value_parser(value_parser!(std::ffi::OsString))
.multiple_values(true),
.num_args(0..),
)
.arg_targets_bin_example(
"Name of the bin target to run",

View File

@ -10,7 +10,7 @@ pub fn cli() -> Command {
.trailing_var_arg(true)
.about("Compile a package, and pass extra options to the compiler")
.arg_quiet()
.arg(Arg::new("args").multiple_values(true).help("Rustc flags"))
.arg(Arg::new("args").num_args(0..).help("Rustc flags"))
.arg_package("Package to build")
.arg_jobs()
.arg_targets_all(

View File

@ -7,7 +7,7 @@ pub fn cli() -> Command {
.trailing_var_arg(true)
.about("Build a package's documentation, using specified custom flags.")
.arg_quiet()
.arg(Arg::new("args").multiple_values(true))
.arg(Arg::new("args").num_args(0..))
.arg(flag(
"open",
"Opens the docs in a browser after the operation",

View File

@ -8,7 +8,7 @@ pub fn cli() -> Command {
subcommand("search")
.about("Search packages in crates.io")
.arg_quiet()
.arg(Arg::new("query").multiple_values(true))
.arg(Arg::new("query").num_args(0..))
.arg_index()
.arg(
opt(

View File

@ -9,13 +9,13 @@ pub fn cli() -> Command {
.about("Execute all unit and integration tests and build examples of a local package")
.arg(
Arg::new("TESTNAME")
.action(ArgAction::Set)
.help("If specified, only run tests containing this string in their names"),
)
.arg(
Arg::new("args")
.help("Arguments for the test binary")
.multiple_values(true)
.last(true),
.num_args(0..),
)
.arg(
flag(

View File

@ -6,7 +6,7 @@ pub fn cli() -> Command {
subcommand("uninstall")
.about("Remove a Rust binary")
.arg_quiet()
.arg(Arg::new("spec").multiple_values(true))
.arg(Arg::new("spec").num_args(0..))
.arg_package_spec_simple("Package to uninstall")
.arg(multi_opt("bin", "NAME", "Only uninstall the binary NAME"))
.arg(opt("root", "Directory to uninstall packages from").value_name("DIR"))

View File

@ -9,6 +9,7 @@ pub fn cli() -> Command {
.arg_manifest_path()
.arg(
Arg::new("path")
.action(ArgAction::Set)
.value_parser(clap::value_parser!(PathBuf))
.help("Where to vendor crates (`vendor` by default)"),
)

View File

@ -6,7 +6,7 @@ pub fn cli() -> Command {
subcommand("yank")
.about("Remove a pushed crate from the index")
.arg_quiet()
.arg(Arg::new("crate"))
.arg(Arg::new("crate").action(ArgAction::Set))
.arg(
opt("version", "The version to yank or un-yank")
.alias("vers")

View File

@ -168,7 +168,7 @@ fn execute_external_subcommand(config: &Config, cmd: &str, args: &[&OsStr]) -> C
None => {
let err = if cmd.starts_with('+') {
anyhow::format_err!(
"no such subcommand: `{}`\n\n\t\
"no such command: `{}`\n\n\t\
Cargo does not handle `+toolchain` directives.\n\t\
Did you mean to invoke `cargo` through `rustup` instead?",
cmd
@ -178,7 +178,7 @@ fn execute_external_subcommand(config: &Config, cmd: &str, args: &[&OsStr]) -> C
let did_you_mean = closest_msg(cmd, suggestions.keys(), |c| c);
anyhow::format_err!(
"no such subcommand: `{}`{}\n\n\t\
"no such command: `{}`{}\n\n\t\
View all installed commands with `cargo --list`",
cmd,
did_you_mean

View File

@ -20,12 +20,12 @@ use std::path::PathBuf;
pub use crate::core::compiler::CompileMode;
pub use crate::{CliError, CliResult, Config};
pub use clap::{value_parser, AppSettings, Arg, ArgAction, ArgMatches};
pub use clap::{value_parser, Arg, ArgAction, ArgMatches};
pub type Command = clap::Command<'static>;
pub use clap::Command;
pub trait CommandExt: Sized {
fn _arg(self, arg: Arg<'static>) -> Self;
fn _arg(self, arg: Arg) -> Self;
/// Do not use this method, it is only for backwards compatibility.
/// Use `arg_package_spec_no_all` instead.
@ -256,40 +256,34 @@ pub trait CommandExt: Sized {
}
impl CommandExt for Command {
fn _arg(self, arg: Arg<'static>) -> Self {
fn _arg(self, arg: Arg) -> Self {
self.arg(arg)
}
}
pub fn flag(name: &'static str, help: &'static str) -> Arg<'static> {
pub fn flag(name: &'static str, help: &'static str) -> Arg {
Arg::new(name)
.long(name)
.help(help)
.action(ArgAction::SetTrue)
}
pub fn opt(name: &'static str, help: &'static str) -> Arg<'static> {
Arg::new(name).long(name).help(help)
pub fn opt(name: &'static str, help: &'static str) -> Arg {
Arg::new(name).long(name).help(help).action(ArgAction::Set)
}
pub fn optional_opt(name: &'static str, help: &'static str) -> Arg<'static> {
opt(name, help).min_values(0)
pub fn optional_opt(name: &'static str, help: &'static str) -> Arg {
opt(name, help).num_args(0..=1)
}
pub fn optional_multi_opt(
name: &'static str,
value_name: &'static str,
help: &'static str,
) -> Arg<'static> {
pub fn optional_multi_opt(name: &'static str, value_name: &'static str, help: &'static str) -> Arg {
opt(name, help)
.value_name(value_name)
.num_args(0..=1)
.action(ArgAction::Append)
.multiple_values(true)
.min_values(0)
.number_of_values(1)
}
pub fn multi_opt(name: &'static str, value_name: &'static str, help: &'static str) -> Arg<'static> {
pub fn multi_opt(name: &'static str, value_name: &'static str, help: &'static str) -> Arg {
opt(name, help)
.value_name(value_name)
.action(ArgAction::Append)
@ -297,8 +291,6 @@ pub fn multi_opt(name: &'static str, value_name: &'static str, help: &'static st
pub fn subcommand(name: &'static str) -> Command {
Command::new(name)
.dont_collapse_args_in_usage(true)
.setting(AppSettings::DeriveDisplayOrder)
}
/// Determines whether or not to gate `--profile` as unstable when resolving it.
@ -792,7 +784,7 @@ pub fn values_os(args: &ArgMatches, name: &str) -> Vec<OsString> {
}
#[track_caller]
fn ignore_unknown<T: Default>(r: Result<T, clap::parser::MatchesError>) -> T {
pub fn ignore_unknown<T: Default>(r: Result<T, clap::parser::MatchesError>) -> T {
match r {
Ok(t) => t,
Err(clap::parser::MatchesError::UnknownArgument { .. }) => Default::default(),

View File

@ -1,8 +1,7 @@
error: The argument '--dev' cannot be used with '--build'
USAGE:
cargo add [OPTIONS] <DEP>[@<VERSION>] ...
cargo add [OPTIONS] --path <PATH> ...
cargo add [OPTIONS] --git <URL> ...
Usage: cargo add [OPTIONS] <DEP>[@<VERSION>] ...
cargo add [OPTIONS] --path <PATH> ...
cargo add [OPTIONS] --git <URL> ...
For more information try --help
For more information try '--help'

View File

@ -1,10 +1,9 @@
error: Found argument '--flag' which wasn't expected, or isn't valid in this context
If you tried to supply `--flag` as a value rather than a flag, use `-- --flag`
If you tried to supply '--flag' as a value rather than a flag, use '-- --flag'
USAGE:
cargo add [OPTIONS] <DEP>[@<VERSION>] ...
cargo add [OPTIONS] --path <PATH> ...
cargo add [OPTIONS] --git <URL> ...
Usage: cargo add [OPTIONS] <DEP>[@<VERSION>] ...
cargo add [OPTIONS] --path <PATH> ...
cargo add [OPTIONS] --git <URL> ...
For more information try --help
For more information try '--help'

View File

@ -1,3 +1,3 @@
error: The argument '--target <TARGET>' requires a value but none was supplied
For more information try --help
For more information try '--help'

View File

@ -1,9 +1,8 @@
error: The following required arguments were not provided:
<DEP_ID|--path <PATH>|--git <URI>>
<DEP_ID|--path <PATH>|--git <URI>>
USAGE:
cargo add [OPTIONS] <DEP>[@<VERSION>] ...
cargo add [OPTIONS] --path <PATH> ...
cargo add [OPTIONS] --git <URL> ...
Usage: cargo add [OPTIONS] <DEP>[@<VERSION>] ...
cargo add [OPTIONS] --path <PATH> ...
cargo add [OPTIONS] --git <URL> ...
For more information try --help
For more information try '--help'

View File

@ -156,7 +156,7 @@ fn find_closest_capital_c_to_c() {
.with_status(101)
.with_stderr_contains(
"\
error: no such subcommand: `C`
error: no such command: `C`
<tab>Did you mean `c`?
",
@ -170,7 +170,7 @@ fn find_closest_capital_b_to_b() {
.with_status(101)
.with_stderr_contains(
"\
error: no such subcommand: `B`
error: no such command: `B`
<tab>Did you mean `b`?
",
@ -184,7 +184,7 @@ fn find_closest_biuld_to_build() {
.with_status(101)
.with_stderr_contains(
"\
error: no such subcommand: `biuld`
error: no such command: `biuld`
<tab>Did you mean `build`?
",
@ -235,7 +235,7 @@ fn find_closest_alias() {
.with_status(101)
.with_stderr_contains(
"\
error: no such subcommand: `myalais`
error: no such command: `myalais`
<tab>Did you mean `myalias`?
",
@ -247,7 +247,7 @@ error: no such subcommand: `myalais`
.with_status(101)
.with_stderr_contains(
"\
error: no such subcommand: `myalais`
error: no such command: `myalais`
",
)
.with_stderr_does_not_contain(
@ -266,7 +266,7 @@ fn find_closest_dont_correct_nonsense() {
.with_status(101)
.with_stderr(
"\
[ERROR] no such subcommand: `there-is-no-way-that-there-is-a-command-close-to-this`
[ERROR] no such command: `there-is-no-way-that-there-is-a-command-close-to-this`
<tab>View all installed commands with `cargo --list`",
)
@ -279,7 +279,7 @@ fn displays_subcommand_on_error() {
.with_status(101)
.with_stderr(
"\
[ERROR] no such subcommand: `invalid-command`
[ERROR] no such command: `invalid-command`
<tab>View all installed commands with `cargo --list`",
)
@ -473,7 +473,7 @@ fn subcommand_leading_plus_output_contains() {
.with_status(101)
.with_stderr(
"\
error: no such subcommand: `+nightly`
error: no such command: `+nightly`
<tab>Cargo does not handle `+toolchain` directives.
<tab>Did you mean to invoke `cargo` through `rustup` instead?",
@ -487,7 +487,7 @@ fn full_did_you_mean() {
.with_status(101)
.with_stderr(
"\
error: no such subcommand: `bluid`
error: no such command: `bluid`
<tab>Did you mean `build`?

View File

@ -1,8 +1,7 @@
error: Found argument '--flag' which wasn't expected, or isn't valid in this context
If you tried to supply `--flag` as a value rather than a flag, use `-- --flag`
If you tried to supply '--flag' as a value rather than a flag, use '-- --flag'
USAGE:
cargo[EXE] init <path>
Usage: cargo[EXE] init <path>
For more information try --help
For more information try '--help'

View File

@ -1448,8 +1448,7 @@ fn not_both_vers_and_version() {
.with_status(1)
.with_stderr_contains(
"\
error: The argument '--version <VERSION>' was provided more than once, \
but cannot be used multiple times
error: The argument '--version <VERSION>' was provided more than once, but cannot be used multiple times
",
)
.run();
@ -1652,7 +1651,7 @@ fn install_empty_argument() {
.arg("")
.with_status(1)
.with_stderr_contains(
"[ERROR] The argument '<crate>...' requires a value but none was supplied",
"[ERROR] The argument '[crate]...' requires a value but none was supplied",
)
.run();
}

View File

@ -1911,7 +1911,7 @@ fn cargo_metadata_bad_version() {
.with_stderr_contains(
"\
error: \"2\" isn't a valid value for '--format-version <VERSION>'
<tab>[possible values: 1]
[possible values: 1]
",
)
.run();

View File

@ -107,7 +107,7 @@ fn no_argument() {
.with_stderr_contains(
"\
error: The following required arguments were not provided:
<path>
<path>
",
)
.run();

View File

@ -1314,9 +1314,16 @@ fn run_multiple_packages() {
cargo().with_stdout("foo").run();
cargo().arg("-p").arg("d1").arg("-p").arg("d2")
.with_status(1)
.with_stderr_contains("error: The argument '--package [<SPEC>...]' was provided more than once, but cannot be used multiple times").run();
cargo()
.arg("-p")
.arg("d1")
.arg("-p")
.arg("d2")
.with_status(1)
.with_stderr_contains(
"error: The argument '--package [<SPEC>]' was provided more than once, but cannot be used multiple times",
)
.run();
cargo()
.arg("-p")

View File

@ -551,8 +551,7 @@ fn fail_with_multiple_packages() {
.with_status(1)
.with_stderr_contains(
"\
error: The argument '--package [<SPEC>...]' was provided more than once, \
but cannot be used multiple times
error: The argument '--package [<SPEC>]' was provided more than once, but cannot be used multiple times
",
)
.run();

View File

@ -405,10 +405,9 @@ fn test_sync_argument() {
.with_stderr("\
error: Found argument 'test_vendor' which wasn't expected, or isn't valid in this context
USAGE:
cargo[EXE] vendor [OPTIONS] [path]
Usage: cargo[EXE] vendor [OPTIONS] [path]
For more information try --help",
For more information try '--help'",
)
.with_status(1)
.run();