Skip to content

Commit f4ba05b

Browse files
Eric GumbaEric Gumba
authored andcommitted
fix(clap_mangen): Render optional and multiple values
In #3358 we discovered clapmangen wouldn’t render optional required_equals values with [=<VALUE>]. After further experimentation it was also found out that it didn’t appropriately render optional values [VALUE] and variadic values <Val>… as well. This commit adds a check to the options render section Where it will add the correct formatting. e.g instead of —-option=VALUE it will render —option <VALUE>, —option [VALUE] —-option[=VALUE] or option <VALUE>… and so on Fixes #3358.
1 parent 1ffb9a8 commit f4ba05b

File tree

6 files changed

+52
-9
lines changed

6 files changed

+52
-9
lines changed

clap_mangen/src/render.rs

Lines changed: 47 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -99,10 +99,28 @@ pub(crate) fn options(roff: &mut Roff, items: &[&Arg]) {
9999
(None, None) => vec![],
100100
};
101101

102-
if opt.get_num_args().expect("built").takes_values() {
103-
if let Some(value) = &opt.get_value_names() {
104-
header.push(roman("="));
105-
header.push(italic(value.join(" ")));
102+
let arg_range = opt.get_num_args().expect("built");
103+
if arg_range.takes_values() {
104+
if let Some(value_names) = &opt.get_value_names() {
105+
let (lhs, rhs) = option_value_markers(opt);
106+
107+
header.push(roman(lhs));
108+
for (i, name) in value_names.iter().enumerate() {
109+
if i > 0 {
110+
header.push(italic(" "));
111+
}
112+
113+
let mut val = format!("<{name}>");
114+
115+
// If this is the last value and it's variadic, add "..."
116+
let is_last = i == value_names.len() - 1;
117+
118+
if is_last && arg_range.max_values() > value_names.len() {
119+
val.push_str("...");
120+
}
121+
header.push(italic(val));
122+
}
123+
header.push(roman(rhs));
106124
}
107125
}
108126

@@ -251,6 +269,31 @@ fn markers(required: bool) -> (&'static str, &'static str) {
251269
}
252270
}
253271

272+
fn option_value_markers(arg: &Arg) -> (&'static str, &'static str) {
273+
let range = arg.get_num_args().expect("built");
274+
275+
if !range.takes_values() {
276+
return ("", ""); // no value, so nothing to render
277+
}
278+
279+
let required = range.min_values() > 0;
280+
let require_equals = arg.is_require_equals_set();
281+
282+
match (required, require_equals) {
283+
// Required, no equals: <VALUE>
284+
(true, false) => (" ", ""),
285+
286+
// Optional, no equals: [<VALUE>]
287+
(false, false) => (" [", "]"),
288+
289+
// Optional, with equals: [=<VALUE>]
290+
(false, true) => ("[=", "]"),
291+
292+
// Required, with equals
293+
(true, true) => ("=", ""),
294+
}
295+
}
296+
254297
fn short_option(opt: char) -> Inline {
255298
bold(format!("-{opt}"))
256299
}

clap_mangen/tests/snapshots/multiple_optional_values.bash.roff

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ my\-app
88
.SH DESCRIPTION
99
.SH OPTIONS
1010
.TP
11-
\fB\-\-config\fR=\fIFILE1 FILE2\fR
11+
\fB\-\-config\fR [\fI<FILE1>\fR\fI \fR\fI<FILE2>\fR]
1212
Optional config file
1313
.TP
1414
\fB\-h\fR, \fB\-\-help\fR

clap_mangen/tests/snapshots/optional_value.bash.roff

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ my\-app
88
.SH DESCRIPTION
99
.SH OPTIONS
1010
.TP
11-
\fB\-\-config\fR=\fIFILE\fR
11+
\fB\-\-config\fR [\fI<FILE>\fR]
1212
Optional config file
1313
.TP
1414
\fB\-h\fR, \fB\-\-help\fR

clap_mangen/tests/snapshots/optional_with_required_equals_value.bash.roff

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ my\-app
88
.SH DESCRIPTION
99
.SH OPTIONS
1010
.TP
11-
\fB\-\-config\fR=\fIFILE\fR
11+
\fB\-\-config\fR[=\fI<FILE>\fR]
1212
Optional config file
1313
.TP
1414
\fB\-h\fR, \fB\-\-help\fR

clap_mangen/tests/snapshots/value_with_required_equals.bash.roff

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ my\-app
88
.SH DESCRIPTION
99
.SH OPTIONS
1010
.TP
11-
\fB\-\-config\fR=\fIFILE\fR
11+
\fB\-\-config\fR=\fI<FILE>\fR
1212
Optional config file
1313
.TP
1414
\fB\-h\fR, \fB\-\-help\fR

clap_mangen/tests/snapshots/variadic_values.bash.roff

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ my\-app
88
.SH DESCRIPTION
99
.SH OPTIONS
1010
.TP
11-
\fB\-\-config\fR=\fIFILE1 FILE2\fR
11+
\fB\-\-config\fR \fI<FILE1>\fR\fI \fR\fI<FILE2>...\fR
1212
Optional config file
1313
.TP
1414
\fB\-h\fR, \fB\-\-help\fR

0 commit comments

Comments
 (0)