Skip to content

Commit c39193a

Browse files
committed
Auto merge of #11159 - epage:clap4, r=weihanglo
refactor(cli): Upgrade to clap v4
2 parents a3e352e + 69ba69f commit c39193a

File tree

35 files changed

+113
-119
lines changed

35 files changed

+113
-119
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ toml_edit = { version = "0.14.3", features = ["serde", "easy", "perf"] }
6262
unicode-xid = "0.2.0"
6363
url = "2.2.2"
6464
walkdir = "2.2"
65-
clap = "3.2.18"
65+
clap = "4.0.4"
6666
unicode-width = "0.1.5"
6767
openssl = { version = '0.10.11', optional = true }
6868
im-rc = "15.0.0"

src/bin/cargo/cli.rs

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use anyhow::anyhow;
22
use cargo::core::shell::Shell;
33
use cargo::core::{features, CliUnstable};
44
use cargo::{self, drop_print, drop_println, CliResult, Config};
5-
use clap::{AppSettings, Arg, ArgMatches};
5+
use clap::{Arg, ArgMatches};
66
use itertools::Itertools;
77
use std::collections::HashMap;
88
use std::ffi::OsStr;
@@ -69,7 +69,7 @@ Available unstable (nightly-only) flags:
6969
7070
{}
7171
72-
Run with 'cargo -Z [FLAG] [SUBCOMMAND]'",
72+
Run with 'cargo -Z [FLAG] [COMMAND]'",
7373
joined
7474
);
7575
if !config.nightly_features_allowed {
@@ -406,14 +406,12 @@ impl GlobalArgs {
406406
pub fn cli() -> Command {
407407
let is_rustup = std::env::var_os("RUSTUP_HOME").is_some();
408408
let usage = if is_rustup {
409-
"cargo [+toolchain] [OPTIONS] [SUBCOMMAND]"
409+
"cargo [+toolchain] [OPTIONS] [COMMAND]"
410410
} else {
411-
"cargo [OPTIONS] [SUBCOMMAND]"
411+
"cargo [OPTIONS] [COMMAND]"
412412
};
413413
Command::new("cargo")
414414
.allow_external_subcommands(true)
415-
.allow_invalid_utf8_for_external_subcommands(true)
416-
.setting(AppSettings::DeriveDisplayOrder)
417415
// Doesn't mix well with our list of common cargo commands. See clap-rs/clap#3108 for
418416
// opening clap up to allow us to style our help template
419417
.disable_colored_help(true)
@@ -424,10 +422,9 @@ pub fn cli() -> Command {
424422
"\
425423
Rust's package manager
426424
427-
USAGE:
428-
{usage}
425+
Usage: {usage}
429426
430-
OPTIONS:
427+
Options:
431428
{options}
432429
433430
Some common cargo commands are (see all commands with --list):

src/bin/cargo/commands/add.rs

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,19 @@ use cargo::CargoResult;
1414

1515
pub fn cli() -> Command {
1616
clap::Command::new("add")
17-
.setting(clap::AppSettings::DeriveDisplayOrder)
1817
.about("Add dependencies to a Cargo.toml manifest file")
1918
.override_usage(
2019
"\
21-
cargo add [OPTIONS] <DEP>[@<VERSION>] ...
22-
cargo add [OPTIONS] --path <PATH> ...
23-
cargo add [OPTIONS] --git <URL> ..."
20+
cargo add [OPTIONS] <DEP>[@<VERSION>] ...
21+
cargo add [OPTIONS] --path <PATH> ...
22+
cargo add [OPTIONS] --git <URL> ..."
2423
)
2524
.after_help("Run `cargo help add` for more detailed information.\n")
2625
.group(clap::ArgGroup::new("selected").multiple(true).required(true))
2726
.args([
2827
clap::Arg::new("crates")
29-
.takes_value(true)
3028
.value_name("DEP_ID")
31-
.multiple_values(true)
29+
.num_args(0..)
3230
.help("Reference to a package to add as a dependency")
3331
.long_help(
3432
"Reference to a package to add as a dependency
@@ -46,7 +44,6 @@ You can reference a package by:
4644
clap::Arg::new("features")
4745
.short('F')
4846
.long("features")
49-
.takes_value(true)
5047
.value_name("FEATURES")
5148
.action(ArgAction::Append)
5249
.help("Space or comma separated list of features to activate"),
@@ -65,7 +62,7 @@ The package will be removed from your features.")
6562
.overrides_with("optional"),
6663
clap::Arg::new("rename")
6764
.long("rename")
68-
.takes_value(true)
65+
.action(ArgAction::Set)
6966
.value_name("NAME")
7067
.help("Rename the dependency")
7168
.long_help("Rename the dependency
@@ -79,7 +76,7 @@ Example uses:
7976
clap::Arg::new("package")
8077
.short('p')
8178
.long("package")
82-
.takes_value(true)
79+
.action(ArgAction::Set)
8380
.value_name("SPEC")
8481
.help("Package to modify"),
8582
])
@@ -89,14 +86,14 @@ Example uses:
8986
.args([
9087
clap::Arg::new("path")
9188
.long("path")
92-
.takes_value(true)
89+
.action(ArgAction::Set)
9390
.value_name("PATH")
9491
.help("Filesystem path to local crate to add")
9592
.group("selected")
9693
.conflicts_with("git"),
9794
clap::Arg::new("git")
9895
.long("git")
99-
.takes_value(true)
96+
.action(ArgAction::Set)
10097
.value_name("URI")
10198
.help("Git repository location")
10299
.long_help("Git repository location
@@ -105,21 +102,21 @@ Without any other information, cargo will use latest commit on the main branch."
105102
.group("selected"),
106103
clap::Arg::new("branch")
107104
.long("branch")
108-
.takes_value(true)
105+
.action(ArgAction::Set)
109106
.value_name("BRANCH")
110107
.help("Git branch to download the crate from")
111108
.requires("git")
112109
.group("git-ref"),
113110
clap::Arg::new("tag")
114111
.long("tag")
115-
.takes_value(true)
112+
.action(ArgAction::Set)
116113
.value_name("TAG")
117114
.help("Git tag to download the crate from")
118115
.requires("git")
119116
.group("git-ref"),
120117
clap::Arg::new("rev")
121118
.long("rev")
122-
.takes_value(true)
119+
.action(ArgAction::Set)
123120
.value_name("REV")
124121
.help("Git reference to download the crate from")
125122
.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
129126
.group("git-ref"),
130127
clap::Arg::new("registry")
131128
.long("registry")
132-
.takes_value(true)
129+
.action(ArgAction::Set)
133130
.value_name("NAME")
134131
.help("Package registry for this dependency"),
135132
])
@@ -151,7 +148,7 @@ Build-dependencies are the only dependencies available for use by build scripts
151148
.group("section"),
152149
clap::Arg::new("target")
153150
.long("target")
154-
.takes_value(true)
151+
.action(ArgAction::Set)
155152
.value_name("TARGET")
156153
.value_parser(clap::builder::NonEmptyStringValueParser::new())
157154
.help("Add as dependency to the given target platform")

src/bin/cargo/commands/bench.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,18 @@ use cargo::ops::{self, TestOptions};
33

44
pub fn cli() -> Command {
55
subcommand("bench")
6-
.trailing_var_arg(true)
76
.about("Execute all benchmarks of a local package")
87
.arg_quiet()
98
.arg(
109
Arg::new("BENCHNAME")
10+
.action(ArgAction::Set)
1111
.help("If specified, only run benches containing this string in their names"),
1212
)
1313
.arg(
1414
Arg::new("args")
1515
.help("Arguments for the bench binary")
16-
.multiple_values(true)
17-
.last(true),
16+
.num_args(0..)
17+
.trailing_var_arg(true),
1818
)
1919
.arg_targets_all(
2020
"Benchmark only this package's library",

src/bin/cargo/commands/config.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,11 @@ pub fn cli() -> Command {
99
.arg_required_else_help(true)
1010
.subcommand(
1111
subcommand("get")
12-
.arg(Arg::new("key").help("The config key to display"))
12+
.arg(
13+
Arg::new("key")
14+
.action(ArgAction::Set)
15+
.help("The config key to display"),
16+
)
1317
.arg(
1418
opt("format", "Display format")
1519
.value_parser(cargo_config::ConfigFormat::POSSIBLE_VALUES)

src/bin/cargo/commands/git_checkout.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
use crate::command_prelude::*;
22

3-
const REMOVED: &str = "The `git-checkout` subcommand has been removed.";
3+
const REMOVED: &str = "The `git-checkout` command has been removed.";
44

55
pub fn cli() -> Command {
66
subcommand("git-checkout")
7-
.about("This subcommand has been removed")
7+
.about("This command has been removed")
88
.hide(true)
99
.override_help(REMOVED)
1010
}

src/bin/cargo/commands/help.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@ const COMPRESSED_MAN: &[u8] = include_bytes!(concat!(env!("OUT_DIR"), "/man.tgz"
1515
pub fn cli() -> Command {
1616
subcommand("help")
1717
.about("Displays help for a cargo subcommand")
18-
.arg(Arg::new("SUBCOMMAND"))
18+
.arg(Arg::new("COMMAND").action(ArgAction::Set))
1919
}
2020

2121
pub fn exec(config: &mut Config, args: &ArgMatches) -> CliResult {
22-
let subcommand = args.get_one::<String>("SUBCOMMAND");
22+
let subcommand = args.get_one::<String>("COMMAND");
2323
if let Some(subcommand) = subcommand {
2424
if !try_help(config, subcommand)? {
2525
crate::execute_external_subcommand(

src/bin/cargo/commands/init.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ pub fn cli() -> Command {
66
subcommand("init")
77
.about("Create a new cargo package in an existing directory")
88
.arg_quiet()
9-
.arg(Arg::new("path").default_value("."))
9+
.arg(Arg::new("path").action(ArgAction::Set).default_value("."))
1010
.arg(opt("registry", "Registry to use").value_name("REGISTRY"))
1111
.arg_new_opts()
1212
.after_help("Run `cargo help init` for more detailed information.\n")

src/bin/cargo/commands/install.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ pub fn cli() -> Command {
1313
.arg(
1414
Arg::new("crate")
1515
.value_parser(clap::builder::NonEmptyStringValueParser::new())
16-
.multiple_values(true),
16+
.num_args(0..),
1717
)
1818
.arg(
1919
opt("version", "Specify a version to install")

src/bin/cargo/commands/login.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ pub fn cli() -> Command {
99
If token is not specified, it will be read from stdin.",
1010
)
1111
.arg_quiet()
12-
.arg(Arg::new("token"))
12+
.arg(Arg::new("token").action(ArgAction::Set))
1313
.arg(opt("registry", "Registry to use").value_name("REGISTRY"))
1414
.after_help("Run `cargo help login` for more detailed information.\n")
1515
}

src/bin/cargo/commands/new.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ pub fn cli() -> Command {
66
subcommand("new")
77
.about("Create a new cargo package at <path>")
88
.arg_quiet()
9-
.arg(Arg::new("path").required(true))
9+
.arg(Arg::new("path").action(ArgAction::Set).required(true))
1010
.arg(opt("registry", "Registry to use").value_name("REGISTRY"))
1111
.arg_new_opts()
1212
.after_help("Run `cargo help new` for more detailed information.\n")

src/bin/cargo/commands/owner.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ pub fn cli() -> Command {
66
subcommand("owner")
77
.about("Manage the owners of a crate on the registry")
88
.arg_quiet()
9-
.arg(Arg::new("crate"))
9+
.arg(Arg::new("crate").action(ArgAction::Set))
1010
.arg(
1111
multi_opt(
1212
"add",

src/bin/cargo/commands/pkgid.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ pub fn cli() -> Command {
77
subcommand("pkgid")
88
.about("Print a fully qualified package specification")
99
.arg_quiet()
10-
.arg(Arg::new("spec"))
10+
.arg(Arg::new("spec").action(ArgAction::Set))
1111
.arg_package("Argument to get the package ID specifier for")
1212
.arg_manifest_path()
1313
.after_help("Run `cargo help pkgid` for more detailed information.\n")

src/bin/cargo/commands/run.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@ pub fn cli() -> Command {
88
subcommand("run")
99
// subcommand aliases are handled in aliased_command()
1010
// .alias("r")
11-
.trailing_var_arg(true)
1211
.about("Run a binary or example of the local package")
1312
.arg_quiet()
1413
.arg(
1514
Arg::new("args")
1615
.value_parser(value_parser!(std::ffi::OsString))
17-
.multiple_values(true),
16+
.num_args(0..)
17+
.trailing_var_arg(true),
1818
)
1919
.arg_targets_bin_example(
2020
"Name of the bin target to run",

src/bin/cargo/commands/rustc.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,14 @@ const CRATE_TYPE_ARG_NAME: &str = "crate-type";
77

88
pub fn cli() -> Command {
99
subcommand("rustc")
10-
.trailing_var_arg(true)
1110
.about("Compile a package, and pass extra options to the compiler")
1211
.arg_quiet()
13-
.arg(Arg::new("args").multiple_values(true).help("Rustc flags"))
12+
.arg(
13+
Arg::new("args")
14+
.num_args(0..)
15+
.help("Rustc flags")
16+
.trailing_var_arg(true),
17+
)
1418
.arg_package("Package to build")
1519
.arg_jobs()
1620
.arg_targets_all(

src/bin/cargo/commands/rustdoc.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,9 @@ use crate::command_prelude::*;
44

55
pub fn cli() -> Command {
66
subcommand("rustdoc")
7-
.trailing_var_arg(true)
87
.about("Build a package's documentation, using specified custom flags.")
98
.arg_quiet()
10-
.arg(Arg::new("args").multiple_values(true))
9+
.arg(Arg::new("args").num_args(0..).trailing_var_arg(true))
1110
.arg(flag(
1211
"open",
1312
"Opens the docs in a browser after the operation",

src/bin/cargo/commands/search.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ pub fn cli() -> Command {
88
subcommand("search")
99
.about("Search packages in crates.io")
1010
.arg_quiet()
11-
.arg(Arg::new("query").multiple_values(true))
11+
.arg(Arg::new("query").num_args(0..))
1212
.arg_index()
1313
.arg(
1414
opt(

src/bin/cargo/commands/test.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,17 @@ pub fn cli() -> Command {
55
subcommand("test")
66
// Subcommand aliases are handled in `aliased_command()`.
77
// .alias("t")
8-
.trailing_var_arg(true)
98
.about("Execute all unit and integration tests and build examples of a local package")
109
.arg(
1110
Arg::new("TESTNAME")
11+
.action(ArgAction::Set)
1212
.help("If specified, only run tests containing this string in their names"),
1313
)
1414
.arg(
1515
Arg::new("args")
1616
.help("Arguments for the test binary")
17-
.multiple_values(true)
18-
.last(true),
17+
.num_args(0..)
18+
.trailing_var_arg(true),
1919
)
2020
.arg(
2121
flag(

src/bin/cargo/commands/uninstall.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ pub fn cli() -> Command {
66
subcommand("uninstall")
77
.about("Remove a Rust binary")
88
.arg_quiet()
9-
.arg(Arg::new("spec").multiple_values(true))
9+
.arg(Arg::new("spec").num_args(0..))
1010
.arg_package_spec_simple("Package to uninstall")
1111
.arg(multi_opt("bin", "NAME", "Only uninstall the binary NAME"))
1212
.arg(opt("root", "Directory to uninstall packages from").value_name("DIR"))

src/bin/cargo/commands/vendor.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ pub fn cli() -> Command {
99
.arg_manifest_path()
1010
.arg(
1111
Arg::new("path")
12+
.action(ArgAction::Set)
1213
.value_parser(clap::value_parser!(PathBuf))
1314
.help("Where to vendor crates (`vendor` by default)"),
1415
)

src/bin/cargo/commands/yank.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ pub fn cli() -> Command {
66
subcommand("yank")
77
.about("Remove a pushed crate from the index")
88
.arg_quiet()
9-
.arg(Arg::new("crate"))
9+
.arg(Arg::new("crate").action(ArgAction::Set))
1010
.arg(
1111
opt("version", "The version to yank or un-yank")
1212
.alias("vers")

0 commit comments

Comments
 (0)