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
This commit is contained in:
Ed Page
2025-10-29 09:12:41 -05:00
parent 9e3c05ef38
commit 126440ca84
5 changed files with 19 additions and 26 deletions

View File

@@ -479,20 +479,18 @@ impl HelpTemplate<'_, '_> {
// args alignment
should_show_arg(self.use_long, arg)
}) {
if longest_filter(arg) {
let width = display_width(&arg.to_string());
let actual_width = if arg.is_positional() {
width
} else {
width + SHORT_SIZE
};
longest = longest.max(actual_width);
debug!(
"HelpTemplate::write_args: arg={:?} longest={}",
arg.get_id(),
longest
);
}
let width = display_width(&arg.to_string());
let actual_width = if arg.get_long().is_some() {
width + SHORT_SIZE
} else {
width
};
longest = longest.max(actual_width);
debug!(
"HelpTemplate::write_args: arg={:?} longest={}",
arg.get_id(),
longest
);
let key = (sort_key)(arg);
ord_v.insert(key, arg);
@@ -1144,10 +1142,6 @@ fn should_show_subcommand(subcommand: &Command) -> bool {
!subcommand.is_hide_set()
}
fn longest_filter(arg: &Arg) -> bool {
arg.is_takes_value_set() || arg.get_long().is_some() || arg.get_short().is_none()
}
#[cfg(test)]
mod test {
#[test]

View File

@@ -4,8 +4,8 @@ $ typed-derive fn-parser --help
Usage: typed-derive fn-parser [OPTIONS]
Options:
-D <KEY=VALUE> Hand-written parser for tuples
-h, --help Print help
-D <KEY=VALUE> Hand-written parser for tuples
-h, --help Print help
```

View File

@@ -2433,14 +2433,13 @@ Options:
-h, --help Print help
Baz:
-z <BAZ> Short only
-z <BAZ> Short only
"#]];
utils::assert_output(cmd, "demo -h", expected, false);
}
#[test]
#[should_panic]
fn short_with_count() {
let cmd = Command::new("demo").arg(
Arg::new("baz")

View File

@@ -480,8 +480,8 @@ fn optional_value() {
Usage: test [OPTIONS]
Options:
-p [<NUM>]
-h, --help Print help
-p [<NUM>]
-h, --help Print help
"#]]
);

View File

@@ -308,8 +308,8 @@ mod arg {
Usage: test [OPTIONS]
Options:
-p [<NUM>]
-h, --help Print help
-p [<NUM>]
-h, --help Print help
"#]]
);