Skip to content

Commit

Permalink
Auto merge of #11159 - epage:clap4, r=weihanglo
Browse files Browse the repository at this point in the history
refactor(cli): Upgrade to clap v4
  • Loading branch information
bors committed Sep 29, 2022
2 parents a3e352e + 69ba69f commit c39193a
Show file tree
Hide file tree
Showing 35 changed files with 113 additions and 119 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
15 changes: 6 additions & 9 deletions src/bin/cargo/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand All @@ -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):
Expand Down
29 changes: 13 additions & 16 deletions src/bin/cargo/commands/add.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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"),
Expand All @@ -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
Expand All @@ -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"),
])
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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"),
])
Expand All @@ -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")
Expand Down
6 changes: 3 additions & 3 deletions src/bin/cargo/commands/bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,18 @@ use cargo::ops::{self, TestOptions};

pub fn cli() -> Command {
subcommand("bench")
.trailing_var_arg(true)
.about("Execute all benchmarks of a local package")
.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..)
.trailing_var_arg(true),
)
.arg_targets_all(
"Benchmark only this package's library",
Expand Down
6 changes: 5 additions & 1 deletion src/bin/cargo/commands/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions src/bin/cargo/commands/git_checkout.rs
Original file line number Diff line number Diff line change
@@ -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)
}
Expand Down
4 changes: 2 additions & 2 deletions src/bin/cargo/commands/help.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
2 changes: 1 addition & 1 deletion src/bin/cargo/commands/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
2 changes: 1 addition & 1 deletion src/bin/cargo/commands/install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
2 changes: 1 addition & 1 deletion src/bin/cargo/commands/login.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand Down
2 changes: 1 addition & 1 deletion src/bin/cargo/commands/new.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
2 changes: 1 addition & 1 deletion src/bin/cargo/commands/owner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion src/bin/cargo/commands/pkgid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
4 changes: 2 additions & 2 deletions src/bin/cargo/commands/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ pub fn cli() -> Command {
subcommand("run")
// subcommand aliases are handled in aliased_command()
// .alias("r")
.trailing_var_arg(true)
.about("Run a binary or example of the local package")
.arg_quiet()
.arg(
Arg::new("args")
.value_parser(value_parser!(std::ffi::OsString))
.multiple_values(true),
.num_args(0..)
.trailing_var_arg(true),
)
.arg_targets_bin_example(
"Name of the bin target to run",
Expand Down
8 changes: 6 additions & 2 deletions src/bin/cargo/commands/rustc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,14 @@ const CRATE_TYPE_ARG_NAME: &str = "crate-type";

pub fn cli() -> Command {
subcommand("rustc")
.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")
.trailing_var_arg(true),
)
.arg_package("Package to build")
.arg_jobs()
.arg_targets_all(
Expand Down
3 changes: 1 addition & 2 deletions src/bin/cargo/commands/rustdoc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@ use crate::command_prelude::*;

pub fn cli() -> Command {
subcommand("rustdoc")
.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..).trailing_var_arg(true))
.arg(flag(
"open",
"Opens the docs in a browser after the operation",
Expand Down
2 changes: 1 addition & 1 deletion src/bin/cargo/commands/search.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
6 changes: 3 additions & 3 deletions src/bin/cargo/commands/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@ pub fn cli() -> Command {
subcommand("test")
// Subcommand aliases are handled in `aliased_command()`.
// .alias("t")
.trailing_var_arg(true)
.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..)
.trailing_var_arg(true),
)
.arg(
flag(
Expand Down
2 changes: 1 addition & 1 deletion src/bin/cargo/commands/uninstall.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
Expand Down
1 change: 1 addition & 0 deletions src/bin/cargo/commands/vendor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)"),
)
Expand Down
2 changes: 1 addition & 1 deletion src/bin/cargo/commands/yank.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down

0 comments on commit c39193a

Please sign in to comment.