Skip to content

Commit

Permalink
Merge pull request #2681 from clap-rs/global-setting
Browse files Browse the repository at this point in the history
Remove settings that are inherently global
  • Loading branch information
pksunkara committed Aug 11, 2021
2 parents 69d7594 + 6ae15a7 commit 33c305e
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 27 deletions.
19 changes: 9 additions & 10 deletions src/build/app/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
33 changes: 19 additions & 14 deletions src/build/app/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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
///
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -713,25 +713,28 @@ 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
/// // "myprog-test v1.1"
/// ```
///
/// [`subcommands`]: crate::App::subcommand()
GlobalVersion,
PropagateVersion,

/// Specifies that this [`subcommand`] should be hidden from help messages
///
Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -1182,8 +1187,8 @@ mod test {
AppSettings::DeriveDisplayOrder
);
assert_eq!(
"globalversion".parse::<AppSettings>().unwrap(),
AppSettings::GlobalVersion
"propagateversion".parse::<AppSettings>().unwrap(),
AppSettings::PropagateVersion
);
assert_eq!(
"hidden".parse::<AppSettings>().unwrap(),
Expand Down
6 changes: 3 additions & 3 deletions src/build/app/tests.rs
Original file line number Diff line number Diff line change
@@ -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();
Expand Down

0 comments on commit 33c305e

Please sign in to comment.