From 0ceba231c6767cd6d88fdb1feeeea41deadf77ff Mon Sep 17 00:00:00 2001 From: Barret Rennie Date: Sat, 23 Jul 2016 22:08:24 -0600 Subject: [PATCH] feat(Settings): Add unset_setting and unset_settings fns to App (#598) Closes #590. --- src/app/mod.rs | 41 +++++++++++++++++++++++++++++++++++++++++ tests/app_settings.rs | 21 +++++++++++++++++++++ 2 files changed, 62 insertions(+) diff --git a/src/app/mod.rs b/src/app/mod.rs index 7446b97b..4458d72a 100644 --- a/src/app/mod.rs +++ b/src/app/mod.rs @@ -484,6 +484,47 @@ impl<'a, 'b> App<'a, 'b> { self } + /// Disables a single command, or [`SubCommand`], level setting. + /// + /// See [`AppSettings`] for a full list of possibilities and examples. + /// + /// # Examples + /// + /// ```no_run + /// # use clap::{App, AppSettings}; + /// App::new("myprog") + /// .unset_setting(AppSettings::ColorAuto) + /// # ; + /// ``` + /// [`SubCommand`]: ./struct.SubCommand.html + /// [`AppSettings`]: ./enum.AppSettings.html + pub fn unset_setting(mut self, setting: AppSettings) -> Self { + self.p.unset(setting); + self + } + + /// Disables multiple command, or [`SubCommand`], level settings. + /// + /// See [`AppSettings`] for a full list of possibilities and examples. + /// + /// # Examples + /// + /// ```no_run + /// # use clap::{App, AppSettings}; + /// App::new("myprog") + /// .unset_settings(&[AppSettings::ColorAuto, + /// AppSettings::AllowInvalidUtf8]) + /// # ; + /// ``` + /// [`SubCommand`]: ./struct.SubCommand.html + /// [`AppSettings`]: ./enum.AppSettings.html + pub fn unset_settings(mut self, settings: &[AppSettings]) -> Self { + for s in settings { + self.p.unset(*s); + } + self + } + /// Sets the terminal width at which to wrap help messages. Defaults to `120`. /// /// `clap` automatically tries to determine the terminal width on Unix, Linux, and OSX if the diff --git a/tests/app_settings.rs b/tests/app_settings.rs index 24d4ebf0..54911b18 100644 --- a/tests/app_settings.rs +++ b/tests/app_settings.rs @@ -247,3 +247,24 @@ fn leading_double_hyphen_trailingvararg() { assert!(m.is_present("opt")); assert_eq!(m.values_of("opt").unwrap().collect::>(), &["--foo", "-Wl", "bar"]); } + +#[test] +fn test_unset_setting() { + let m = App::new("unset_setting"); + assert!(m.p.is_set(AppSettings::AllowInvalidUtf8)); + + let m = m.unset_setting(AppSettings::AllowInvalidUtf8); + assert!(!m.p.is_set(AppSettings::AllowInvalidUtf8)); +} + +#[test] +fn test_unset_settings() { + let m = App::new("unset_settings"); + assert!(&m.p.is_set(AppSettings::AllowInvalidUtf8)); + assert!(&m.p.is_set(AppSettings::ColorAuto)); + + let m = m.unset_settings(&[AppSettings::AllowInvalidUtf8, + AppSettings::ColorAuto]); + assert!(!m.p.is_set(AppSettings::AllowInvalidUtf8)); + assert!(!m.p.is_set(AppSettings::ColorAuto)); +}