Skip to content

Commit

Permalink
fix(help): Show value_parser's PossibleValues
Browse files Browse the repository at this point in the history
  • Loading branch information
epage committed May 21, 2022
1 parent 5b4ea8e commit 852a1b1
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 19 deletions.
4 changes: 3 additions & 1 deletion clap_complete/src/generator/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
44 changes: 26 additions & 18 deletions src/output/help.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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("- ")?;
Expand Down Expand Up @@ -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<_>>()
Expand Down Expand Up @@ -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::*;
Expand Down

0 comments on commit 852a1b1

Please sign in to comment.