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
  • Loading branch information
Calder-Ty committed Aug 24, 2022
1 parent 7a4bec7 commit 85768d7
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 24 deletions.
60 changes: 49 additions & 11 deletions clap_mangen/src/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,23 +106,23 @@ pub(crate) fn options(roff: &mut Roff, cmd: &clap::Command) {
header.push(roman(&defs));
}


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));
}

let possibles = &opt.get_possible_values();
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 @@ -252,3 +252,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().as_str()));
match value.get_help() {
Some(help) => {
formats.push(roman(": "));
formats.push(roman(help.as_str()));
}
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().as_str())
.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 @@ -3682,19 +3682,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_args(&self) -> Option<ValueRange> {
Expand Down

0 comments on commit 85768d7

Please sign in to comment.