refactor: Pull out bin name fallback

This commit is contained in:
Ed Page 2023-11-09 13:26:20 -06:00
parent a920a7fe8d
commit 4b60cefbf1
4 changed files with 9 additions and 10 deletions

View file

@ -3347,6 +3347,12 @@ impl Command {
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`.
pub fn set_bin_name(&mut self, name: impl Into<String>) {
self.bin_name = Some(name.into());

View file

@ -214,8 +214,7 @@ 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())
.unwrap_or_else(|| self.cmd.get_bin_name_fallback())
}
// Determines if we need the `[OPTIONS]` tag in the usage string

View file

@ -501,10 +501,7 @@ impl<'cmd> Parser<'cmd> {
self.cmd,
arg_os.display().to_string(),
candidates,
self.cmd
.get_bin_name()
.unwrap_or_else(|| self.cmd.get_name())
.to_owned(),
self.cmd.get_bin_name_fallback().to_owned(),
suggested_trailing_arg,
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() {
let bn = self
.cmd
.get_bin_name()
.unwrap_or_else(|| self.cmd.get_name());
let bn = self.cmd.get_bin_name_fallback();
return Err(Error::missing_subcommand(
self.cmd,
bn.to_string(),