Merge pull request #4185 from epage/setting

refactor: Use parameters over settings
This commit is contained in:
Ed Page 2022-09-06 15:09:53 -05:00 committed by GitHub
commit f731ce70e4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 22 additions and 27 deletions

View file

@ -52,7 +52,6 @@ pub(crate) enum AppSettings {
Hidden,
HidePossibleValues,
HelpExpected,
ExpandHelpSubcommandTrees,
NoBinaryName,
#[allow(dead_code)]
ColorAuto,
@ -142,8 +141,6 @@ impl_settings! { AppSettings, AppFlags,
=> Flags::NO_POS_VALUES,
HelpExpected
=> Flags::HELP_REQUIRED,
ExpandHelpSubcommandTrees
=> Flags::EXPAND_HELP_SUBCOMMAND_TREES,
Hidden
=> Flags::HIDDEN,
Multicall

View file

@ -717,7 +717,7 @@ impl Command {
/// ```
/// [`io::stdout()`]: std::io::stdout()
pub fn print_help(&mut self) -> io::Result<()> {
self._build_self();
self._build_self(false);
let color = self.color_help();
let mut styled = StyledStr::new();
@ -744,7 +744,7 @@ impl Command {
/// [`-h` (short)]: Arg::help()
/// [`--help` (long)]: Arg::long_help()
pub fn print_long_help(&mut self) -> io::Result<()> {
self._build_self();
self._build_self(false);
let color = self.color_help();
let mut styled = StyledStr::new();
@ -772,7 +772,7 @@ impl Command {
/// [`-h` (short)]: Arg::help()
/// [`--help` (long)]: Arg::long_help()
pub fn write_help<W: io::Write>(&mut self, w: &mut W) -> io::Result<()> {
self._build_self();
self._build_self(false);
let mut styled = StyledStr::new();
let usage = Usage::new(self);
@ -798,7 +798,7 @@ impl Command {
/// [`-h` (short)]: Arg::help()
/// [`--help` (long)]: Arg::long_help()
pub fn write_long_help<W: io::Write>(&mut self, w: &mut W) -> io::Result<()> {
self._build_self();
self._build_self(false);
let mut styled = StyledStr::new();
let usage = Usage::new(self);
@ -872,7 +872,7 @@ impl Command {
pub(crate) fn render_usage_(&mut self) -> StyledStr {
// If there are global arguments, or settings we need to propagate them down to subcommands
// before parsing incase we run into a subcommand
self._build_self();
self._build_self(false);
Usage::new(self).create_usage_with_title(&[])
}
@ -3716,7 +3716,7 @@ impl Command {
// If there are global arguments, or settings we need to propagate them down to subcommands
// before parsing in case we run into a subcommand
self._build_self();
self._build_self(false);
let mut matcher = ArgMatcher::new(self);
@ -3743,23 +3743,18 @@ impl Command {
/// Call this on the top-level [`Command`] when done building and before reading state for
/// cases like completions, custom help output, etc.
pub fn build(&mut self) {
self._prepare_build_for_completion();
self._build_recursive();
self._build_recursive(true);
self._build_bin_names_internal();
}
fn _prepare_build_for_completion(&mut self) {
self.g_settings.set(AppSettings::ExpandHelpSubcommandTrees);
}
pub(crate) fn _build_recursive(&mut self) {
self._build_self();
pub(crate) fn _build_recursive(&mut self, expand_help_tree: bool) {
self._build_self(expand_help_tree);
for subcmd in self.get_subcommands_mut() {
subcmd._build_recursive();
subcmd._build_recursive(expand_help_tree);
}
}
pub(crate) fn _build_self(&mut self) {
pub(crate) fn _build_self(&mut self, expand_help_tree: bool) {
debug!("Command::_build: name={:?}", self.get_name());
if !self.settings.is_set(AppSettings::Built) {
// Make sure all the globally set flags apply to us as well
@ -3784,7 +3779,7 @@ impl Command {
}
self._propagate();
self._check_help_and_version();
self._check_help_and_version(expand_help_tree);
self._propagate_global_args();
let mut pos_counter = 1;
@ -3901,7 +3896,7 @@ impl Command {
}
// Ensure all args are built and ready to parse
sc._build_self();
sc._build_self(false);
Some(sc)
}
@ -4123,8 +4118,11 @@ impl Command {
}
}
pub(crate) fn _check_help_and_version(&mut self) {
debug!("Command::_check_help_and_version:{}", self.name,);
pub(crate) fn _check_help_and_version(&mut self, expand_help_tree: bool) {
debug!(
"Command::_check_help_and_version:{} expand_help_tree={}",
self.name, expand_help_tree
);
self.long_help_exists = self.long_help_exists_();
@ -4161,7 +4159,7 @@ impl Command {
debug!("Command::_check_help_and_version: Building help subcommand");
let help_about = "Print this message or the help of the given subcommand(s)";
let mut help_subcmd = if self.is_set(AppSettings::ExpandHelpSubcommandTrees) {
let mut help_subcmd = if expand_help_tree {
// Slow code path to recursively clone all other subcommand subtrees under help
let help_subcmd = Command::new("help")
.about(help_about)

View file

@ -39,7 +39,7 @@ fn issue_2090() {
let mut cmd = Command::new("cmd")
.disable_version_flag(true)
.subcommand(Command::new("sub"));
cmd._build_self();
cmd._build_self(false);
assert!(cmd
.get_subcommands()

View file

@ -74,7 +74,7 @@ impl<F: ErrorFormatter> Error<F> {
/// Format the existing message with the Command's context
#[must_use]
pub fn format(mut self, cmd: &mut Command) -> Self {
cmd._build_self();
cmd._build_self(false);
let usage = cmd.render_usage_();
if let Some(message) = self.inner.message.as_mut() {
message.format(cmd, usage);

View file

@ -51,7 +51,7 @@ where
None => subcommands
.into_iter()
.filter_map(|subcommand| {
subcommand._build_self();
subcommand._build_self(false);
let longs = subcommand.get_keymap().keys().filter_map(|a| {
if let KeyType::Long(v) = a {