fix: Allow using deprecated color AppSettings

This partiall reverts commit efeb02cd34,
bringing back the `AppSettins::Color*` and making them the backing store
for `App::color`, to help ease the transition from clap2->3.

Once we remove these deprecated settings, we might want to keep this
backing store to save on memory.

This is a part of #2617
This commit is contained in:
Ed Page 2021-10-18 08:52:59 -05:00
parent 88b89317ab
commit 99058014d8
2 changed files with 43 additions and 6 deletions

View file

@ -90,7 +90,6 @@ pub struct App<'help> {
pub(crate) template: Option<&'help str>,
pub(crate) settings: AppFlags,
pub(crate) g_settings: AppFlags,
pub(crate) color: ColorChoice,
pub(crate) args: MKeyMap<'help>,
pub(crate) subcommands: Vec<App<'help>>,
pub(crate) replacers: HashMap<&'help str, &'help [&'help str]>,
@ -1021,9 +1020,13 @@ impl<'help> App<'help> {
/// [`ColorChoice::Auto`]: crate::ColorChoice::Auto
#[cfg(feature = "color")]
#[inline]
pub fn color(mut self, color: ColorChoice) -> Self {
self.color = color;
self
pub fn color(self, color: ColorChoice) -> Self {
#[allow(deprecated)]
match color {
ColorChoice::Auto => self.setting(AppSettings::ColorAuto),
ColorChoice::Always => self.setting(AppSettings::ColorAlways),
ColorChoice::Never => self.setting(AppSettings::ColorNever),
}
}
/// Sets the terminal width at which to wrap help messages. Defaults to
@ -2818,8 +2821,21 @@ impl<'help> App<'help> {
}
#[inline]
// Should we color the output?
pub(crate) fn get_color(&self) -> ColorChoice {
self.color
debug!("App::color: Color setting...");
#[allow(deprecated)]
if self.is_set(AppSettings::ColorNever) {
debug!("Never");
ColorChoice::Never
} else if self.is_set(AppSettings::ColorAlways) {
debug!("Always");
ColorChoice::Always
} else {
debug!("Auto");
ColorChoice::Auto
}
}
#[inline]

View file

@ -25,6 +25,9 @@ bitflags! {
const NO_POS_VALUES = 1 << 17;
const NEXT_LINE_HELP = 1 << 18;
const DERIVE_DISP_ORDER = 1 << 19;
const COLOR_ALWAYS = 1 << 21;
const COLOR_AUTO = 1 << 22;
const COLOR_NEVER = 1 << 23;
const DONT_DELIM_TRAIL = 1 << 24;
const ALLOW_NEG_NUMS = 1 << 25;
const DISABLE_HELP_SC = 1 << 27;
@ -56,7 +59,7 @@ pub struct AppFlags(Flags);
impl Default for AppFlags {
fn default() -> Self {
Self::empty()
AppFlags(Flags::COLOR_AUTO)
}
}
@ -77,6 +80,12 @@ impl_settings! { AppSettings, AppFlags,
=> Flags::ALLOW_NEG_NUMS,
AllowMissingPositional("allowmissingpositional")
=> Flags::ALLOW_MISSING_POS,
ColorAlways("coloralways")
=> Flags::COLOR_ALWAYS,
ColorAuto("colorauto")
=> Flags::COLOR_AUTO,
ColorNever("colornever")
=> Flags::COLOR_NEVER,
DontDelimitTrailingValues("dontdelimittrailingvalues")
=> Flags::DONT_DELIM_TRAIL,
DontCollapseArgsInUsage("dontcollapseargsinusage")
@ -489,6 +498,18 @@ pub enum AppSettings {
/// ```
SubcommandPrecedenceOverArg,
/// Deprecated, see [`App::color`]
#[deprecated(since = "3.0.0", note = "Replaced with `App::color`")]
ColorAuto,
/// Deprecated, see [`App::color`]
#[deprecated(since = "3.0.0", note = "Replaced with `App::color`")]
ColorAlways,
/// Deprecated, see [`App::color`]
#[deprecated(since = "3.0.0", note = "Replaced with `App::color`")]
ColorNever,
/// Disables the automatic collapsing of positional args into `[ARGS]` inside the usage string
///
/// # Examples