mirror of
https://github.com/clap-rs/clap
synced 2024-12-13 22:32:33 +00:00
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:
parent
6342802f55
commit
ec518e4819
2 changed files with 49 additions and 24 deletions
|
@ -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", []);
|
||||
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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.
|
||||
#[inline]
|
||||
pub fn get_num_args(&self) -> Option<ValueRange> {
|
||||
|
|
Loading…
Reference in a new issue