Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add DisableColoredHelp setting to improve flexibility #2956

Merged
merged 1 commit into from
Dec 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -97,6 +98,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 @@ -558,6 +561,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 @@ -1173,6 +1188,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 @@ -1866,7 +1876,7 @@ impl<'help, 'app> Parser<'help, 'app> {
}

pub(crate) fn write_help_err(&self) -> ClapResult<Colorizer> {
let mut c = Colorizer::new(true, self.app.get_color());
let mut c = Colorizer::new(true, self.color_help());
Help::new(HelpWriter::Buffer(&mut c), self, false).write_help()?;
Ok(c)
}
Expand All @@ -1878,7 +1888,7 @@ impl<'help, 'app> Parser<'help, 'app> {
);

use_long = use_long && self.use_long_help();
let mut c = Colorizer::new(false, self.app.get_color());
let mut c = Colorizer::new(false, self.color_help());

match Help::new(HelpWriter::Buffer(&mut c), self, use_long).write_help() {
Err(e) => e.into(),
Expand All @@ -1894,7 +1904,7 @@ impl<'help, 'app> Parser<'help, 'app> {
debug!("Parser::version_err");

let msg = self.app._render_version(use_long);
let mut c = Colorizer::new(false, self.app.get_color());
let mut c = Colorizer::new(false, self.color_help());
c.none(msg);
ClapError::new(
c,
Expand Down