Skip to content

Commit 86376c8

Browse files
committedFeb 17, 2022
Auto merge of #10396 - jonhoo:clap-deprecations, r=epage
Avoid new deprecation warnings from clap 3.1.0 ### What does this PR try to resolve? This fixes a number of new deprecations in [clap 3.1.0](https://github.com/clap-rs/clap/blob/master/CHANGELOG.md#310---2022-02-16) that are currently causing CI to fail for all other PRs due to `-Dwarnings`.
2 parents ea2a21c + 6cbf913 commit 86376c8

File tree

11 files changed

+33
-23
lines changed

11 files changed

+33
-23
lines changed
 

‎Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ toml_edit = { version = "0.13.4", features = ["serde", "easy"] }
6161
unicode-xid = "0.2.0"
6262
url = "2.2.2"
6363
walkdir = "2.2"
64-
clap = "3.0.13"
64+
clap = "3.1.0"
6565
unicode-width = "0.1.5"
6666
openssl = { version = '0.10.11', optional = true }
6767
im-rc = "15.0.0"

‎src/bin/cargo/cli.rs

+18-12
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
use anyhow::anyhow;
22
use cargo::core::{features, CliUnstable};
33
use cargo::{self, drop_print, drop_println, CliResult, Config};
4-
use clap::{AppSettings, Arg, ArgMatches};
4+
use clap::{
5+
error::{ContextKind, ContextValue},
6+
AppSettings, Arg, ArgMatches,
7+
};
58
use itertools::Itertools;
69
use std::collections::HashMap;
710
use std::fmt::Write;
@@ -33,9 +36,17 @@ pub fn main(config: &mut Config) -> CliResult {
3336
let args = match cli().try_get_matches() {
3437
Ok(args) => args,
3538
Err(e) => {
36-
if e.kind == clap::ErrorKind::UnrecognizedSubcommand {
39+
if e.kind() == clap::ErrorKind::UnrecognizedSubcommand {
3740
// An unrecognized subcommand might be an external subcommand.
38-
let cmd = e.info[0].clone();
41+
let cmd = e
42+
.context()
43+
.find_map(|c| match c {
44+
(ContextKind::InvalidSubcommand, &ContextValue::String(ref cmd)) => {
45+
Some(cmd)
46+
}
47+
_ => None,
48+
})
49+
.expect("UnrecognizedSubcommand implies the presence of InvalidSubcommand");
3950
return super::execute_external_subcommand(config, &cmd, &[&cmd, "--help"])
4051
.map_err(|_| e.into());
4152
} else {
@@ -286,9 +297,7 @@ For more information, see issue #10049 <https://github.com/rust-lang/cargo/issue
286297
// Note that an alias to an external command will not receive
287298
// these arguments. That may be confusing, but such is life.
288299
let global_args = GlobalArgs::new(args);
289-
let new_args = cli()
290-
.setting(AppSettings::NoBinaryName)
291-
.try_get_matches_from(alias)?;
300+
let new_args = cli().no_binary_name(true).try_get_matches_from(alias)?;
292301

293302
let new_cmd = new_args.subcommand_name().expect("subcommand is required");
294303
already_expanded.push(cmd.to_string());
@@ -406,14 +415,11 @@ fn cli() -> App {
406415
"cargo [OPTIONS] [SUBCOMMAND]"
407416
};
408417
App::new("cargo")
409-
.setting(
410-
AppSettings::DeriveDisplayOrder
411-
| AppSettings::AllowExternalSubcommands
412-
| AppSettings::NoAutoVersion,
413-
)
418+
.allow_external_subcommands(true)
419+
.setting(AppSettings::DeriveDisplayOrder | AppSettings::NoAutoVersion)
414420
// Doesn't mix well with our list of common cargo commands. See clap-rs/clap#3108 for
415421
// opening clap up to allow us to style our help template
416-
.global_setting(AppSettings::DisableColoredHelp)
422+
.disable_colored_help(true)
417423
.override_usage(usage)
418424
.help_template(
419425
"\

‎src/bin/cargo/commands/bench.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use cargo::ops::{self, TestOptions};
33

44
pub fn cli() -> App {
55
subcommand("bench")
6-
.setting(AppSettings::TrailingVarArg)
6+
.trailing_var_arg(true)
77
.about("Execute all benchmarks of a local package")
88
.arg_quiet()
99
.arg(

‎src/bin/cargo/commands/config.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ pub fn cli() -> App {
55
subcommand("config")
66
.about("Inspect configuration values")
77
.after_help("Run `cargo help config` for more detailed information.\n")
8-
.setting(clap::AppSettings::SubcommandRequiredElseHelp)
8+
.subcommand_required(true)
9+
.arg_required_else_help(true)
910
.subcommand(
1011
subcommand("get")
1112
.arg(Arg::new("key").help("The config key to display"))

‎src/bin/cargo/commands/git_checkout.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const REMOVED: &str = "The `git-checkout` subcommand has been removed.";
55
pub fn cli() -> App {
66
subcommand("git-checkout")
77
.about("This subcommand has been removed")
8-
.setting(AppSettings::Hidden)
8+
.hide(true)
99
.override_help(REMOVED)
1010
}
1111

‎src/bin/cargo/commands/report.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ pub fn cli() -> App {
66
subcommand("report")
77
.about("Generate and display various kinds of reports")
88
.after_help("Run `cargo help report` for more detailed information.\n")
9-
.setting(clap::AppSettings::SubcommandRequiredElseHelp)
9+
.subcommand_required(true)
10+
.arg_required_else_help(true)
1011
.subcommand(
1112
subcommand("future-incompatibilities")
1213
.alias("future-incompat")

‎src/bin/cargo/commands/run.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ pub fn cli() -> App {
88
subcommand("run")
99
// subcommand aliases are handled in aliased_command()
1010
// .alias("r")
11-
.setting(AppSettings::TrailingVarArg)
11+
.trailing_var_arg(true)
1212
.about("Run a binary or example of the local package")
1313
.arg_quiet()
1414
.arg(

‎src/bin/cargo/commands/rustc.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const CRATE_TYPE_ARG_NAME: &str = "crate-type";
77

88
pub fn cli() -> App {
99
subcommand("rustc")
10-
.setting(AppSettings::TrailingVarArg)
10+
.trailing_var_arg(true)
1111
.about("Compile a package, and pass extra options to the compiler")
1212
.arg_quiet()
1313
.arg(Arg::new("args").multiple_values(true).help("Rustc flags"))

‎src/bin/cargo/commands/rustdoc.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use crate::command_prelude::*;
44

55
pub fn cli() -> App {
66
subcommand("rustdoc")
7-
.setting(AppSettings::TrailingVarArg)
7+
.trailing_var_arg(true)
88
.about("Build a package's documentation, using specified custom flags.")
99
.arg_quiet()
1010
.arg(Arg::new("args").multiple_values(true))

‎src/bin/cargo/commands/test.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ pub fn cli() -> App {
66
subcommand("test")
77
// Subcommand aliases are handled in `aliased_command()`.
88
// .alias("t")
9-
.setting(AppSettings::TrailingVarArg)
9+
.trailing_var_arg(true)
1010
.about("Execute all unit and integration tests and build examples of a local package")
1111
.arg(
1212
Arg::new("TESTNAME")

‎src/cargo/util/command_prelude.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ pub use crate::core::compiler::CompileMode;
2222
pub use crate::{CliError, CliResult, Config};
2323
pub use clap::{AppSettings, Arg, ArgMatches};
2424

25-
pub type App = clap::App<'static>;
25+
pub type App = clap::Command<'static>;
2626

2727
pub trait AppExt: Sized {
2828
fn _arg(self, arg: Arg<'static>) -> Self;
@@ -281,7 +281,9 @@ pub fn multi_opt(name: &'static str, value_name: &'static str, help: &'static st
281281
}
282282

283283
pub fn subcommand(name: &'static str) -> App {
284-
App::new(name).setting(AppSettings::DeriveDisplayOrder | AppSettings::DontCollapseArgsInUsage)
284+
App::new(name)
285+
.dont_collapse_args_in_usage(true)
286+
.setting(AppSettings::DeriveDisplayOrder)
285287
}
286288

287289
/// Determines whether or not to gate `--profile` as unstable when resolving it.

0 commit comments

Comments
 (0)