Skip to content

Commit

Permalink
refactor: Pull out bin name fallback
Browse files Browse the repository at this point in the history
  • Loading branch information
epage committed Nov 9, 2023
1 parent a920a7f commit 4b60cef
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 10 deletions.
6 changes: 6 additions & 0 deletions clap_builder/src/builder/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
3 changes: 1 addition & 2 deletions clap_builder/src/output/usage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 1 addition & 4 deletions clap_builder/src/parser/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(&[]),
);
Expand Down
5 changes: 1 addition & 4 deletions clap_builder/src/parser/validator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down

0 comments on commit 4b60cef

Please sign in to comment.