Skip to content

Commit

Permalink
refactor(parser): Pull out long-help determination
Browse files Browse the repository at this point in the history
This isn't a parser policy but command-level policy.
  • Loading branch information
epage committed Apr 21, 2022
1 parent a484f62 commit 639f9e8
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 25 deletions.
23 changes: 23 additions & 0 deletions src/build/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ use crate::output::{fmt::Colorizer, Help, HelpWriter, Usage};
use crate::parse::{ArgMatcher, ArgMatches, Parser};
use crate::util::ChildGraph;
use crate::util::{color::ColorChoice, Id, Key};
use crate::PossibleValue;
use crate::{Error, INTERNAL_ERROR_MSG};

#[cfg(debug_assertions)]
Expand Down Expand Up @@ -4675,6 +4676,28 @@ impl<'help> App<'help> {
pub(crate) fn get_display_order(&self) -> usize {
self.disp_ord.unwrap_or(999)
}

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
// original formatting, but also ensures that the actual -h or --help
// specified by the user is sent through. If hide_short_help is not included,
// then items specified with hidden_short_help will also be hidden.
let should_long = |v: &Arg| {
v.long_help.is_some()
|| v.is_hide_long_help_set()
|| v.is_hide_short_help_set()
|| cfg!(feature = "unstable-v4")
&& v.possible_vals.iter().any(PossibleValue::should_show_help)
};

// Subcommands aren't checked because we prefer short help for them, deferring to
// `cmd subcmd --help` for more.
self.get_long_about().is_some()
|| self.get_before_long_help().is_some()
|| self.get_after_long_help().is_some()
|| self.get_arguments().any(should_long)
}
}

impl<'help> Default for App<'help> {
Expand Down
28 changes: 3 additions & 25 deletions src/parse/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use std::{
use clap_lex::RawOsStr;

// Internal
use crate::build::AppSettings as AS;
use crate::build::{Arg, Command};
use crate::error::Error as ClapError;
use crate::error::Result as ClapResult;
Expand All @@ -17,7 +18,6 @@ use crate::parse::features::suggestions;
use crate::parse::{ArgMatcher, SubCommand};
use crate::parse::{Validator, ValueSource};
use crate::util::{color::ColorChoice, Id};
use crate::{build::AppSettings as AS, PossibleValue};
use crate::{INTERNAL_ERROR_MSG, INVALID_UTF8};

pub(crate) struct Parser<'help, 'cmd> {
Expand Down Expand Up @@ -805,28 +805,6 @@ impl<'help, 'cmd> Parser<'help, 'cmd> {
None
}

fn use_long_help(&self) -> bool {
debug!("Parser::use_long_help");
// In this case, both must be checked. This allows the retention of
// original formatting, but also ensures that the actual -h or --help
// specified by the user is sent through. If hide_short_help is not included,
// then items specified with hidden_short_help will also be hidden.
let should_long = |v: &Arg| {
v.long_help.is_some()
|| v.is_hide_long_help_set()
|| v.is_hide_short_help_set()
|| cfg!(feature = "unstable-v4")
&& v.possible_vals.iter().any(PossibleValue::should_show_help)
};

// Subcommands aren't checked because we prefer short help for them, deferring to
// `cmd subcmd --help` for more.
self.cmd.get_long_about().is_some()
|| self.cmd.get_before_long_help().is_some()
|| self.cmd.get_after_long_help().is_some()
|| self.cmd.get_arguments().any(should_long)
}

fn parse_long_arg(
&mut self,
matcher: &mut ArgMatcher,
Expand Down Expand Up @@ -1539,10 +1517,10 @@ impl<'help, 'cmd> Parser<'help, 'cmd> {
fn help_err(&self, mut use_long: bool) -> ClapError {
debug!(
"Parser::help_err: use_long={:?}",
use_long && self.use_long_help()
use_long && self.cmd.use_long_help()
);

use_long = use_long && self.use_long_help();
use_long = use_long && self.cmd.use_long_help();
let usage = Usage::new(self.cmd);
let mut c = Colorizer::new(false, self.color_help());

Expand Down

0 comments on commit 639f9e8

Please sign in to comment.