fix(help): Show value_parser's PossibleValues

This commit is contained in:
Ed Page 2022-05-20 19:59:22 -05:00
parent 5b4ea8e56e
commit 852a1b15d4
2 changed files with 29 additions and 19 deletions

View file

@ -128,7 +128,9 @@ pub fn flags<'help>(p: &Command<'help>) -> Vec<Arg<'help>> {
/// Get the possible values for completion
pub fn possible_values<'help>(a: &Arg<'help>) -> Option<Vec<clap::PossibleValue<'help>>> {
if let Some(pvs) = a.get_possible_values() {
if !a.is_takes_value_set() {
None
} else if let Some(pvs) = a.get_possible_values() {
// Check old first in case the user explicitly set possible values and the derive inferred
// a `ValueParser` with some.
Some(pvs.to_vec())

View file

@ -456,27 +456,23 @@ impl<'help, 'cmd, 'writer> Help<'help, 'cmd, 'writer> {
if let Some(arg) = arg {
const DASH_SPACE: usize = "- ".len();
const COLON_SPACE: usize = ": ".len();
let possible_vals = get_possible_values(arg);
if self.use_long
&& !arg.is_hide_possible_values_set()
&& arg
.possible_vals
.iter()
.any(PossibleValue::should_show_help)
&& possible_vals.iter().any(PossibleValue::should_show_help)
{
debug!("Help::help: Found possible vals...{:?}", arg.possible_vals);
debug!("Help::help: Found possible vals...{:?}", possible_vals);
if !help.is_empty() {
self.none("\n\n")?;
self.spaces(spaces)?;
}
self.none("Possible values:")?;
let longest = arg
.possible_vals
let longest = possible_vals
.iter()
.filter_map(|f| f.get_visible_quoted_name().map(|name| display_width(&name)))
.max()
.expect("Only called with possible value");
let help_longest = arg
.possible_vals
let help_longest = possible_vals
.iter()
.filter_map(|f| f.get_visible_help().map(display_width))
.max()
@ -494,7 +490,7 @@ impl<'help, 'cmd, 'writer> Help<'help, 'cmd, 'writer> {
spaces + longest + DASH_SPACE + COLON_SPACE
};
for pv in arg.possible_vals.iter().filter(|pv| !pv.is_hide_set()) {
for pv in possible_vals.iter().filter(|pv| !pv.is_hide_set()) {
self.none("\n")?;
self.spaces(spaces)?;
self.none("- ")?;
@ -662,19 +658,16 @@ impl<'help, 'cmd, 'writer> Help<'help, 'cmd, 'writer> {
}
}
let possible_vals = get_possible_values(a);
if !(a.is_hide_possible_values_set()
|| a.possible_vals.is_empty()
|| possible_vals.is_empty()
|| cfg!(feature = "unstable-v4")
&& self.use_long
&& a.possible_vals.iter().any(PossibleValue::should_show_help))
&& possible_vals.iter().any(PossibleValue::should_show_help))
{
debug!(
"Help::spec_vals: Found possible vals...{:?}",
a.possible_vals
);
debug!("Help::spec_vals: Found possible vals...{:?}", possible_vals);
let pvs = a
.possible_vals
let pvs = possible_vals
.iter()
.filter_map(PossibleValue::get_visible_quoted_name)
.collect::<Vec<_>>()
@ -1148,6 +1141,21 @@ fn text_wrapper(help: &str, width: usize) -> String {
.join("\n")
}
fn get_possible_values<'help>(a: &Arg<'help>) -> Vec<PossibleValue<'help>> {
if !a.is_takes_value_set() {
vec![]
} else if let Some(pvs) = a.get_possible_values() {
// Check old first in case the user explicitly set possible values and the derive inferred
// a `ValueParser` with some.
pvs.to_vec()
} else {
a.get_value_parser()
.possible_values()
.map(|pvs| pvs.collect())
.unwrap_or_default()
}
}
#[cfg(test)]
mod test {
use super::*;