refactor(help): Simplify the code

While this doesn't reduce size, it cleans things up to make it easier
This commit is contained in:
Ed Page 2022-08-25 12:11:27 -05:00
parent 64753bbc7d
commit 8607695ed9
3 changed files with 35 additions and 40 deletions

View file

@ -4,7 +4,6 @@ use std::env;
#[cfg(feature = "env")]
use std::ffi::OsString;
use std::{
borrow::Cow,
cmp::{Ord, Ordering},
fmt::{self, Display, Formatter},
str,
@ -3951,26 +3950,28 @@ impl Arg {
}
// 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());
let delim = " ";
if !self.val_names.is_empty() {
debug!("Arg::name_no_brackets: val_names={:#?}", self.val_names);
if self.val_names.len() > 1 {
Cow::Owned(
self.val_names
.iter()
.map(|n| format!("<{}>", n))
.collect::<Vec<_>>()
.join(delim),
)
self.val_names
.iter()
.map(|n| format!("<{}>", n))
.collect::<Vec<_>>()
.join(delim)
} else {
Cow::Borrowed(self.val_names.first().expect(INTERNAL_ERROR_MSG))
self.val_names
.first()
.expect(INTERNAL_ERROR_MSG)
.as_str()
.to_owned()
}
} else {
debug!("Arg::name_no_brackets: just name");
Cow::Borrowed(self.get_id().as_str())
self.get_id().as_str().to_owned()
}
}

View file

@ -4190,7 +4190,7 @@ impl Command {
.map(|x| {
if x.is_positional() {
// Print val_name for positional arguments. e.g. <file_name>
x.name_no_brackets().to_string()
x.name_no_brackets()
} else {
// Print usage string for flags arguments, e.g. <--help>
x.to_string()

View file

@ -503,10 +503,11 @@ impl<'cmd, 'writer> Help<'cmd, 'writer> {
"={}",
env.1
.as_ref()
.map_or(Cow::Borrowed(""), |val| val.to_string_lossy())
.map(|s| s.to_string_lossy())
.unwrap_or_default()
)
} else {
String::new()
Default::default()
};
let env_info = format!("[env: {}{}]", env.0.to_string_lossy(), env_val);
spec_vals.push(env_info);
@ -534,39 +535,32 @@ impl<'cmd, 'writer> Help<'cmd, 'writer> {
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);
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));
}
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!(
"Help::spec_vals: Found short aliases...{:?}",
a.short_aliases
);
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));
}
spec_vals.push(format!("[short aliases: {}]", als));
}
let possible_vals = a.get_possible_values();