diff --git a/clap_builder/src/builder/command.rs b/clap_builder/src/builder/command.rs index 0f3668e5..4c749323 100644 --- a/clap_builder/src/builder/command.rs +++ b/clap_builder/src/builder/command.rs @@ -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) { self.bin_name = Some(name.into()); diff --git a/clap_builder/src/output/usage.rs b/clap_builder/src/output/usage.rs index 4bb901f8..81d56e1c 100644 --- a/clap_builder/src/output/usage.rs +++ b/clap_builder/src/output/usage.rs @@ -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 diff --git a/clap_builder/src/parser/parser.rs b/clap_builder/src/parser/parser.rs index 4b24eb0d..93616d68 100644 --- a/clap_builder/src/parser/parser.rs +++ b/clap_builder/src/parser/parser.rs @@ -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(&[]), ); diff --git a/clap_builder/src/parser/validator.rs b/clap_builder/src/parser/validator.rs index 17fb7c1e..55f4633c 100644 --- a/clap_builder/src/parser/validator.rs +++ b/clap_builder/src/parser/validator.rs @@ -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(),