Repository: clap-rs/clap
Pull request #5968 (2025-04-11 19:34:45)

Revert "feat(help): Only print short when required"
Concern was raised over the alignment in #5963 and decided to defer this for now.  This wasn't part of the original issue and hadn't been discussed before.

If someone wants to move this forward again, I'd recommend starting with an issue to discuss the trade offs.

This reverts commit 78e41f66665f89ceea5975ab6320cfd4524122f2.

<!--
Thanks for helping out!

Please link the appropriate issue from your PR.

If you don't have an issue, we'd recommend starting with one first so the PR can focus on the
implementation (unless its an obvious bug or documentation fix that will have
little conversation).
-->



Patch:
diff --git a/clap_builder/src/output/help_template.rs b/clap_builder/src/output/help_template.rs
index adfc3b97..98d9aad9 100644
--- a/clap_builder/src/output/help_template.rs
+++ b/clap_builder/src/output/help_template.rs
@@ -470,8 +470,6 @@ impl HelpTemplate<'_, '_> {
         debug!("HelpTemplate::write_args {_category}");
         // The shortest an arg can legally be is 2 (i.e. '-x')
         let mut longest = 2;
-        let mut longest_without_short = 2;
-        let mut has_short = false;
         let mut ord_v = BTreeMap::new();
 
         // Determine the longest
@@ -481,10 +479,6 @@ impl HelpTemplate<'_, '_> {
             // args alignment
             should_show_arg(self.use_long, arg)
         }) {
-            if !has_short && arg.get_short().is_some() {
-                has_short = true;
-            }
-
             if longest_filter(arg) {
                 let width = display_width(&arg.to_string());
                 let actual_width = if arg.is_positional() {
@@ -493,9 +487,6 @@ impl HelpTemplate<'_, '_> {
                     width + SHORT_SIZE
                 };
                 longest = longest.max(actual_width);
-                if !has_short {
-                    longest_without_short = longest_without_short.max(width);
-                }
                 debug!(
                     "HelpTemplate::write_args: arg={:?} longest={}",
                     arg.get_id(),
@@ -507,10 +498,6 @@ impl HelpTemplate<'_, '_> {
             ord_v.insert(key, arg);
         }
 
-        if !has_short {
-            longest = longest_without_short;
-        }
-
         let next_line_help = self.will_args_wrap(args, longest);
 
         for (i, (_, arg)) in ord_v.iter().enumerate() {
@@ -520,22 +507,20 @@ impl HelpTemplate<'_, '_> {
                     self.writer.push_str("\n");
                 }
             }
-            self.write_arg(arg, next_line_help, longest, has_short);
+            self.write_arg(arg, next_line_help, longest);
         }
     }
 
     /// Writes help for an argument to the wrapped stream.
-    fn write_arg(&mut self, arg: &Arg, next_line_help: bool, longest: usize, has_short: bool) {
+    fn write_arg(&mut self, arg: &Arg, next_line_help: bool, longest: usize) {
         let spec_vals = &self.spec_vals(arg);
 
         self.writer.push_str(TAB);
-        if has_short {
-            self.short(arg);
-        }
+        self.short(arg);
         self.long(arg);
         self.writer
             .push_styled(&arg.stylize_arg_suffix(self.styles, None));
-        self.align_to_about(arg, next_line_help, longest, has_short);
+        self.align_to_about(arg, next_line_help, longest);
 
         let about = if self.use_long {
             arg.get_long_help()
@@ -578,7 +563,7 @@ impl HelpTemplate<'_, '_> {
     }
 
     /// Write alignment padding between arg's switches/values and its about message.
-    fn align_to_about(&mut self, arg: &Arg, next_line_help: bool, longest: usize, has_short: bool) {
+    fn align_to_about(&mut self, arg: &Arg, next_line_help: bool, longest: usize) {
         debug!(
             "HelpTemplate::align_to_about: arg={}, next_line_help={}, longest={}",
             arg.get_id(),
@@ -590,10 +575,7 @@ impl HelpTemplate<'_, '_> {
             debug!("HelpTemplate::align_to_about: printing long help so skip alignment");
             0
         } else if !arg.is_positional() {
-            let mut self_len = display_width(&arg.to_string());
-            if has_short {
-                self_len += SHORT_SIZE;
-            }
+            let self_len = display_width(&arg.to_string()) + SHORT_SIZE;
             // Since we're writing spaces from the tab point we first need to know if we
             // had a long and short, or just short
             let padding = if arg.get_long().is_some() {
diff --git a/examples/find.md b/examples/find.md
index f88cbad8..c52cbd39 100644
--- a/examples/find.md
+++ b/examples/find.md
@@ -11,9 +11,9 @@ Options:
   -V, --version  Print version
 
 TESTS:
-  --empty        File is empty and is either a regular file or a directory
-  --name <name>  Base of file name (the path with the leading directories removed) matches shell
-                 pattern pattern
+      --empty        File is empty and is either a regular file or a directory
+      --name <name>  Base of file name (the path with the leading directories removed) matches shell
+                     pattern pattern
 
 OPERATORS:
   -o, --or   expr2 is not evaluate if exp1 is true


Test patch:
diff --git a/tests/builder/help.rs b/tests/builder/help.rs
index 26bb1308..1a2014da 100644
--- a/tests/builder/help.rs
+++ b/tests/builder/help.rs
@@ -4037,7 +4037,7 @@ Options:
   -h, --help  Print help
 
 Mixed:
-  --long        Long only
+      --long    Long only
   <POSITIONAL>  Positional
 
 "#]];