fix: Take in account possible values being hidden

This makes sure we take into account the setting that possible args
is hidden
This commit is contained in:
Tyler Calder 2022-08-16 10:15:01 -06:00 committed by Tyler Calder
parent 6342802f55
commit ec518e4819
2 changed files with 49 additions and 24 deletions

View file

@ -106,23 +106,23 @@ pub(crate) fn options(roff: &mut Roff, cmd: &clap::Command) {
header.push(roman(&defs)); header.push(roman(&defs));
} }
let mut body = vec![]; let mut body = vec![];
let mut help_written = false;
if let Some(help) = opt.get_long_help().or_else(|| opt.get_help()) { if let Some(help) = opt.get_long_help().or_else(|| opt.get_help()) {
help_written = true;
body.push(roman(help)); body.push(roman(help));
} }
let possibles = &opt.get_possible_values(); let possibles = &opt.get_possible_values();
if !possibles.is_empty() {
let pos_options: Vec<&str> = possibles if !(possibles.is_empty() || opt.is_hide_possible_values_set()) {
.iter() if help_written {
.filter(|pos| !pos.is_hide_set()) // It looks nice to have a separation between the help and the values
.map(|pos| pos.get_name()) body.push(Inline::LineBreak);
.collect(); }
body.push(Inline::LineBreak);
body.push(roman("[possible values: ")); let possible_vals = possibles.iter().filter(|pos| !pos.is_hide_set()).collect();
body.push(italic(pos_options.join(", "))); body.append(&mut format_possible_values(possible_vals));
body.push(roman("]"));
} }
roff.control("TP", []); roff.control("TP", []);
@ -252,3 +252,41 @@ fn option_default_values(opt: &clap::Arg) -> Option<String> {
None 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
}

View file

@ -3679,19 +3679,6 @@ impl Arg {
} }
} }
// 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. /// Get the number of values for this argument.
#[inline] #[inline]
pub fn get_num_args(&self) -> Option<ValueRange> { pub fn get_num_args(&self) -> Option<ValueRange> {