From 3ff5033b722a463d64101b7ab4574e7f0eb83571 Mon Sep 17 00:00:00 2001 From: Ed Page Date: Thu, 9 Nov 2023 13:27:59 -0600 Subject: [PATCH] refactor: Pull out usage name fallback --- clap_builder/src/builder/command.rs | 13 +++++++++++++ clap_builder/src/output/usage.rs | 10 ++-------- 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/clap_builder/src/builder/command.rs b/clap_builder/src/builder/command.rs index 4c749323e129..00ddc7ce2ed5 100644 --- a/clap_builder/src/builder/command.rs +++ b/clap_builder/src/builder/command.rs @@ -3335,6 +3335,19 @@ 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"))] + 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 81d56e1c4a14..d4e932ce7f6d 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");