Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 0f7dbbc

Browse files
committed
fix(help): Align mixed argument types
Fixes #3835
1 parent bdfa79a commit 0f7dbbc

4 files changed

Lines changed: 22 additions & 22 deletions

File tree

clap_builder/src/output/help_template.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -480,7 +480,13 @@ impl HelpTemplate<'_, '_> {
480480
should_show_arg(self.use_long, arg)
481481
}) {
482482
if longest_filter(arg) {
483-
longest = longest.max(display_width(&arg.to_string()));
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);
484490
debug!(
485491
"HelpTemplate::write_args: arg={:?} longest={}",
486492
arg.get_id(),
@@ -569,7 +575,7 @@ impl HelpTemplate<'_, '_> {
569575
debug!("HelpTemplate::align_to_about: printing long help so skip alignment");
570576
0
571577
} else if !arg.is_positional() {
572-
let self_len = display_width(&arg.to_string());
578+
let self_len = display_width(&arg.to_string()) + SHORT_SIZE;
573579
// Since we're writing spaces from the tab point we first need to know if we
574580
// had a long and short, or just short
575581
let padding = if arg.get_long().is_some() {
@@ -622,10 +628,8 @@ impl HelpTemplate<'_, '_> {
622628

623629
let spaces = if next_line_help {
624630
TAB.len() + NEXT_LINE_INDENT.len()
625-
} else if arg.map(|a| a.is_positional()).unwrap_or(true) {
626-
longest + TAB_WIDTH * 2
627631
} else {
628-
longest + TAB_WIDTH * 2 + SHORT_SIZE
632+
longest + TAB_WIDTH * 2
629633
};
630634
let trailing_indent = spaces; // Don't indent any further than the first line is indented
631635
let trailing_indent = self.get_spaces(trailing_indent);
@@ -726,11 +730,7 @@ impl HelpTemplate<'_, '_> {
726730
.or_else(|| arg.get_long_help())
727731
.unwrap_or_default();
728732
let h_w = h.display_width() + display_width(spec_vals);
729-
let taken = if arg.is_positional() {
730-
longest + TAB_WIDTH * 2
731-
} else {
732-
longest + TAB_WIDTH * 2 + SHORT_SIZE
733-
};
733+
let taken = longest + TAB_WIDTH * 2;
734734
self.term_w >= taken
735735
&& (taken as f32 / self.term_w as f32) > 0.40
736736
&& h_w > (self.term_w - taken)

examples/git-derive.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,12 +87,12 @@ git-derive[EXE] stash push:
8787
-h, --help Print help
8888

8989
git-derive[EXE] stash pop:
90-
-h, --help Print help
91-
[STASH]
90+
-h, --help Print help
91+
[STASH]
9292

9393
git-derive[EXE] stash apply:
94-
-h, --help Print help
95-
[STASH]
94+
-h, --help Print help
95+
[STASH]
9696

9797
git-derive[EXE] stash help:
9898
Print this message or the help of the given subcommand(s)

examples/git.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,12 +85,12 @@ git[EXE] stash push:
8585
-h, --help Print help
8686

8787
git[EXE] stash pop:
88-
-h, --help Print help
89-
[STASH]
88+
-h, --help Print help
89+
[STASH]
9090

9191
git[EXE] stash apply:
92-
-h, --help Print help
93-
[STASH]
92+
-h, --help Print help
93+
[STASH]
9494

9595
git[EXE] stash help:
9696
Print this message or the help of the given subcommand(s)

tests/builder/help.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3986,8 +3986,8 @@ Options:
39863986
-h, --help Print help
39873987
39883988
Mixed:
3989-
-b, --both Both long and short
3990-
--long Long only
3989+
-b, --both Both long and short
3990+
--long Long only
39913991
<POSITIONAL> Positional
39923992
39933993
"#]];
@@ -4014,7 +4014,7 @@ Options:
40144014
Mixed:
40154015
-b, --both Both long and short
40164016
--long Long only
4017-
<S> Short positional
4017+
<S> Short positional
40184018
40194019
"#]];
40204020
utils::assert_output(cmd, "myprog --help", expected, false);
@@ -4037,7 +4037,7 @@ Options:
40374037
-h, --help Print help
40384038
40394039
Mixed:
4040-
--long Long only
4040+
--long Long only
40414041
<POSITIONAL> Positional
40424042
40434043
"#]];

0 commit comments

Comments
 (0)