Skip to content

Commit

Permalink
fix: Show possible values in generated man file
Browse files Browse the repository at this point in the history
This adds feature parity for mangen with the standard help output. Users
will now see the list of possible values for value arguments.

One change that was made to make this possible was adding the method
`get_possible_values` to the public API for an arg. I tried to think of
a way to get around this, but because this is the interface that the
help generation uses, and it is part of the crate public interface
I thing adding it as a part of the public API might be for the best.

fixes: #3861
  • Loading branch information
Calder-Ty committed Aug 16, 2022
1 parent 472ede9 commit d69937f
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 2 deletions.
13 changes: 13 additions & 0 deletions clap_mangen/src/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,19 @@ pub(crate) fn options(roff: &mut Roff, cmd: &clap::Command) {
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("]"));
}

roff.control("TP", []);
roff.text(header);
roff.text(body);
Expand Down
3 changes: 2 additions & 1 deletion clap_mangen/tests/snapshots/value_hint.bash.roff
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ my/-app
.SH OPTIONS
.TP
/fB/-/-choice/fR

.br
[possible values: /fIbash, fish, zsh/fR]
.TP
/fB/-/-unknown/fR

Expand Down
4 changes: 3 additions & 1 deletion src/builder/arg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3693,7 +3693,9 @@ impl<'help> Arg<'help> {
Some(longs)
}

pub(crate) fn get_possible_values(&self) -> Vec<PossibleValue<'help>> {
/// Get the names of possible values for this argument. Only useful for user
/// facing applications, such as building help messages or man files
pub fn get_possible_values(&self) -> Vec<PossibleValue<'help>> {
if !self.is_takes_value_set() {
vec![]
} else {
Expand Down

0 comments on commit d69937f

Please sign in to comment.