Skip to content

Commit 126440c

Browse files
committed
fix(help): Correctly calculate padding for short-only args
This manifests in two ways - If a value is taken, we double the padding - If `ArgAction::Count` is used, we don't account for `...` and crash Fixes #6164
1 parent 9e3c05e commit 126440c

File tree

5 files changed

+19
-26
lines changed

5 files changed

+19
-26
lines changed

clap_builder/src/output/help_template.rs

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -479,20 +479,18 @@ impl HelpTemplate<'_, '_> {
479479
// args alignment
480480
should_show_arg(self.use_long, arg)
481481
}) {
482-
if longest_filter(arg) {
483-
let width = display_width(&arg.to_string());
484-
let actual_width = if arg.is_positional() {
485-
width
486-
} else {
487-
width + SHORT_SIZE
488-
};
489-
longest = longest.max(actual_width);
490-
debug!(
491-
"HelpTemplate::write_args: arg={:?} longest={}",
492-
arg.get_id(),
493-
longest
494-
);
495-
}
482+
let width = display_width(&arg.to_string());
483+
let actual_width = if arg.get_long().is_some() {
484+
width + SHORT_SIZE
485+
} else {
486+
width
487+
};
488+
longest = longest.max(actual_width);
489+
debug!(
490+
"HelpTemplate::write_args: arg={:?} longest={}",
491+
arg.get_id(),
492+
longest
493+
);
496494

497495
let key = (sort_key)(arg);
498496
ord_v.insert(key, arg);
@@ -1144,10 +1142,6 @@ fn should_show_subcommand(subcommand: &Command) -> bool {
11441142
!subcommand.is_hide_set()
11451143
}
11461144

1147-
fn longest_filter(arg: &Arg) -> bool {
1148-
arg.is_takes_value_set() || arg.get_long().is_some() || arg.get_short().is_none()
1149-
}
1150-
11511145
#[cfg(test)]
11521146
mod test {
11531147
#[test]

examples/typed-derive/fn_parser.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ $ typed-derive fn-parser --help
44
Usage: typed-derive fn-parser [OPTIONS]
55

66
Options:
7-
-D <KEY=VALUE> Hand-written parser for tuples
8-
-h, --help Print help
7+
-D <KEY=VALUE> Hand-written parser for tuples
8+
-h, --help Print help
99

1010
```
1111

tests/builder/help.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2433,14 +2433,13 @@ Options:
24332433
-h, --help Print help
24342434
24352435
Baz:
2436-
-z <BAZ> Short only
2436+
-z <BAZ> Short only
24372437
24382438
"#]];
24392439
utils::assert_output(cmd, "demo -h", expected, false);
24402440
}
24412441

24422442
#[test]
2443-
#[should_panic]
24442443
fn short_with_count() {
24452444
let cmd = Command::new("demo").arg(
24462445
Arg::new("baz")

tests/builder/multiple_values.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -480,8 +480,8 @@ fn optional_value() {
480480
Usage: test [OPTIONS]
481481
482482
Options:
483-
-p [<NUM>]
484-
-h, --help Print help
483+
-p [<NUM>]
484+
-h, --help Print help
485485
486486
"#]]
487487
);

tests/macros.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -308,8 +308,8 @@ mod arg {
308308
Usage: test [OPTIONS]
309309
310310
Options:
311-
-p [<NUM>]
312-
-h, --help Print help
311+
-p [<NUM>]
312+
-h, --help Print help
313313
314314
"#]]
315315
);

0 commit comments

Comments
 (0)