Skip to content

Commit

Permalink
fix: Take in account possible values being hidden
Browse files Browse the repository at this point in the history
This makes sure we take into account the setting that possible args
is hidden

backporting to v3-master
  • Loading branch information
Calder-Ty committed Aug 27, 2022
1 parent 17960a4 commit 3d085b8
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 23 deletions.
59 changes: 49 additions & 10 deletions clap_mangen/src/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,9 @@ pub(crate) fn options(roff: &mut Roff, cmd: &clap::Command) {
}

let mut body = vec![];
let mut help_written = false;
if let Some(help) = opt.get_long_help().or_else(|| opt.get_help()) {
help_written = true;
body.push(roman(help));
}

Expand All @@ -118,16 +120,15 @@ pub(crate) fn options(roff: &mut Roff, cmd: &clap::Command) {
.map(|pvs| pvs.collect())
.unwrap_or_default();

if !possibles.is_empty() {
let pos_options: Vec<&str> = possibles
.iter()
.filter(|pos| !pos.is_hide_set())
.map(|pos| pos.get_name())
.collect();
body.push(Inline::LineBreak);
body.push(roman("[possible values: "));
body.push(italic(pos_options.join(", ")));
body.push(roman("]"));

if !(possibles.is_empty() || opt.is_hide_possible_values_set()) {
if help_written {
// It looks nice to have a separation between the help and the values
body.push(Inline::LineBreak);
}

let possible_vals = possibles.iter().filter(|pos| !pos.is_hide_set()).collect();
body.append(&mut format_possible_values(possible_vals));
}

roff.control("TP", []);
Expand Down Expand Up @@ -258,3 +259,41 @@ fn option_default_values(opt: &clap::Arg) -> Option<String> {

None
}

/// Generates a Vector of Inline Commands to push to the roff
/// to properly format possible values that an option can take.
fn format_possible_values(values: Vec<&clap::builder::PossibleValue>) -> Vec<Inline> {
let mut formats: Vec<Inline> = vec![];
// With Help
if values.iter().any(|p| p.get_help().is_some()) {
formats.push(Inline::LineBreak);
formats.push(roman("Possible values:"));
formats.push(Inline::LineBreak);
for value in values {
formats.push(roman(" - "));
formats.push(roman(value.get_name()));
match value.get_help() {
Some(help) => {
formats.push(roman(": "));
formats.push(roman(help));
}
None => {}
}
formats.push(Inline::LineBreak);
}
}
// Without help
else {
formats.push(Inline::LineBreak);
formats.push(roman("[possible values: "));
formats.push(italic(
values
.iter()
.map(|p| p.get_name())
.collect::<Vec<&str>>()
.join(", "),
));
formats.push(roman("]"));
}
formats
}
13 changes: 0 additions & 13 deletions src/builder/arg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4426,19 +4426,6 @@ impl<'help> Arg<'help> {
}
}

// Get the names of possible values for this argument
// pub fn get_possible_value_names(&self) -> Option<Vec<&'help str>> {
// let possible_values = self.get_possible_values();
// if !possible_values.is_empty() {
// let possible_options: Vec<&str> = possible_values.iter().map(|pos| pos.get_name()).collect();
// Some(possible_options)
// }
// else {
// None
// }

// }

/// Get the number of values for this argument.
#[inline]
pub fn get_num_vals(&self) -> Option<usize> {
Expand Down

0 comments on commit 3d085b8

Please sign in to comment.