Merge pull request #5205 from epage/flatten-refactor

refavtor: Prepare for help flattening
This commit is contained in:
Ed Page 2023-11-09 15:20:21 -06:00 committed by GitHub
commit 3383242b47
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 24 additions and 17 deletions

View file

@ -3335,6 +3335,20 @@ impl Command {
self.usage_name.as_deref() self.usage_name.as_deref()
} }
#[inline]
#[cfg(feature = "usage")]
pub(crate) fn get_usage_name_fallback(&self) -> &str {
self.get_usage_name()
.unwrap_or_else(|| self.get_bin_name_fallback())
}
#[inline]
#[cfg(not(feature = "usage"))]
#[allow(dead_code)]
pub(crate) fn get_usage_name_fallback(&self) -> &str {
self.get_bin_name_fallback()
}
/// Get the name of the binary. /// Get the name of the binary.
#[inline] #[inline]
pub fn get_display_name(&self) -> Option<&str> { pub fn get_display_name(&self) -> Option<&str> {
@ -3347,6 +3361,12 @@ impl Command {
self.bin_name.as_deref() self.bin_name.as_deref()
} }
/// Get the name of the binary.
#[inline]
pub(crate) fn get_bin_name_fallback(&self) -> &str {
self.bin_name.as_deref().unwrap_or_else(|| self.get_name())
}
/// Set binary name. Uses `&mut self` instead of `self`. /// Set binary name. Uses `&mut self` instead of `self`.
pub fn set_bin_name(&mut self, name: impl Into<String>) { pub fn set_bin_name(&mut self, name: impl Into<String>) {
self.bin_name = Some(name.into()); self.bin_name = Some(name.into());

View file

@ -135,7 +135,7 @@ impl<'cmd> Usage<'cmd> {
let literal = &self.styles.get_literal(); let literal = &self.styles.get_literal();
let placeholder = &self.styles.get_placeholder(); let placeholder = &self.styles.get_placeholder();
let bin_name = self.get_name(); let bin_name = self.cmd.get_usage_name_fallback();
if !bin_name.is_empty() { if !bin_name.is_empty() {
// the trim won't properly remove a leading space due to the formatting // the trim won't properly remove a leading space due to the formatting
let _ = write!( let _ = write!(
@ -176,7 +176,7 @@ impl<'cmd> Usage<'cmd> {
styled.trim_end(); styled.trim_end();
let _ = write!(styled, "{}", USAGE_SEP); let _ = write!(styled, "{}", USAGE_SEP);
if self.cmd.is_args_conflicts_with_subcommands_set() { if self.cmd.is_args_conflicts_with_subcommands_set() {
let bin_name = self.get_name(); let bin_name = self.cmd.get_usage_name_fallback();
// Short-circuit full usage creation since no args will be relevant // Short-circuit full usage creation since no args will be relevant
let _ = write!( let _ = write!(
styled, styled,
@ -211,13 +211,6 @@ impl<'cmd> Usage<'cmd> {
} }
} }
fn get_name(&self) -> &str {
self.cmd
.get_usage_name()
.or_else(|| self.cmd.get_bin_name())
.unwrap_or_else(|| self.cmd.get_name())
}
// Determines if we need the `[OPTIONS]` tag in the usage string // Determines if we need the `[OPTIONS]` tag in the usage string
fn needs_options_tag(&self) -> bool { fn needs_options_tag(&self) -> bool {
debug!("Usage::needs_options_tag"); debug!("Usage::needs_options_tag");

View file

@ -501,10 +501,7 @@ impl<'cmd> Parser<'cmd> {
self.cmd, self.cmd,
arg_os.display().to_string(), arg_os.display().to_string(),
candidates, candidates,
self.cmd self.cmd.get_bin_name_fallback().to_owned(),
.get_bin_name()
.unwrap_or_else(|| self.cmd.get_name())
.to_owned(),
suggested_trailing_arg, suggested_trailing_arg,
Usage::new(self.cmd).create_usage_with_title(&[]), Usage::new(self.cmd).create_usage_with_title(&[]),
); );

View file

@ -63,10 +63,7 @@ impl<'cmd> Validator<'cmd> {
} }
} }
if !has_subcmd && self.cmd.is_subcommand_required_set() { if !has_subcmd && self.cmd.is_subcommand_required_set() {
let bn = self let bn = self.cmd.get_bin_name_fallback();
.cmd
.get_bin_name()
.unwrap_or_else(|| self.cmd.get_name());
return Err(Error::missing_subcommand( return Err(Error::missing_subcommand(
self.cmd, self.cmd,
bn.to_string(), bn.to_string(),