Skip to content

Commit eabbc6b

Browse files
authored
Merge pull request #6217 from epage/man
fix(man): Always show possible values as a list
2 parents a99bd42 + 76a016a commit eabbc6b

File tree

7 files changed

+72
-40
lines changed

7 files changed

+72
-40
lines changed

clap_mangen/src/render.rs

Lines changed: 24 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -193,33 +193,22 @@ pub(crate) fn options(roff: &mut Roff, items: &[&Arg]) {
193193
}
194194

195195
fn possible_options(roff: &mut Roff, arg: &Arg, arg_help_written: bool) {
196-
if let Some((possible_values_text, with_help)) = get_possible_values(arg) {
196+
if let Some(possible_values_text) = get_possible_values(arg) {
197197
if arg_help_written {
198198
// It looks nice to have a separation between the help and the values
199199
roff.text([Inline::LineBreak]);
200200
}
201-
if with_help {
202-
roff.text([Inline::LineBreak, italic("Possible values:")]);
203-
204-
// Need to indent twice to get it to look right, because .TP heading indents, but
205-
// that indent doesn't Carry over to the .IP for the bullets. The standard shift
206-
// size is 7 for terminal devices
207-
roff.control("RS", ["14"]);
208-
for line in possible_values_text {
209-
roff.control("IP", ["\\(bu", "2"]);
210-
roff.text([roman(line)]);
211-
}
212-
roff.control("RE", []);
213-
} else {
214-
let possible_value_text: Vec<Inline> = vec![
215-
Inline::LineBreak,
216-
roman("["),
217-
italic("possible values: "),
218-
roman(possible_values_text.join(", ")),
219-
roman("]"),
220-
];
221-
roff.text(possible_value_text);
201+
roff.text([Inline::LineBreak, italic("Possible values:")]);
202+
203+
// Need to indent twice to get it to look right, because .TP heading indents, but
204+
// that indent doesn't Carry over to the .IP for the bullets. The standard shift
205+
// size is 7 for terminal devices
206+
roff.control("RS", ["14"]);
207+
for line in possible_values_text {
208+
roff.control("IP", ["\\(bu", "2"]);
209+
roff.extend([line]);
222210
}
211+
roff.control("RE", []);
223212
}
224213
}
225214

@@ -357,7 +346,7 @@ fn option_default_values(opt: &Arg) -> Option<String> {
357346
None
358347
}
359348

360-
fn get_possible_values(arg: &Arg) -> Option<(Vec<String>, bool)> {
349+
fn get_possible_values(arg: &Arg) -> Option<Vec<Roff>> {
361350
if arg.is_hide_possible_values_set() {
362351
return None;
363352
}
@@ -372,21 +361,22 @@ fn get_possible_values(arg: &Arg) -> Option<(Vec<String>, bool)> {
372361
None
373362
}
374363

375-
fn format_possible_values(possibles: &Vec<&clap::builder::PossibleValue>) -> (Vec<String>, bool) {
364+
fn format_possible_values(possibles: &Vec<&clap::builder::PossibleValue>) -> Vec<Roff> {
376365
let mut lines = vec![];
377-
let with_help = possibles.iter().any(|p| p.get_help().is_some());
378-
if with_help {
379-
for value in possibles {
380-
let val_name = value.get_name();
381-
match value.get_help() {
382-
Some(help) => lines.push(format!("{val_name}: {help}")),
383-
None => lines.push(val_name.to_string()),
366+
for value in possibles {
367+
let mut roff = Roff::default();
368+
let val_name = value.get_name();
369+
match value.get_help() {
370+
Some(help) => {
371+
roff.text([roman(format!("{val_name}: {help}"))]);
372+
}
373+
None => {
374+
roff.text([roman(val_name.to_owned())]);
384375
}
385376
}
386-
} else {
387-
lines.append(&mut possibles.iter().map(|p| p.get_name().to_string()).collect());
377+
lines.push(roff);
388378
}
389-
(lines, with_help)
379+
lines
390380
}
391381

392382
fn subcommand_sort_key(command: &clap::Command) -> (usize, &str) {

clap_mangen/tests/snapshots/feature_sample.bash.roff

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,13 @@ some input file
2424
[\fIchoice\fR]
2525

2626
.br
27-
[\fIpossible values: \fRfirst, second]
27+
\fIPossible values:\fR
28+
.RS 14
29+
.IP \(bu 2
30+
first
31+
.IP \(bu 2
32+
second
33+
.RE
2834
.SH SUBCOMMANDS
2935
.TP
3036
my\-app\-test(1)

clap_mangen/tests/snapshots/help_headings.bash.roff

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,12 @@ Print help
2222
[\fIcolor\fR]
2323

2424
.br
25-
[\fIpossible values: \fRalways, never, auto]
25+
\fIPossible values:\fR
26+
.RS 14
27+
.IP \(bu 2
28+
always
29+
.IP \(bu 2
30+
never
31+
.IP \(bu 2
32+
auto
33+
.RE

clap_mangen/tests/snapshots/possible_values.bash.roff

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,15 @@ my\-app
1111
\fB\-\-choice\fR
1212

1313
.br
14-
[\fIpossible values: \fRbash, fish, zsh]
14+
\fIPossible values:\fR
15+
.RS 14
16+
.IP \(bu 2
17+
bash
18+
.IP \(bu 2
19+
fish
20+
.IP \(bu 2
21+
zsh
22+
.RE
1523
.TP
1624
\fB\-\-method\fR
1725

clap_mangen/tests/snapshots/special_commands.bash.roff

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,13 @@ some input file
2424
[\fIchoice\fR]
2525

2626
.br
27-
[\fIpossible values: \fRfirst, second]
27+
\fIPossible values:\fR
28+
.RS 14
29+
.IP \(bu 2
30+
first
31+
.IP \(bu 2
32+
second
33+
.RE
2834
.SH SUBCOMMANDS
2935
.TP
3036
my\-app\-test(1)

clap_mangen/tests/snapshots/sub_subcommands.bash.roff

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,13 @@ some input file
2424
[\fIchoice\fR]
2525

2626
.br
27-
[\fIpossible values: \fRfirst, second]
27+
\fIPossible values:\fR
28+
.RS 14
29+
.IP \(bu 2
30+
first
31+
.IP \(bu 2
32+
second
33+
.RE
2834
.SH SUBCOMMANDS
2935
.TP
3036
my\-app\-test(1)

clap_mangen/tests/snapshots/value_hint.bash.roff

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,15 @@ my\-app
1111
\fB\-\-choice\fR
1212

1313
.br
14-
[\fIpossible values: \fRbash, fish, zsh]
14+
\fIPossible values:\fR
15+
.RS 14
16+
.IP \(bu 2
17+
bash
18+
.IP \(bu 2
19+
fish
20+
.IP \(bu 2
21+
zsh
22+
.RE
1523
.TP
1624
\fB\-\-unknown\fR
1725

0 commit comments

Comments
 (0)