mirror of
https://github.com/clap-rs/clap.git
synced 2026-01-25 08:06:14 +00:00
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:
@@ -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]
|
||||
|
||||
@@ -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
|
||||
|
||||
```
|
||||
|
||||
|
||||
@@ -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")
|
||||
|
||||
@@ -480,8 +480,8 @@ fn optional_value() {
|
||||
Usage: test [OPTIONS]
|
||||
|
||||
Options:
|
||||
-p [<NUM>]
|
||||
-h, --help Print help
|
||||
-p [<NUM>]
|
||||
-h, --help Print help
|
||||
|
||||
"#]]
|
||||
);
|
||||
|
||||
@@ -308,8 +308,8 @@ mod arg {
|
||||
Usage: test [OPTIONS]
|
||||
|
||||
Options:
|
||||
-p [<NUM>]
|
||||
-h, --help Print help
|
||||
-p [<NUM>]
|
||||
-h, --help Print help
|
||||
|
||||
"#]]
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user