diff --git a/src/build/command.rs b/src/build/command.rs index 71fcda65427..7cd03ffc842 100644 --- a/src/build/command.rs +++ b/src/build/command.rs @@ -4678,6 +4678,25 @@ impl<'help> App<'help> { self.disp_ord.unwrap_or(999) } + pub(crate) fn write_help_err( + &self, + mut use_long: bool, + stream: Stream, + ) -> ClapResult { + debug!( + "Parser::write_help_err: use_long={:?}, stream={:?}", + use_long && self.use_long_help(), + stream + ); + + use_long = use_long && self.use_long_help(); + let usage = Usage::new(self); + + let mut c = Colorizer::new(stream, self.color_help()); + Help::new(HelpWriter::Buffer(&mut c), self, &usage, use_long).write_help()?; + Ok(c) + } + pub(crate) fn use_long_help(&self) -> bool { debug!("Command::use_long_help"); // In this case, both must be checked. This allows the retention of @@ -4699,6 +4718,16 @@ impl<'help> App<'help> { || self.get_after_long_help().is_some() || self.get_arguments().any(should_long) } + + // Should we color the help? + pub(crate) fn color_help(&self) -> ColorChoice { + #[cfg(feature = "color")] + if self.is_disable_colored_help_set() { + return ColorChoice::Never; + } + + self.get_color() + } } impl<'help> Default for App<'help> { diff --git a/src/parse/parser.rs b/src/parse/parser.rs index 8d10f81b16f..c6b83a06f3d 100644 --- a/src/parse/parser.rs +++ b/src/parse/parser.rs @@ -14,11 +14,11 @@ use crate::error::Error as ClapError; use crate::error::Result as ClapResult; use crate::mkeymap::KeyType; use crate::output::fmt::Stream; -use crate::output::{fmt::Colorizer, Help, HelpWriter, Usage}; +use crate::output::{fmt::Colorizer, Usage}; use crate::parse::features::suggestions; use crate::parse::{ArgMatcher, SubCommand}; use crate::parse::{Validator, ValueSource}; -use crate::util::{color::ColorChoice, Id}; +use crate::util::Id; use crate::{INTERNAL_ERROR_MSG, INVALID_UTF8}; pub(crate) struct Parser<'help, 'cmd> { @@ -43,16 +43,6 @@ impl<'help, 'cmd> Parser<'help, 'cmd> { flag_subcmd_skip: 0, } } - - // Should we color the help? - pub(crate) fn color_help(&self) -> ColorChoice { - #[cfg(feature = "color")] - if self.cmd.is_disable_colored_help_set() { - return ColorChoice::Never; - } - - self.cmd.get_color() - } } // Parsing Methods @@ -1508,27 +1498,8 @@ impl<'help, 'cmd> Parser<'help, 'cmd> { ) } - pub(crate) fn write_help_err( - &self, - mut use_long: bool, - stream: Stream, - ) -> ClapResult { - debug!( - "Parser::write_help_err: use_long={:?}, stream={:?}", - use_long && self.cmd.use_long_help(), - stream - ); - - use_long = use_long && self.cmd.use_long_help(); - let usage = Usage::new(self.cmd); - - let mut c = Colorizer::new(stream, self.color_help()); - Help::new(HelpWriter::Buffer(&mut c), self.cmd, &usage, use_long).write_help()?; - Ok(c) - } - fn help_err(&self, use_long: bool, stream: Stream) -> ClapError { - match self.write_help_err(use_long, stream) { + match self.cmd.write_help_err(use_long, stream) { Ok(c) => ClapError::display_help(self.cmd, c), Err(e) => e, } @@ -1538,7 +1509,7 @@ impl<'help, 'cmd> Parser<'help, 'cmd> { debug!("Parser::version_err"); let msg = self.cmd._render_version(use_long); - let mut c = Colorizer::new(Stream::Stdout, self.color_help()); + let mut c = Colorizer::new(Stream::Stdout, self.cmd.color_help()); c.none(msg); ClapError::display_version(self.cmd, c) } diff --git a/src/parse/validator.rs b/src/parse/validator.rs index 77895a57198..f3f902ce592 100644 --- a/src/parse/validator.rs +++ b/src/parse/validator.rs @@ -65,7 +65,7 @@ impl<'help, 'cmd, 'parser> Validator<'help, 'cmd, 'parser> { .filter(|arg_id| matcher.check_explicit(arg_id, ArgPredicate::IsPresent)) .count(); if num_user_values == 0 { - let message = self.p.write_help_err(false, Stream::Stderr)?; + let message = self.p.cmd.write_help_err(false, Stream::Stderr)?; return Err(Error::display_help_error(self.p.cmd, message)); } } @@ -85,7 +85,7 @@ impl<'help, 'cmd, 'parser> Validator<'help, 'cmd, 'parser> { )); } else if !has_subcmd && self.p.cmd.is_set(AppSettings::SubcommandRequiredElseHelp) { debug!("Validator::new::get_matches_with: SubcommandRequiredElseHelp=true"); - let message = self.p.write_help_err(false, Stream::Stderr)?; + let message = self.p.cmd.write_help_err(false, Stream::Stderr)?; return Err(Error::display_help_error(self.p.cmd, message)); }