From 4b60cefbf1cea6e8161226dd5fa8bc22fea72e93 Mon Sep 17 00:00:00 2001 From: Ed Page Date: Thu, 9 Nov 2023 13:26:20 -0600 Subject: [PATCH 1/2] refactor: Pull out bin name fallback --- clap_builder/src/builder/command.rs | 6 ++++++ clap_builder/src/output/usage.rs | 3 +-- clap_builder/src/parser/parser.rs | 5 +---- clap_builder/src/parser/validator.rs | 5 +---- 4 files changed, 9 insertions(+), 10 deletions(-) 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(), From 2142b136b879c37f54658e9f5a9bd0f929d4925e Mon Sep 17 00:00:00 2001 From: Ed Page Date: Thu, 9 Nov 2023 13:27:59 -0600 Subject: [PATCH 2/2] refactor: Pull out usage name fallback --- clap_builder/src/builder/command.rs | 14 ++++++++++++++ clap_builder/src/output/usage.rs | 10 ++-------- 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/clap_builder/src/builder/command.rs b/clap_builder/src/builder/command.rs index 4c749323..264649c7 100644 --- a/clap_builder/src/builder/command.rs +++ b/clap_builder/src/builder/command.rs @@ -3335,6 +3335,20 @@ impl Command { 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. #[inline] pub fn get_display_name(&self) -> Option<&str> { diff --git a/clap_builder/src/output/usage.rs b/clap_builder/src/output/usage.rs index 81d56e1c..d4e932ce 100644 --- a/clap_builder/src/output/usage.rs +++ b/clap_builder/src/output/usage.rs @@ -135,7 +135,7 @@ impl<'cmd> Usage<'cmd> { let literal = &self.styles.get_literal(); 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() { // the trim won't properly remove a leading space due to the formatting let _ = write!( @@ -176,7 +176,7 @@ impl<'cmd> Usage<'cmd> { styled.trim_end(); let _ = write!(styled, "{}", USAGE_SEP); 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 let _ = write!( styled, @@ -211,12 +211,6 @@ impl<'cmd> Usage<'cmd> { } } - fn get_name(&self) -> &str { - self.cmd - .get_usage_name() - .unwrap_or_else(|| self.cmd.get_bin_name_fallback()) - } - // Determines if we need the `[OPTIONS]` tag in the usage string fn needs_options_tag(&self) -> bool { debug!("Usage::needs_options_tag");