diff --git a/clap_mangen/src/render.rs b/clap_mangen/src/render.rs index 9bef14ce..6883aef4 100644 --- a/clap_mangen/src/render.rs +++ b/clap_mangen/src/render.rs @@ -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 { 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 { + let mut formats: Vec = 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::>() + .join(", "), + )); + formats.push(roman("]")); + } + formats +} diff --git a/src/builder/arg.rs b/src/builder/arg.rs index e057ad19..7fbd0b32 100644 --- a/src/builder/arg.rs +++ b/src/builder/arg.rs @@ -3679,19 +3679,6 @@ impl Arg { } } - // Get the names of possible values for this argument - // pub fn get_possible_value_names(&self) -> Option> { - // 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 {