Skip to content

Revert "feat(help): Only print short when required" #5968

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 6 additions & 24 deletions clap_builder/src/output/help_template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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() {
Expand All @@ -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(),
Expand All @@ -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() {
Expand All @@ -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()
Expand Down Expand Up @@ -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(),
Expand All @@ -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() {
Expand Down
6 changes: 3 additions & 3 deletions examples/find.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion tests/builder/help.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4037,7 +4037,7 @@ Options:
-h, --help Print help

Mixed:
--long Long only
--long Long only
<POSITIONAL> Positional

"#]];
Expand Down
Loading