mirror of
https://github.com/clap-rs/clap
synced 2025-01-05 17:28:42 +00:00
refactor(help): Simplify the code
While this doesn't reduce size, it cleans things up to make it easier
This commit is contained in:
parent
64753bbc7d
commit
8607695ed9
3 changed files with 35 additions and 40 deletions
|
@ -4,7 +4,6 @@ use std::env;
|
||||||
#[cfg(feature = "env")]
|
#[cfg(feature = "env")]
|
||||||
use std::ffi::OsString;
|
use std::ffi::OsString;
|
||||||
use std::{
|
use std::{
|
||||||
borrow::Cow,
|
|
||||||
cmp::{Ord, Ordering},
|
cmp::{Ord, Ordering},
|
||||||
fmt::{self, Display, Formatter},
|
fmt::{self, Display, Formatter},
|
||||||
str,
|
str,
|
||||||
|
@ -3951,26 +3950,28 @@ impl Arg {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Used for positionals when printing
|
// Used for positionals when printing
|
||||||
pub(crate) fn name_no_brackets(&self) -> Cow<str> {
|
pub(crate) fn name_no_brackets(&self) -> String {
|
||||||
debug!("Arg::name_no_brackets:{}", self.get_id());
|
debug!("Arg::name_no_brackets:{}", self.get_id());
|
||||||
let delim = " ";
|
let delim = " ";
|
||||||
if !self.val_names.is_empty() {
|
if !self.val_names.is_empty() {
|
||||||
debug!("Arg::name_no_brackets: val_names={:#?}", self.val_names);
|
debug!("Arg::name_no_brackets: val_names={:#?}", self.val_names);
|
||||||
|
|
||||||
if self.val_names.len() > 1 {
|
if self.val_names.len() > 1 {
|
||||||
Cow::Owned(
|
self.val_names
|
||||||
self.val_names
|
.iter()
|
||||||
.iter()
|
.map(|n| format!("<{}>", n))
|
||||||
.map(|n| format!("<{}>", n))
|
.collect::<Vec<_>>()
|
||||||
.collect::<Vec<_>>()
|
.join(delim)
|
||||||
.join(delim),
|
|
||||||
)
|
|
||||||
} else {
|
} else {
|
||||||
Cow::Borrowed(self.val_names.first().expect(INTERNAL_ERROR_MSG))
|
self.val_names
|
||||||
|
.first()
|
||||||
|
.expect(INTERNAL_ERROR_MSG)
|
||||||
|
.as_str()
|
||||||
|
.to_owned()
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
debug!("Arg::name_no_brackets: just name");
|
debug!("Arg::name_no_brackets: just name");
|
||||||
Cow::Borrowed(self.get_id().as_str())
|
self.get_id().as_str().to_owned()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -4190,7 +4190,7 @@ impl Command {
|
||||||
.map(|x| {
|
.map(|x| {
|
||||||
if x.is_positional() {
|
if x.is_positional() {
|
||||||
// Print val_name for positional arguments. e.g. <file_name>
|
// Print val_name for positional arguments. e.g. <file_name>
|
||||||
x.name_no_brackets().to_string()
|
x.name_no_brackets()
|
||||||
} else {
|
} else {
|
||||||
// Print usage string for flags arguments, e.g. <--help>
|
// Print usage string for flags arguments, e.g. <--help>
|
||||||
x.to_string()
|
x.to_string()
|
||||||
|
|
|
@ -503,10 +503,11 @@ impl<'cmd, 'writer> Help<'cmd, 'writer> {
|
||||||
"={}",
|
"={}",
|
||||||
env.1
|
env.1
|
||||||
.as_ref()
|
.as_ref()
|
||||||
.map_or(Cow::Borrowed(""), |val| val.to_string_lossy())
|
.map(|s| s.to_string_lossy())
|
||||||
|
.unwrap_or_default()
|
||||||
)
|
)
|
||||||
} else {
|
} else {
|
||||||
String::new()
|
Default::default()
|
||||||
};
|
};
|
||||||
let env_info = format!("[env: {}{}]", env.0.to_string_lossy(), env_val);
|
let env_info = format!("[env: {}{}]", env.0.to_string_lossy(), env_val);
|
||||||
spec_vals.push(env_info);
|
spec_vals.push(env_info);
|
||||||
|
@ -534,39 +535,32 @@ impl<'cmd, 'writer> Help<'cmd, 'writer> {
|
||||||
|
|
||||||
spec_vals.push(format!("[default: {}]", pvs));
|
spec_vals.push(format!("[default: {}]", pvs));
|
||||||
}
|
}
|
||||||
if !a.aliases.is_empty() {
|
|
||||||
|
let als = a
|
||||||
|
.aliases
|
||||||
|
.iter()
|
||||||
|
.filter(|&als| als.1) // visible
|
||||||
|
.map(|als| als.0.as_str()) // name
|
||||||
|
.collect::<Vec<_>>()
|
||||||
|
.join(", ");
|
||||||
|
if !als.is_empty() {
|
||||||
debug!("Help::spec_vals: Found aliases...{:?}", a.aliases);
|
debug!("Help::spec_vals: Found aliases...{:?}", a.aliases);
|
||||||
|
spec_vals.push(format!("[aliases: {}]", als));
|
||||||
let als = a
|
|
||||||
.aliases
|
|
||||||
.iter()
|
|
||||||
.filter(|&als| als.1) // visible
|
|
||||||
.map(|als| als.0.as_str()) // name
|
|
||||||
.collect::<Vec<_>>()
|
|
||||||
.join(", ");
|
|
||||||
|
|
||||||
if !als.is_empty() {
|
|
||||||
spec_vals.push(format!("[aliases: {}]", als));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if !a.short_aliases.is_empty() {
|
let als = a
|
||||||
|
.short_aliases
|
||||||
|
.iter()
|
||||||
|
.filter(|&als| als.1) // visible
|
||||||
|
.map(|&als| als.0.to_string()) // name
|
||||||
|
.collect::<Vec<_>>()
|
||||||
|
.join(", ");
|
||||||
|
if !als.is_empty() {
|
||||||
debug!(
|
debug!(
|
||||||
"Help::spec_vals: Found short aliases...{:?}",
|
"Help::spec_vals: Found short aliases...{:?}",
|
||||||
a.short_aliases
|
a.short_aliases
|
||||||
);
|
);
|
||||||
|
spec_vals.push(format!("[short aliases: {}]", als));
|
||||||
let als = a
|
|
||||||
.short_aliases
|
|
||||||
.iter()
|
|
||||||
.filter(|&als| als.1) // visible
|
|
||||||
.map(|&als| als.0.to_string()) // name
|
|
||||||
.collect::<Vec<_>>()
|
|
||||||
.join(", ");
|
|
||||||
|
|
||||||
if !als.is_empty() {
|
|
||||||
spec_vals.push(format!("[short aliases: {}]", als));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
let possible_vals = a.get_possible_values();
|
let possible_vals = a.get_possible_values();
|
||||||
|
|
Loading…
Reference in a new issue