From 6ae15a79b63efeb1c18d65ad60b431c6f384dc3d Mon Sep 17 00:00:00 2001 From: Pavan Kumar Sunkara Date: Wed, 11 Aug 2021 22:41:25 +0100 Subject: [PATCH] Remove settings that are inherently global --- src/build/app/mod.rs | 19 +++++++++---------- src/build/app/settings.rs | 33 +++++++++++++++++++-------------- src/build/app/tests.rs | 6 +++--- 3 files changed, 31 insertions(+), 27 deletions(-) diff --git a/src/build/app/mod.rs b/src/build/app/mod.rs index 5ac56422db1..87388acac40 100644 --- a/src/build/app/mod.rs +++ b/src/build/app/mod.rs @@ -2373,21 +2373,20 @@ impl<'help> App<'help> { // We have to create a new scope in order to tell rustc the borrow of `sc` is // done and to recursively call this method { - let vsc = $_self + if $_self .settings - .is_set(AppSettings::DisableVersionForSubcommands); - let gv = $_self.settings.is_set(AppSettings::GlobalVersion); - - if vsc { + .is_set(AppSettings::DisableVersionForSubcommands) + { $sc.set(AppSettings::DisableVersionFlag); } - if gv && $sc.version.is_none() && $_self.version.is_some() { - $sc.set(AppSettings::GlobalVersion); + + if $_self.settings.is_set(AppSettings::PropagateVersion) + && $sc.version.is_none() + && $_self.version.is_some() + { $sc.version = Some($_self.version.unwrap()); } - if $_self.settings.is_set(AppSettings::IgnoreErrors) { - $sc.set(AppSettings::IgnoreErrors); - } + $sc.settings = $sc.settings | $_self.g_settings; $sc.g_settings = $sc.g_settings | $_self.g_settings; $sc.term_w = $_self.term_w; diff --git a/src/build/app/settings.rs b/src/build/app/settings.rs index 1d788637e54..e8cbb9d823e 100644 --- a/src/build/app/settings.rs +++ b/src/build/app/settings.rs @@ -9,7 +9,7 @@ bitflags! { const SC_NEGATE_REQS = 1; const SC_REQUIRED = 1 << 1; const ARG_REQUIRED_ELSE_HELP = 1 << 2; - const GLOBAL_VERSION = 1 << 3; + const PROPAGATE_VERSION = 1 << 3; const DISABLE_VERSION_FOR_SC = 1 << 4; const UNIFIED_HELP = 1 << 5; const WAIT_ON_ERROR = 1 << 6; @@ -110,8 +110,8 @@ impl_settings! { AppSettings, AppFlags, => Flags::DISABLE_HELP_FLAG, DisableVersionFlag("disableversionflag") => Flags::DISABLE_VERSION_FLAG, - GlobalVersion("globalversion") - => Flags::GLOBAL_VERSION, + PropagateVersion("propagateversion") + => Flags::PROPAGATE_VERSION, HidePossibleValuesInHelp("hidepossiblevaluesinhelp") => Flags::NO_POS_VALUES, HelpRequired("helprequired") @@ -469,8 +469,8 @@ pub enum AppSettings { /// values subcommand /// ``` /// - /// **Note:** Make sure you apply it as `global_setting` if you want it to be propagated to - /// sub-sub commands! + /// **Note:** Make sure you apply it as `global_setting` if you want this setting + /// to be propagated to subcommands and sub-subcommands! /// /// # Examples /// @@ -588,7 +588,7 @@ pub enum AppSettings { /// ```no_run /// # use clap::{App, Arg, AppSettings}; /// App::new("myprog") - /// .global_setting(AppSettings::DisableEnv) + /// .setting(AppSettings::DisableEnv) /// .get_matches(); /// ``` DisableEnv, @@ -713,17 +713,20 @@ pub enum AppSettings { /// [`subcommands`]: crate::App::subcommand() DeriveDisplayOrder, - /// Specifies to use the version of the current command for all child [`subcommands`]. + /// Specifies to use the version of the current command for all [`subcommands`]. /// /// Defaults to `false`; subcommands have independent version strings from their parents. /// + /// **Note:** Make sure you apply it as `global_setting` if you want this setting + /// to be propagated to subcommands and sub-subcommands! + /// /// # Examples /// /// ```no_run /// # use clap::{App, Arg, AppSettings}; /// App::new("myprog") /// .version("v1.1") - /// .setting(AppSettings::GlobalVersion) + /// .setting(AppSettings::PropagateVersion) /// .subcommand(App::new("test")) /// .get_matches(); /// // running `$ myprog test --version` will display @@ -731,7 +734,7 @@ pub enum AppSettings { /// ``` /// /// [`subcommands`]: crate::App::subcommand() - GlobalVersion, + PropagateVersion, /// Specifies that this [`subcommand`] should be hidden from help messages /// @@ -783,8 +786,10 @@ pub enum AppSettings { ///``` HelpRequired, - /// Try not to fail on parse errors like missing option values. This is a - /// global option that gets propagated sub commands. + /// Try not to fail on parse errors like missing option values. + /// + /// **Note:** Make sure you apply it as `global_setting` if you want this setting + /// to be propagated to subcommands and sub-subcommands! /// /// Issue: [#1880 Partial / Pre Parsing a /// CLI](https://github.com/clap-rs/clap/issues/1880) @@ -793,7 +798,7 @@ pub enum AppSettings { /// /// * [Changing app settings based on /// flags](https://github.com/clap-rs/clap/issues/1880#issuecomment-637779787) - /// * [#1232 Dynamic completion + /// * [#1232 Dynamic completion /// support](https://github.com/clap-rs/clap/issues/1232) /// /// Support is not complete: Errors are still possible but they can be @@ -1182,8 +1187,8 @@ mod test { AppSettings::DeriveDisplayOrder ); assert_eq!( - "globalversion".parse::().unwrap(), - AppSettings::GlobalVersion + "propagateversion".parse::().unwrap(), + AppSettings::PropagateVersion ); assert_eq!( "hidden".parse::().unwrap(), diff --git a/src/build/app/tests.rs b/src/build/app/tests.rs index 62cf2b46cc7..59639f97e8b 100644 --- a/src/build/app/tests.rs +++ b/src/build/app/tests.rs @@ -1,9 +1,9 @@ use crate::{App, AppSettings}; #[test] -fn global_version() { - let mut app = App::new("global_version") - .setting(AppSettings::GlobalVersion) +fn propagate_version() { + let mut app = App::new("test") + .setting(AppSettings::PropagateVersion) .version("1.1") .subcommand(App::new("sub1")); app._propagate();