Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refavtor: Prepare for help flattening #5205

Merged
merged 2 commits into from
Nov 9, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
refactor: Pull out usage name fallback
  • Loading branch information
epage committed Nov 9, 2023
commit 2142b136b879c37f54658e9f5a9bd0f929d4925e
14 changes: 14 additions & 0 deletions clap_builder/src/builder/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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> {
Expand Down
10 changes: 2 additions & 8 deletions clap_builder/src/output/usage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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!(
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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");
Expand Down
Loading