From f78ffb9f3249a7faa7657a14c025a3e84eeb2525 Mon Sep 17 00:00:00 2001 From: Jon Gjengset Date: Wed, 16 Feb 2022 16:25:58 -0800 Subject: [PATCH 1/4] Avoid new deprecation warnings from clap 3.1.0 --- src/bin/cargo/cli.rs | 31 ++++++++++++++++---------- src/bin/cargo/commands/bench.rs | 2 +- src/bin/cargo/commands/config.rs | 2 +- src/bin/cargo/commands/git_checkout.rs | 2 +- src/bin/cargo/commands/report.rs | 2 +- src/bin/cargo/commands/run.rs | 2 +- src/bin/cargo/commands/rustc.rs | 2 +- src/bin/cargo/commands/rustdoc.rs | 2 +- src/bin/cargo/commands/test.rs | 2 +- src/cargo/util/command_prelude.rs | 6 +++-- 10 files changed, 31 insertions(+), 22 deletions(-) diff --git a/src/bin/cargo/cli.rs b/src/bin/cargo/cli.rs index 879e898d5a9..4b12881cf17 100644 --- a/src/bin/cargo/cli.rs +++ b/src/bin/cargo/cli.rs @@ -1,7 +1,10 @@ use anyhow::anyhow; use cargo::core::{features, CliUnstable}; use cargo::{self, drop_print, drop_println, CliResult, Config}; -use clap::{AppSettings, Arg, ArgMatches}; +use clap::{ + error::{ContextKind, ContextValue}, + AppSettings, Arg, ArgMatches, +}; use itertools::Itertools; use std::collections::HashMap; use std::fmt::Write; @@ -33,9 +36,18 @@ pub fn main(config: &mut Config) -> CliResult { let args = match cli().try_get_matches() { Ok(args) => args, Err(e) => { - if e.kind == clap::ErrorKind::UnrecognizedSubcommand { + if e.kind() == clap::ErrorKind::UnrecognizedSubcommand { // An unrecognized subcommand might be an external subcommand. - let cmd = e.info[0].clone(); + let cmd = e + .context() + .find_map(|c| match c { + (ContextKind::InvalidSubcommand, &ContextValue::String(ref cmd)) => { + Some(cmd) + } + (ContextKind::InvalidSubcommand, _) => unreachable!(), + _ => None, + }) + .unwrap(); return super::execute_external_subcommand(config, &cmd, &[&cmd, "--help"]) .map_err(|_| e.into()); } else { @@ -286,9 +298,7 @@ For more information, see issue #10049 App { "cargo [OPTIONS] [SUBCOMMAND]" }; App::new("cargo") - .setting( - AppSettings::DeriveDisplayOrder - | AppSettings::AllowExternalSubcommands - | AppSettings::NoAutoVersion, - ) + .allow_external_subcommands(true) + .setting(AppSettings::DeriveDisplayOrder | AppSettings::NoAutoVersion) // 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 - .global_setting(AppSettings::DisableColoredHelp) + .disable_colored_help(true) .override_usage(usage) .help_template( "\ diff --git a/src/bin/cargo/commands/bench.rs b/src/bin/cargo/commands/bench.rs index 4cd48294c8f..1e2fe37fcc3 100644 --- a/src/bin/cargo/commands/bench.rs +++ b/src/bin/cargo/commands/bench.rs @@ -3,7 +3,7 @@ use cargo::ops::{self, TestOptions}; pub fn cli() -> App { subcommand("bench") - .setting(AppSettings::TrailingVarArg) + .trailing_var_arg(true) .about("Execute all benchmarks of a local package") .arg_quiet() .arg( diff --git a/src/bin/cargo/commands/config.rs b/src/bin/cargo/commands/config.rs index e3492ab876e..dde20036b90 100644 --- a/src/bin/cargo/commands/config.rs +++ b/src/bin/cargo/commands/config.rs @@ -5,7 +5,7 @@ pub fn cli() -> App { subcommand("config") .about("Inspect configuration values") .after_help("Run `cargo help config` for more detailed information.\n") - .setting(clap::AppSettings::SubcommandRequiredElseHelp) + .subcommand_required(true) .subcommand( subcommand("get") .arg(Arg::new("key").help("The config key to display")) diff --git a/src/bin/cargo/commands/git_checkout.rs b/src/bin/cargo/commands/git_checkout.rs index 1b9c83771a9..cd9770302a0 100644 --- a/src/bin/cargo/commands/git_checkout.rs +++ b/src/bin/cargo/commands/git_checkout.rs @@ -5,7 +5,7 @@ const REMOVED: &str = "The `git-checkout` subcommand has been removed."; pub fn cli() -> App { subcommand("git-checkout") .about("This subcommand has been removed") - .setting(AppSettings::Hidden) + .hide(true) .override_help(REMOVED) } diff --git a/src/bin/cargo/commands/report.rs b/src/bin/cargo/commands/report.rs index 0135ef90afb..70018b98245 100644 --- a/src/bin/cargo/commands/report.rs +++ b/src/bin/cargo/commands/report.rs @@ -6,7 +6,7 @@ pub fn cli() -> App { subcommand("report") .about("Generate and display various kinds of reports") .after_help("Run `cargo help report` for more detailed information.\n") - .setting(clap::AppSettings::SubcommandRequiredElseHelp) + .subcommand_required(true) .subcommand( subcommand("future-incompatibilities") .alias("future-incompat") diff --git a/src/bin/cargo/commands/run.rs b/src/bin/cargo/commands/run.rs index 1763decbaad..29e8656e3f0 100644 --- a/src/bin/cargo/commands/run.rs +++ b/src/bin/cargo/commands/run.rs @@ -8,7 +8,7 @@ pub fn cli() -> App { subcommand("run") // subcommand aliases are handled in aliased_command() // .alias("r") - .setting(AppSettings::TrailingVarArg) + .trailing_var_arg(true) .about("Run a binary or example of the local package") .arg_quiet() .arg( diff --git a/src/bin/cargo/commands/rustc.rs b/src/bin/cargo/commands/rustc.rs index 2580732ff6b..a893fb35d70 100644 --- a/src/bin/cargo/commands/rustc.rs +++ b/src/bin/cargo/commands/rustc.rs @@ -7,7 +7,7 @@ const CRATE_TYPE_ARG_NAME: &str = "crate-type"; pub fn cli() -> App { subcommand("rustc") - .setting(AppSettings::TrailingVarArg) + .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")) diff --git a/src/bin/cargo/commands/rustdoc.rs b/src/bin/cargo/commands/rustdoc.rs index cdf6717dde1..304b1a42b2f 100644 --- a/src/bin/cargo/commands/rustdoc.rs +++ b/src/bin/cargo/commands/rustdoc.rs @@ -4,7 +4,7 @@ use crate::command_prelude::*; pub fn cli() -> App { subcommand("rustdoc") - .setting(AppSettings::TrailingVarArg) + .trailing_var_arg(true) .about("Build a package's documentation, using specified custom flags.") .arg_quiet() .arg(Arg::new("args").multiple_values(true)) diff --git a/src/bin/cargo/commands/test.rs b/src/bin/cargo/commands/test.rs index 23ecee0c351..7c8030803f8 100644 --- a/src/bin/cargo/commands/test.rs +++ b/src/bin/cargo/commands/test.rs @@ -6,7 +6,7 @@ pub fn cli() -> App { subcommand("test") // Subcommand aliases are handled in `aliased_command()`. // .alias("t") - .setting(AppSettings::TrailingVarArg) + .trailing_var_arg(true) .about("Execute all unit and integration tests and build examples of a local package") .arg( Arg::new("TESTNAME") diff --git a/src/cargo/util/command_prelude.rs b/src/cargo/util/command_prelude.rs index 635a2b17552..bdd076a33a0 100644 --- a/src/cargo/util/command_prelude.rs +++ b/src/cargo/util/command_prelude.rs @@ -22,7 +22,7 @@ pub use crate::core::compiler::CompileMode; pub use crate::{CliError, CliResult, Config}; pub use clap::{AppSettings, Arg, ArgMatches}; -pub type App = clap::App<'static>; +pub type App = clap::Command<'static>; pub trait AppExt: Sized { 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 } pub fn subcommand(name: &'static str) -> App { - App::new(name).setting(AppSettings::DeriveDisplayOrder | AppSettings::DontCollapseArgsInUsage) + App::new(name) + .dont_collapse_args_in_usage(true) + .setting(AppSettings::DeriveDisplayOrder) } /// Determines whether or not to gate `--profile` as unstable when resolving it. From dfda89a82e3112d4e1b35e0554aac95dc5878a26 Mon Sep 17 00:00:00 2001 From: Jon Gjengset Date: Wed, 16 Feb 2022 20:01:57 -0800 Subject: [PATCH 2/4] Bump clap to match when deprecations happened --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 60e1560cda8..fba5257a811 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -61,7 +61,7 @@ toml_edit = { version = "0.13.4", features = ["serde", "easy"] } unicode-xid = "0.2.0" url = "2.2.2" walkdir = "2.2" -clap = "3.0.13" +clap = "3.1.0" unicode-width = "0.1.5" openssl = { version = '0.10.11', optional = true } im-rc = "15.0.0" From 8f7ab523842fccfefdba6404bb630be36f8c0052 Mon Sep 17 00:00:00 2001 From: Jon Gjengset Date: Wed, 16 Feb 2022 20:03:00 -0800 Subject: [PATCH 3/4] Match semantics of SubcommandRequiredElseHelp --- src/bin/cargo/commands/config.rs | 1 + src/bin/cargo/commands/report.rs | 1 + 2 files changed, 2 insertions(+) diff --git a/src/bin/cargo/commands/config.rs b/src/bin/cargo/commands/config.rs index dde20036b90..2f204f18fa1 100644 --- a/src/bin/cargo/commands/config.rs +++ b/src/bin/cargo/commands/config.rs @@ -6,6 +6,7 @@ pub fn cli() -> App { .about("Inspect configuration values") .after_help("Run `cargo help config` for more detailed information.\n") .subcommand_required(true) + .arg_required_else_help(true) .subcommand( subcommand("get") .arg(Arg::new("key").help("The config key to display")) diff --git a/src/bin/cargo/commands/report.rs b/src/bin/cargo/commands/report.rs index 70018b98245..59becd4dced 100644 --- a/src/bin/cargo/commands/report.rs +++ b/src/bin/cargo/commands/report.rs @@ -7,6 +7,7 @@ pub fn cli() -> App { .about("Generate and display various kinds of reports") .after_help("Run `cargo help report` for more detailed information.\n") .subcommand_required(true) + .arg_required_else_help(true) .subcommand( subcommand("future-incompatibilities") .alias("future-incompat") From 6cbf913db3439b2e35ac42195478a1401bb6f29b Mon Sep 17 00:00:00 2001 From: Jon Gjengset Date: Wed, 16 Feb 2022 20:04:19 -0800 Subject: [PATCH 4/4] Tidy up subcommand logic slightly --- src/bin/cargo/cli.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/bin/cargo/cli.rs b/src/bin/cargo/cli.rs index 4b12881cf17..465d6abd2c0 100644 --- a/src/bin/cargo/cli.rs +++ b/src/bin/cargo/cli.rs @@ -44,10 +44,9 @@ pub fn main(config: &mut Config) -> CliResult { (ContextKind::InvalidSubcommand, &ContextValue::String(ref cmd)) => { Some(cmd) } - (ContextKind::InvalidSubcommand, _) => unreachable!(), _ => None, }) - .unwrap(); + .expect("UnrecognizedSubcommand implies the presence of InvalidSubcommand"); return super::execute_external_subcommand(config, &cmd, &[&cmd, "--help"]) .map_err(|_| e.into()); } else {