Skip to content
This repository has been archived by the owner on Jan 1, 2022. It is now read-only.

Commit

Permalink
Merge pull request #59 from epage/disable-colored-help
Browse files Browse the repository at this point in the history
Add DisableColoredHelp setting to improve flexibility (clap-rs/clap#2956)
  • Loading branch information
epage authored Dec 4, 2021
2 parents f6635b0 + 4830c75 commit 592b9c9
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 8 deletions.
6 changes: 3 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
hands multiple times. Unfortunately, our changelog might be incomplete,
whether in changes or their motivation.

TBD:
- `AppSettings::ColoredHelp`: we are now relying solely on the `color` feature flag and `App::color` method

### Highlights

**[StructOpt](https://docs.rs/structopt/) Integration**
Expand Down Expand Up @@ -111,6 +108,9 @@ Subtle changes (i.e. compiler won't catch):
- `AppSettings::UnifiedHelpMessage` is now default behaviour
- `{flags}` and `{unified}` will assert if present in `App::help_template`
- See [clap-rs/clap#2807](https://github.com/clap-rs/clap/issues/2807)
- `AppSettings::EnableColoredHelp` is now the default behavior but can be
opted-out with `AppSettings::DisableColoredHelp`
([clap-rs/clap#2806](https://github.com/clap-rs/clap/issues/2806))
- `App::override_usage` no longer implies a leading `\t`, allowing multi lined usages
- `Arg::require_equals` no longer implies `ArgSettings::ForbidEmptyValues` ([#2233](https://github.com/clap-rs/clap/issues/2233))
- `Arg::require_delimiter` no longer implies `ArgSettings::TakesValue` and `ArgSettings::UseValueDelimiter` ([#2233](https://github.com/clap-rs/clap/issues/2233))
Expand Down
19 changes: 19 additions & 0 deletions src/build/app/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ bitflags! {
const NO_POS_VALUES = 1 << 17;
const NEXT_LINE_HELP = 1 << 18;
const DERIVE_DISP_ORDER = 1 << 19;
const DISABLE_COLORED_HELP = 1 << 20;
const COLOR_ALWAYS = 1 << 21;
const COLOR_AUTO = 1 << 22;
const COLOR_NEVER = 1 << 23;
Expand Down Expand Up @@ -99,6 +100,8 @@ impl_settings! { AppSettings, AppFlags,
=> Flags::DONT_COLLAPSE_ARGS,
DeriveDisplayOrder("derivedisplayorder")
=> Flags::DERIVE_DISP_ORDER,
DisableColoredHelp("disablecoloredhelp")
=> Flags::DISABLE_COLORED_HELP,
DisableHelpSubcommand("disablehelpsubcommand")
=> Flags::DISABLE_HELP_SC,
DisableHelpFlag("disablehelpflag")
Expand Down Expand Up @@ -571,6 +574,18 @@ pub enum AppSettings {
/// [`Arg::use_delimiter(false)`]: crate::Arg::use_delimiter()
DontDelimitTrailingValues,

/// Disables colorized help messages.
///
/// # Examples
///
/// ```no_run
/// # use clap::{App, AppSettings};
/// App::new("myprog")
/// .setting(AppSettings::DisableColoredHelp)
/// .get_matches();
/// ```
DisableColoredHelp,

/// Disables `-h` and `--help` flag.
///
/// # Examples
Expand Down Expand Up @@ -1194,6 +1209,10 @@ mod test {
"derivedisplayorder".parse::<AppSettings>().unwrap(),
AppSettings::DeriveDisplayOrder
);
assert_eq!(
"disablecoloredhelp".parse::<AppSettings>().unwrap(),
AppSettings::DisableColoredHelp
);
assert_eq!(
"propagateversion".parse::<AppSettings>().unwrap(),
AppSettings::PropagateVersion
Expand Down
3 changes: 2 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,14 @@
#[cfg(not(feature = "std"))]
compile_error!("`std` feature is currently required to build `clap`");

#[cfg(feature = "color")]
pub use crate::util::color::ColorChoice;
pub use crate::{
build::{
App, AppFlags, AppSettings, Arg, ArgFlags, ArgGroup, ArgSettings, PossibleValue, ValueHint,
},
parse::errors::{Error, ErrorKind, Result},
parse::{ArgMatches, Indices, OsValues, Values},
util::color::ColorChoice,
};

pub use crate::derive::{ArgEnum, Args, FromArgMatches, IntoApp, Parser, Subcommand};
Expand Down
18 changes: 14 additions & 4 deletions src/parse/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use crate::{
parse::features::suggestions,
parse::{ArgMatcher, SubCommand},
parse::{Validator, ValueType},
util::{ChildGraph, Id},
util::{color::ColorChoice, ChildGraph, Id},
INTERNAL_ERROR_MSG, INVALID_UTF8,
};

Expand Down Expand Up @@ -332,6 +332,16 @@ impl<'help, 'app> Parser<'help, 'app> {
}
}
}

// Should we color the help?
pub(crate) fn color_help(&self) -> ColorChoice {
#[cfg(feature = "color")]
if self.is_set(AS::DisableColoredHelp) {
return ColorChoice::Never;
}

self.app.get_color()
}
}

// Parsing Methods
Expand Down Expand Up @@ -1869,7 +1879,7 @@ impl<'help, 'app> Parser<'help, 'app> {
}