mirror of
https://github.com/clap-rs/clap
synced 2024-12-14 14:52:33 +00:00
refactor: Pull out bin name fallback
This commit is contained in:
parent
a920a7fe8d
commit
4b60cefbf1
4 changed files with 9 additions and 10 deletions
|
@ -3347,6 +3347,12 @@ impl Command {
|
||||||
self.bin_name.as_deref()
|
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`.
|
/// Set binary name. Uses `&mut self` instead of `self`.
|
||||||
pub fn set_bin_name(&mut self, name: impl Into<String>) {
|
pub fn set_bin_name(&mut self, name: impl Into<String>) {
|
||||||
self.bin_name = Some(name.into());
|
self.bin_name = Some(name.into());
|
||||||
|
|
|
@ -214,8 +214,7 @@ impl<'cmd> Usage<'cmd> {
|
||||||
fn get_name(&self) -> &str {
|
fn get_name(&self) -> &str {
|
||||||
self.cmd
|
self.cmd
|
||||||
.get_usage_name()
|
.get_usage_name()
|
||||||
.or_else(|| self.cmd.get_bin_name())
|
.unwrap_or_else(|| self.cmd.get_bin_name_fallback())
|
||||||
.unwrap_or_else(|| self.cmd.get_name())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Determines if we need the `[OPTIONS]` tag in the usage string
|
// Determines if we need the `[OPTIONS]` tag in the usage string
|
||||||
|
|
|
@ -501,10 +501,7 @@ impl<'cmd> Parser<'cmd> {
|
||||||
self.cmd,
|
self.cmd,
|
||||||
arg_os.display().to_string(),
|
arg_os.display().to_string(),
|
||||||
candidates,
|
candidates,
|
||||||
self.cmd
|
self.cmd.get_bin_name_fallback().to_owned(),
|
||||||
.get_bin_name()
|
|
||||||
.unwrap_or_else(|| self.cmd.get_name())
|
|
||||||
.to_owned(),
|
|
||||||
suggested_trailing_arg,
|
suggested_trailing_arg,
|
||||||
Usage::new(self.cmd).create_usage_with_title(&[]),
|
Usage::new(self.cmd).create_usage_with_title(&[]),
|
||||||
);
|
);
|
||||||
|
|
|
@ -63,10 +63,7 @@ impl<'cmd> Validator<'cmd> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if !has_subcmd && self.cmd.is_subcommand_required_set() {
|
if !has_subcmd && self.cmd.is_subcommand_required_set() {
|
||||||
let bn = self
|
let bn = self.cmd.get_bin_name_fallback();
|
||||||
.cmd
|
|
||||||
.get_bin_name()
|
|
||||||
.unwrap_or_else(|| self.cmd.get_name());
|
|
||||||
return Err(Error::missing_subcommand(
|
return Err(Error::missing_subcommand(
|
||||||
self.cmd,
|
self.cmd,
|
||||||
bn.to_string(),
|
bn.to_string(),
|
||||||
|
|
Loading…
Reference in a new issue