Skip to content

Commit 9d218eb

Browse files
authored
Merge pull request #6165 from epage/shirt
fix(help): Correctly calculate padding for short-only args
2 parents df7bdfc + 126440c commit 9d218eb

File tree

5 files changed

+64
-24
lines changed

5 files changed

+64
-24
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: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2416,6 +2416,52 @@ Options:
24162416
utils::assert_output(cmd, "demo -h", expected, false);
24172417
}
24182418

2419+
#[test]
2420+
fn short_with_value() {
2421+
let cmd = Command::new("demo").arg(
2422+
Arg::new("baz")
2423+
.short('z')
2424+
.value_name("BAZ")
2425+
.help("Short only")
2426+
.help_heading("Baz"),
2427+
);
2428+
2429+
let expected = str![[r#"
2430+
Usage: demo [OPTIONS]
2431+
2432+
Options:
2433+
-h, --help Print help
2434+
2435+
Baz:
2436+
-z <BAZ> Short only
2437+
2438+
"#]];
2439+
utils::assert_output(cmd, "demo -h", expected, false);
2440+
}
2441+
2442+
#[test]
2443+
fn short_with_count() {
2444+
let cmd = Command::new("demo").arg(
2445+
Arg::new("baz")
2446+
.short('z')
2447+
.action(ArgAction::Count)
2448+
.help("Short only")
2449+
.help_heading("Baz"),
2450+
);
2451+
2452+
let expected = str![[r#"
2453+
Usage: demo [OPTIONS]
2454+
2455+
Options:
2456+
-h, --help Print help
2457+
2458+
Baz:
2459+
-z... Short only
2460+
2461+
"#]];
2462+
utils::assert_output(cmd, "demo -h", expected, false);
2463+
}
2464+
24192465
#[test]
24202466
fn issue_1487() {
24212467
let cmd = Command::new("test")

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)