clap_builder/output/
help_template.rs

1// HACK: for rust 1.64 (1.68 doesn't need this since this is in lib.rs)
2//
3// Wanting consistency in our calls
4#![allow(clippy::write_with_newline)]
5
6// Std
7use std::borrow::Cow;
8use std::cmp;
9use std::collections::BTreeMap;
10
11// Internal
12use crate::builder::PossibleValue;
13use crate::builder::Str;
14use crate::builder::StyledStr;
15use crate::builder::Styles;
16use crate::builder::{Arg, Command};
17use crate::output::display_width;
18use crate::output::wrap;
19use crate::output::Usage;
20use crate::output::TAB;
21use crate::output::TAB_WIDTH;
22use crate::util::FlatSet;
23
24/// `clap` auto-generated help writer
25pub(crate) struct AutoHelp<'cmd, 'writer> {
26    template: HelpTemplate<'cmd, 'writer>,
27}
28
29// Public Functions
30impl<'cmd, 'writer> AutoHelp<'cmd, 'writer> {
31    /// Create a new `HelpTemplate` instance.
32    pub(crate) fn new(
33        writer: &'writer mut StyledStr,
34        cmd: &'cmd Command,
35        usage: &'cmd Usage<'cmd>,
36        use_long: bool,
37    ) -> Self {
38        Self {
39            template: HelpTemplate::new(writer, cmd, usage, use_long),
40        }
41    }
42
43    pub(crate) fn write_help(&mut self) {
44        let pos = self
45            .template
46            .cmd
47            .get_positionals()
48            .any(|arg| should_show_arg(self.template.use_long, arg));
49        let non_pos = self
50            .template
51            .cmd
52            .get_non_positionals()
53            .any(|arg| should_show_arg(self.template.use_long, arg));
54        let subcmds = self.template.cmd.has_visible_subcommands();
55
56        let template = if non_pos || pos || subcmds {
57            DEFAULT_TEMPLATE
58        } else {
59            DEFAULT_NO_ARGS_TEMPLATE
60        };
61        self.template.write_templated_help(template);
62    }
63}
64
65const DEFAULT_TEMPLATE: &str = "\
66{before-help}{about-with-newline}
67{usage-heading} {usage}
68
69{all-args}{after-help}\
70    ";
71
72const DEFAULT_NO_ARGS_TEMPLATE: &str = "\
73{before-help}{about-with-newline}
74{usage-heading} {usage}{after-help}\
75    ";
76
77const SHORT_SIZE: usize = 4; // See `fn short` for the 4
78
79/// Help template writer
80///
81/// Wraps a writer stream providing different methods to generate help for `clap` objects.
82pub(crate) struct HelpTemplate<'cmd, 'writer> {
83    writer: &'writer mut StyledStr,
84    cmd: &'cmd Command,
85    styles: &'cmd Styles,
86    usage: &'cmd Usage<'cmd>,
87    next_line_help: bool,
88    term_w: usize,
89    use_long: bool,
90}
91
92// Public Functions
93impl<'cmd, 'writer> HelpTemplate<'cmd, 'writer> {
94    /// Create a new `HelpTemplate` instance.
95    pub(crate) fn new(
96        writer: &'writer mut StyledStr,
97        cmd: &'cmd Command,
98        usage: &'cmd Usage<'cmd>,
99        use_long: bool,
100    ) -> Self {
101        debug!(
102            "HelpTemplate::new cmd={}, use_long={}",
103            cmd.get_name(),
104            use_long
105        );
106        let term_w = Self::term_w(cmd);
107        let next_line_help = cmd.is_next_line_help_set();
108
109        HelpTemplate {
110            writer,
111            cmd,
112            styles: cmd.get_styles(),
113            usage,
114            next_line_help,
115            term_w,
116            use_long,
117        }
118    }
119
120    #[cfg(not(feature = "unstable-v5"))]
121    fn term_w(cmd: &'cmd Command) -> usize {
122        match cmd.get_term_width() {
123            Some(0) => usize::MAX,
124            Some(w) => w,
125            None => {
126                let (current_width, _h) = dimensions();
127                let current_width = current_width.unwrap_or(100);
128                let max_width = match cmd.get_max_term_width() {
129                    None | Some(0) => usize::MAX,
130                    Some(mw) => mw,
131                };
132                cmp::min(current_width, max_width)
133            }
134        }
135    }
136
137    #[cfg(feature = "unstable-v5")]
138    fn term_w(cmd: &'cmd Command) -> usize {
139        let term_w = match cmd.get_term_width() {
140            Some(0) => usize::MAX,
141            Some(w) => w,
142            None => {
143                let (current_width, _h) = dimensions();
144                current_width.unwrap_or(usize::MAX)
145            }
146        };
147
148        let max_term_w = match cmd.get_max_term_width() {
149            Some(0) => usize::MAX,
150            Some(mw) => mw,
151            None => 100,
152        };
153
154        cmp::min(term_w, max_term_w)
155    }
156
157    /// Write help to stream for the parser in the format defined by the template.
158    ///
159    /// For details about the template language see [`Command::help_template`].
160    ///
161    /// [`Command::help_template`]: Command::help_template()
162    pub(crate) fn write_templated_help(&mut self, template: &str) {
163        debug!("HelpTemplate::write_templated_help");
164        use std::fmt::Write as _;
165
166        let mut parts = template.split('{');
167        if let Some(first) = parts.next() {
168            self.writer.push_str(first);
169        }
170        for part in parts {
171            if let Some((tag, rest)) = part.split_once('}') {
172                match tag {
173                    "name" => {
174                        self.write_display_name();
175                    }
176                    #[cfg(not(feature = "unstable-v5"))]
177                    "bin" => {
178                        self.write_bin_name();
179                    }
180                    "version" => {
181                        self.write_version();
182                    }
183                    "author" => {
184                        self.write_author(false, false);
185                    }
186                    "author-with-newline" => {
187                        self.write_author(false, true);
188                    }
189                    "author-section" => {
190                        self.write_author(true, true);
191                    }
192                    "about" => {
193                        self.write_about(false, false);
194                    }
195                    "about-with-newline" => {
196                        self.write_about(false, true);
197                    }
198                    "about-section" => {
199                        self.write_about(true, true);
200                    }
201                    "usage-heading" => {
202                        let _ = write!(
203                            self.writer,
204                            "{}Usage:{}",
205                            self.styles.get_usage().render(),
206                            self.styles.get_usage().render_reset()
207                        );
208                    }
209                    "usage" => {
210                        self.writer.push_styled(
211                            &self.usage.create_usage_no_title(&[]).unwrap_or_default(),
212                        );
213                    }
214                    "all-args" => {
215                        self.write_all_args();
216                    }
217                    "options" => {
218                        // Include even those with a heading as we don't have a good way of
219                        // handling help_heading in the template.
220                        self.write_args(
221                            &self.cmd.get_non_positionals().collect::<Vec<_>>(),
222                            "options",
223                            option_sort_key,
224                        );
225                    }
226                    "positionals" => {
227                        self.write_args(
228                            &self.cmd.get_positionals().collect::<Vec<_>>(),
229                            "positionals",
230                            positional_sort_key,
231                        );
232                    }
233                    "subcommands" => {
234                        self.write_subcommands(self.cmd);
235                    }
236                    "tab" => {
237                        self.writer.push_str(TAB);
238                    }
239                    "after-help" => {
240                        self.write_after_help();
241                    }
242                    "before-help" => {
243                        self.write_before_help();
244                    }
245                    _ => {
246                        let _ = write!(self.writer, "{{{tag}}}");
247                    }
248                }
249                self.writer.push_str(rest);
250            }
251        }
252    }
253}
254
255/// Basic template methods
256impl HelpTemplate<'_, '_> {
257    /// Writes binary name of a Parser Object to the wrapped stream.
258    fn write_display_name(&mut self) {
259        debug!("HelpTemplate::write_display_name");
260
261        let display_name = wrap(
262            &self
263                .cmd
264                .get_display_name()
265                .unwrap_or_else(|| self.cmd.get_name())
266                .replace("{n}", "\n"),
267            self.term_w,
268        );
269        self.writer.push_string(display_name);
270    }
271
272    /// Writes binary name of a Parser Object to the wrapped stream.
273    #[cfg(not(feature = "unstable-v5"))]
274    fn write_bin_name(&mut self) {
275        debug!("HelpTemplate::write_bin_name");
276
277        let bin_name = if let Some(bn) = self.cmd.get_bin_name() {
278            if bn.contains(' ') {
279                // In case we're dealing with subcommands i.e. git mv is translated to git-mv
280                bn.replace(' ', "-")
281            } else {
282                wrap(&self.cmd.get_name().replace("{n}", "\n"), self.term_w)
283            }
284        } else {
285            wrap(&self.cmd.get_name().replace("{n}", "\n"), self.term_w)
286        };
287        self.writer.push_string(bin_name);
288    }
289
290    fn write_version(&mut self) {
291        let version = self
292            .cmd
293            .get_version()
294            .or_else(|| self.cmd.get_long_version());
295        if let Some(output) = version {
296            self.writer.push_string(wrap(output, self.term_w));
297        }
298    }
299
300    fn write_author(&mut self, before_new_line: bool, after_new_line: bool) {
301        if let Some(author) = self.cmd.get_author() {
302            if before_new_line {
303                self.writer.push_str("\n");
304            }
305            self.writer.push_string(wrap(author, self.term_w));
306            if after_new_line {
307                self.writer.push_str("\n");
308            }
309        }
310    }
311
312    fn write_about(&mut self, before_new_line: bool, after_new_line: bool) {
313        let about = if self.use_long {
314            self.cmd.get_long_about().or_else(|| self.cmd.get_about())
315        } else {
316            self.cmd.get_about()
317        };
318        if let Some(output) = about {
319            if before_new_line {
320                self.writer.push_str("\n");
321            }
322            let mut output = output.clone();
323            output.replace_newline_var();
324            output.wrap(self.term_w);
325            self.writer.push_styled(&output);
326            if after_new_line {
327                self.writer.push_str("\n");
328            }
329        }
330    }
331
332    fn write_before_help(&mut self) {
333        debug!("HelpTemplate::write_before_help");
334        let before_help = if self.use_long {
335            self.cmd
336                .get_before_long_help()
337                .or_else(|| self.cmd.get_before_help())
338        } else {
339            self.cmd.get_before_help()
340        };
341        if let Some(output) = before_help {
342            let mut output = output.clone();
343            output.replace_newline_var();
344            output.wrap(self.term_w);
345            self.writer.push_styled(&output);
346            self.writer.push_str("\n\n");
347        }
348    }
349
350    fn write_after_help(&mut self) {
351        debug!("HelpTemplate::write_after_help");
352        let after_help = if self.use_long {
353            self.cmd
354                .get_after_long_help()
355                .or_else(|| self.cmd.get_after_help())
356        } else {
357            self.cmd.get_after_help()
358        };
359        if let Some(output) = after_help {
360            self.writer.push_str("\n\n");
361            let mut output = output.clone();
362            output.replace_newline_var();
363            output.wrap(self.term_w);
364            self.writer.push_styled(&output);
365        }
366    }
367}
368
369/// Arg handling
370impl HelpTemplate<'_, '_> {
371    /// Writes help for all arguments (options, flags, args, subcommands)
372    /// including titles of a Parser Object to the wrapped stream.
373    pub(crate) fn write_all_args(&mut self) {
374        debug!("HelpTemplate::write_all_args");
375        use std::fmt::Write as _;
376        let header = &self.styles.get_header();
377
378        let pos = self
379            .cmd
380            .get_positionals()
381            .filter(|a| a.get_help_heading().is_none())
382            .filter(|arg| should_show_arg(self.use_long, arg))
383            .collect::<Vec<_>>();
384        let non_pos = self
385            .cmd
386            .get_non_positionals()
387            .filter(|a| a.get_help_heading().is_none())
388            .filter(|arg| should_show_arg(self.use_long, arg))
389            .collect::<Vec<_>>();
390        let subcmds = self.cmd.has_visible_subcommands();
391
392        let custom_headings = self
393            .cmd
394            .get_arguments()
395            .filter_map(|arg| arg.get_help_heading())
396            .collect::<FlatSet<_>>();
397
398        let flatten = self.cmd.is_flatten_help_set();
399
400        let mut first = true;
401
402        if subcmds && !flatten {
403            if !first {
404                self.writer.push_str("\n\n");
405            }
406            first = false;
407            let default_help_heading = Str::from("Commands");
408            let help_heading = self
409                .cmd
410                .get_subcommand_help_heading()
411                .unwrap_or(&default_help_heading);
412            let _ = write!(self.writer, "{header}{help_heading}:{header:#}\n",);
413
414            self.write_subcommands(self.cmd);
415        }
416
417        if !pos.is_empty() {
418            if !first {
419                self.writer.push_str("\n\n");
420            }
421            first = false;
422            // Write positional args if any
423            let help_heading = "Arguments";
424            let _ = write!(self.writer, "{header}{help_heading}:{header:#}\n",);
425            self.write_args(&pos, "Arguments", positional_sort_key);
426        }
427
428        if !non_pos.is_empty() {
429            if !first {
430                self.writer.push_str("\n\n");
431            }
432            first = false;
433            let help_heading = "Options";
434            let _ = write!(self.writer, "{header}{help_heading}:{header:#}\n",);
435            self.write_args(&non_pos, "Options", option_sort_key);
436        }
437        if !custom_headings.is_empty() {
438            for heading in custom_headings {
439                let args = self
440                    .cmd
441                    .get_arguments()
442                    .filter(|a| {
443                        if let Some(help_heading) = a.get_help_heading() {
444                            return help_heading == heading;
445                        }
446                        false
447                    })
448                    .filter(|arg| should_show_arg(self.use_long, arg))
449                    .collect::<Vec<_>>();
450
451                if !args.is_empty() {
452                    if !first {
453                        self.writer.push_str("\n\n");
454                    }
455                    first = false;
456                    let _ = write!(self.writer, "{header}{heading}:{header:#}\n",);
457                    self.write_args(&args, heading, option_sort_key);
458                }
459            }
460        }
461        if subcmds && flatten {
462            let mut cmd = self.cmd.clone();
463            cmd.build();
464            self.write_flat_subcommands(&cmd, &mut first);
465        }
466    }
467
468    /// Sorts arguments by length and display order and write their help to the wrapped stream.
469    fn write_args(&mut self, args: &[&Arg], _category: &str, sort_key: ArgSortKey) {
470        debug!("HelpTemplate::write_args {_category}");
471        // The shortest an arg can legally be is 2 (i.e. '-x')
472        let mut longest = 2;
473        let mut ord_v = BTreeMap::new();
474
475        // Determine the longest
476        for &arg in args.iter().filter(|arg| {
477            // If it's NextLineHelp we don't care to compute how long it is because it may be
478            // NextLineHelp on purpose simply *because* it's so long and would throw off all other
479            // args alignment
480            should_show_arg(self.use_long, arg)
481        }) {
482            if longest_filter(arg) {
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);
490                debug!(
491                    "HelpTemplate::write_args: arg={:?} longest={}",
492                    arg.get_id(),
493                    longest
494                );
495            }
496
497            let key = (sort_key)(arg);
498            ord_v.insert(key, arg);
499        }
500
501        let next_line_help = self.will_args_wrap(args, longest);
502
503        for (i, (_, arg)) in ord_v.iter().enumerate() {
504            if i != 0 {
505                self.writer.push_str("\n");
506                if next_line_help && self.use_long {
507                    self.writer.push_str("\n");
508                }
509            }
510            self.write_arg(arg, next_line_help, longest);
511        }
512    }
513
514    /// Writes help for an argument to the wrapped stream.
515    fn write_arg(&mut self, arg: &Arg, next_line_help: bool, longest: usize) {
516        let spec_vals = &self.spec_vals(arg);
517
518        self.writer.push_str(TAB);
519        self.short(arg);
520        self.long(arg);
521        self.writer
522            .push_styled(&arg.stylize_arg_suffix(self.styles, None));
523        self.align_to_about(arg, next_line_help, longest);
524
525        let about = if self.use_long {
526            arg.get_long_help()
527                .or_else(|| arg.get_help())
528                .unwrap_or_default()
529        } else {
530            arg.get_help()
531                .or_else(|| arg.get_long_help())
532                .unwrap_or_default()
533        };
534
535        self.help(Some(arg), about, spec_vals, next_line_help, longest);
536    }
537
538    /// Writes argument's short command to the wrapped stream.
539    fn short(&mut self, arg: &Arg) {
540        debug!("HelpTemplate::short");
541        use std::fmt::Write as _;
542        let literal = &self.styles.get_literal();
543
544        if let Some(s) = arg.get_short() {
545            let _ = write!(self.writer, "{literal}-{s}{literal:#}",);
546        } else if arg.get_long().is_some() {
547            self.writer.push_str("    ");
548        }
549    }
550
551    /// Writes argument's long command to the wrapped stream.
552    fn long(&mut self, arg: &Arg) {
553        debug!("HelpTemplate::long");
554        use std::fmt::Write as _;
555        let literal = &self.styles.get_literal();
556
557        if let Some(long) = arg.get_long() {
558            if arg.get_short().is_some() {
559                self.writer.push_str(", ");
560            }
561            let _ = write!(self.writer, "{literal}--{long}{literal:#}",);
562        }
563    }
564
565    /// Write alignment padding between arg's switches/values and its about message.
566    fn align_to_about(&mut self, arg: &Arg, next_line_help: bool, longest: usize) {
567        debug!(
568            "HelpTemplate::align_to_about: arg={}, next_line_help={}, longest={}",
569            arg.get_id(),
570            next_line_help,
571            longest
572        );
573        let padding = if self.use_long || next_line_help {
574            // long help prints messages on the next line so it doesn't need to align text
575            debug!("HelpTemplate::align_to_about: printing long help so skip alignment");
576            0
577        } else if !arg.is_positional() {
578            let self_len = display_width(&arg.to_string()) + SHORT_SIZE;
579            // Since we're writing spaces from the tab point we first need to know if we
580            // had a long and short, or just short
581            let padding = if arg.get_long().is_some() {
582                // Only account 4 after the val
583                TAB_WIDTH
584            } else {
585                // Only account for ', --' + 4 after the val
586                TAB_WIDTH + 4
587            };
588            let spcs = longest + padding - self_len;
589            debug!(
590                "HelpTemplate::align_to_about: positional=false arg_len={self_len}, spaces={spcs}"
591            );
592
593            spcs
594        } else {
595            let self_len = display_width(&arg.to_string());
596            let padding = TAB_WIDTH;
597            let spcs = longest + padding - self_len;
598            debug!(
599                "HelpTemplate::align_to_about: positional=true arg_len={self_len}, spaces={spcs}",
600            );
601
602            spcs
603        };
604
605        self.write_padding(padding);
606    }
607
608    /// Writes argument's help to the wrapped stream.
609    fn help(
610        &mut self,
611        arg: Option<&Arg>,
612        about: &StyledStr,
613        spec_vals: &str,
614        next_line_help: bool,
615        longest: usize,
616    ) {
617        debug!("HelpTemplate::help");
618        use std::fmt::Write as _;
619        let literal = &self.styles.get_literal();
620
621        // Is help on next line, if so then indent
622        if next_line_help {
623            debug!("HelpTemplate::help: Next Line...{next_line_help:?}");
624            self.writer.push_str("\n");
625            self.writer.push_str(TAB);
626            self.writer.push_str(NEXT_LINE_INDENT);
627        }
628
629        let spaces = if next_line_help {
630            TAB.len() + NEXT_LINE_INDENT.len()
631        } else {
632            longest + TAB_WIDTH * 2
633        };
634        let trailing_indent = spaces; // Don't indent any further than the first line is indented
635        let trailing_indent = self.get_spaces(trailing_indent);
636
637        let mut help = about.clone();
638        help.replace_newline_var();
639        if !spec_vals.is_empty() {
640            if !help.is_empty() {
641                let sep = if self.use_long && arg.is_some() {
642                    "\n\n"
643                } else {
644                    " "
645                };
646                help.push_str(sep);
647            }
648            help.push_str(spec_vals);
649        }
650        let avail_chars = self.term_w.saturating_sub(spaces);
651        debug!(
652            "HelpTemplate::help: help_width={}, spaces={}, avail={}",
653            spaces,
654            help.display_width(),
655            avail_chars
656        );
657        help.wrap(avail_chars);
658        help.indent("", &trailing_indent);
659        let help_is_empty = help.is_empty();
660        self.writer.push_styled(&help);
661        if let Some(arg) = arg {
662            if !arg.is_hide_possible_values_set() && self.use_long_pv(arg) {
663                const DASH_SPACE: usize = "- ".len();
664                let possible_vals = arg.get_possible_values();
665                if !possible_vals.is_empty() {
666                    debug!("HelpTemplate::help: Found possible vals...{possible_vals:?}");
667                    let longest = possible_vals
668                        .iter()
669                        .filter(|f| !f.is_hide_set())
670                        .map(|f| display_width(f.get_name()))
671                        .max()
672                        .expect("Only called with possible value");
673
674                    let spaces = spaces + TAB_WIDTH - DASH_SPACE;
675                    let trailing_indent = spaces + DASH_SPACE;
676                    let trailing_indent = self.get_spaces(trailing_indent);
677
678                    if !help_is_empty {
679                        let _ = write!(self.writer, "\n\n{:spaces$}", "");
680                    }
681                    self.writer.push_str("Possible values:");
682                    for pv in possible_vals.iter().filter(|pv| !pv.is_hide_set()) {
683                        let name = pv.get_name();
684
685                        let mut descr = StyledStr::new();
686                        let _ = write!(&mut descr, "{literal}{name}{literal:#}",);
687                        if let Some(help) = pv.get_help() {
688                            debug!("HelpTemplate::help: Possible Value help");
689                            // To align help messages
690                            let padding = longest - display_width(name);
691                            let _ = write!(&mut descr, ": {:padding$}", "");
692                            descr.push_styled(help);
693                        }
694
695                        let avail_chars = if self.term_w > trailing_indent.len() {
696                            self.term_w - trailing_indent.len()
697                        } else {
698                            usize::MAX
699                        };
700                        descr.replace_newline_var();
701                        descr.wrap(avail_chars);
702                        descr.indent("", &trailing_indent);
703
704                        let _ = write!(self.writer, "\n{:spaces$}- ", "",);
705                        self.writer.push_styled(&descr);
706                    }
707                }
708            }
709        }
710    }
711
712    /// Will use next line help on writing args.
713    fn will_args_wrap(&self, args: &[&Arg], longest: usize) -> bool {
714        args.iter()
715            .filter(|arg| should_show_arg(self.use_long, arg))
716            .any(|arg| {
717                let spec_vals = &self.spec_vals(arg);
718                self.arg_next_line_help(arg, spec_vals, longest)
719            })
720    }
721
722    fn arg_next_line_help(&self, arg: &Arg, spec_vals: &str, longest: usize) -> bool {
723        if self.next_line_help || arg.is_next_line_help_set() || self.use_long {
724            // setting_next_line
725            true
726        } else {
727            // force_next_line
728            let h = arg
729                .get_help()
730                .or_else(|| arg.get_long_help())
731                .unwrap_or_default();
732            let h_w = h.display_width() + display_width(spec_vals);
733            let taken = longest + TAB_WIDTH * 2;
734            self.term_w >= taken
735                && (taken as f32 / self.term_w as f32) > 0.40
736                && h_w > (self.term_w - taken)
737        }
738    }
739
740    fn spec_vals(&self, a: &Arg) -> String {
741        debug!("HelpTemplate::spec_vals: a={a}");
742        let mut spec_vals = Vec::new();
743        #[cfg(feature = "env")]
744        if let Some(ref env) = a.env {
745            if !a.is_hide_env_set() {
746                debug!(
747                    "HelpTemplate::spec_vals: Found environment variable...[{:?}:{:?}]",
748                    env.0, env.1
749                );
750                let env_val = if !a.is_hide_env_values_set() {
751                    format!(
752                        "={}",
753                        env.1
754                            .as_ref()
755                            .map(|s| s.to_string_lossy())
756                            .unwrap_or_default()
757                    )
758                } else {
759                    Default::default()
760                };
761                let env_info = format!("[env: {}{}]", env.0.to_string_lossy(), env_val);
762                spec_vals.push(env_info);
763            }
764        }
765        if a.is_takes_value_set() && !a.is_hide_default_value_set() && !a.default_vals.is_empty() {
766            debug!(
767                "HelpTemplate::spec_vals: Found default value...[{:?}]",
768                a.default_vals
769            );
770
771            let pvs = a
772                .default_vals
773                .iter()
774                .map(|pvs| pvs.to_string_lossy())
775                .map(|pvs| {
776                    if pvs.contains(char::is_whitespace) {
777                        Cow::from(format!("{pvs:?}"))
778                    } else {
779                        pvs
780                    }
781                })
782                .collect::<Vec<_>>()
783                .join(" ");
784
785            spec_vals.push(format!("[default: {pvs}]"));
786        }
787
788        let mut als = Vec::new();
789
790        let short_als = a
791            .short_aliases
792            .iter()
793            .filter(|&als| als.1) // visible
794            .map(|als| format!("-{}", als.0)); // name
795        debug!(
796            "HelpTemplate::spec_vals: Found short aliases...{:?}",
797            a.short_aliases
798        );
799        als.extend(short_als);
800
801        let long_als = a
802            .aliases
803            .iter()
804            .filter(|&als| als.1) // visible
805            .map(|als| format!("--{}", als.0)); // name
806        debug!("HelpTemplate::spec_vals: Found aliases...{:?}", a.aliases);
807        als.extend(long_als);
808
809        if !als.is_empty() {
810            spec_vals.push(format!("[aliases: {}]", als.join(", ")));
811        }
812
813        if !a.is_hide_possible_values_set() && !self.use_long_pv(a) {
814            let possible_vals = a.get_possible_values();
815            if !possible_vals.is_empty() {
816                debug!("HelpTemplate::spec_vals: Found possible vals...{possible_vals:?}");
817
818                let pvs = possible_vals
819                    .iter()
820                    .filter_map(PossibleValue::get_visible_quoted_name)
821                    .collect::<Vec<_>>()
822                    .join(", ");
823
824                spec_vals.push(format!("[possible values: {pvs}]"));
825            }
826        }
827        let connector = if self.use_long { "\n" } else { " " };
828        spec_vals.join(connector)
829    }
830
831    fn get_spaces(&self, n: usize) -> String {
832        " ".repeat(n)
833    }
834
835    fn write_padding(&mut self, amount: usize) {
836        use std::fmt::Write as _;
837        let _ = write!(self.writer, "{:amount$}", "");
838    }
839
840    fn use_long_pv(&self, arg: &Arg) -> bool {
841        self.use_long
842            && arg
843                .get_possible_values()
844                .iter()
845                .any(PossibleValue::should_show_help)
846    }
847}
848
849/// Subcommand handling
850impl HelpTemplate<'_, '_> {
851    /// Writes help for subcommands of a Parser Object to the wrapped stream.
852    fn write_flat_subcommands(&mut self, cmd: &Command, first: &mut bool) {
853        debug!(
854            "HelpTemplate::write_flat_subcommands, cmd={}, first={}",
855            cmd.get_name(),
856            *first
857        );
858        use std::fmt::Write as _;
859        let header = &self.styles.get_header();
860
861        let mut ord_v = BTreeMap::new();
862        for subcommand in cmd
863            .get_subcommands()
864            .filter(|subcommand| should_show_subcommand(subcommand))
865        {
866            ord_v.insert(
867                (subcommand.get_display_order(), subcommand.get_name()),
868                subcommand,
869            );
870        }
871        for (_, subcommand) in ord_v {
872            if !*first {
873                self.writer.push_str("\n\n");
874            }
875            *first = false;
876
877            let heading = subcommand.get_usage_name_fallback();
878            let about = subcommand
879                .get_about()
880                .or_else(|| subcommand.get_long_about())
881                .unwrap_or_default();
882
883            let _ = write!(self.writer, "{header}{heading}:{header:#}",);
884            if !about.is_empty() {
885                let _ = write!(self.writer, "\n{about}",);
886            }
887
888            let args = subcommand
889                .get_arguments()
890                .filter(|arg| should_show_arg(self.use_long, arg) && !arg.is_global_set())
891                .collect::<Vec<_>>();
892            if !args.is_empty() {
893                self.writer.push_str("\n");
894            }
895
896            let mut sub_help = HelpTemplate {
897                writer: self.writer,
898                cmd: subcommand,
899                styles: self.styles,
900                usage: self.usage,
901                next_line_help: self.next_line_help,
902                term_w: self.term_w,
903                use_long: self.use_long,
904            };
905            sub_help.write_args(&args, heading, option_sort_key);
906            if subcommand.is_flatten_help_set() {
907                sub_help.write_flat_subcommands(subcommand, first);
908            }
909        }
910    }
911
912    /// Writes help for subcommands of a Parser Object to the wrapped stream.
913    fn write_subcommands(&mut self, cmd: &Command) {
914        debug!("HelpTemplate::write_subcommands");
915        use std::fmt::Write as _;
916        let literal = &self.styles.get_literal();
917
918        // The shortest an arg can legally be is 2 (i.e. '-x')
919        let mut longest = 2;
920        let mut ord_v = BTreeMap::new();
921        for subcommand in cmd
922            .get_subcommands()
923            .filter(|subcommand| should_show_subcommand(subcommand))
924        {
925            let mut styled = StyledStr::new();
926            let name = subcommand.get_name();
927            let _ = write!(styled, "{literal}{name}{literal:#}",);
928            if let Some(short) = subcommand.get_short_flag() {
929                let _ = write!(styled, ", {literal}-{short}{literal:#}",);
930            }
931            if let Some(long) = subcommand.get_long_flag() {
932                let _ = write!(styled, ", {literal}--{long}{literal:#}",);
933            }
934            longest = longest.max(styled.display_width());
935            ord_v.insert((subcommand.get_display_order(), styled), subcommand);
936        }
937
938        debug!("HelpTemplate::write_subcommands longest = {longest}");
939
940        let next_line_help = self.will_subcommands_wrap(cmd.get_subcommands(), longest);
941
942        for (i, (sc_str, sc)) in ord_v.into_iter().enumerate() {
943            if 0 < i {
944                self.writer.push_str("\n");
945            }
946            self.write_subcommand(sc_str.1, sc, next_line_help, longest);
947        }
948    }
949
950    /// Will use next line help on writing subcommands.
951    fn will_subcommands_wrap<'a>(
952        &self,
953        subcommands: impl IntoIterator<Item = &'a Command>,
954        longest: usize,
955    ) -> bool {
956        subcommands
957            .into_iter()
958            .filter(|&subcommand| should_show_subcommand(subcommand))
959            .any(|subcommand| {
960                let spec_vals = &self.sc_spec_vals(subcommand);
961                self.subcommand_next_line_help(subcommand, spec_vals, longest)
962            })
963    }
964
965    fn write_subcommand(
966        &mut self,
967        sc_str: StyledStr,
968        cmd: &Command,
969        next_line_help: bool,
970        longest: usize,
971    ) {
972        debug!("HelpTemplate::write_subcommand");
973
974        let spec_vals = &self.sc_spec_vals(cmd);
975
976        let about = cmd
977            .get_about()
978            .or_else(|| cmd.get_long_about())
979            .unwrap_or_default();
980
981        self.subcmd(sc_str, next_line_help, longest);
982        self.help(None, about, spec_vals, next_line_help, longest);
983    }
984
985    fn sc_spec_vals(&self, a: &Command) -> String {
986        debug!("HelpTemplate::sc_spec_vals: a={}", a.get_name());
987        let mut spec_vals = vec![];
988
989        let mut short_als = a
990            .get_visible_short_flag_aliases()
991            .map(|a| format!("-{a}"))
992            .collect::<Vec<_>>();
993        let als = a.get_visible_aliases().map(|s| s.to_string());
994        short_als.extend(als);
995        let all_als = short_als.join(", ");
996        if !all_als.is_empty() {
997            debug!(
998                "HelpTemplate::spec_vals: Found aliases...{:?}",
999                a.get_all_aliases().collect::<Vec<_>>()
1000            );
1001            debug!(
1002                "HelpTemplate::spec_vals: Found short flag aliases...{:?}",
1003                a.get_all_short_flag_aliases().collect::<Vec<_>>()
1004            );
1005            spec_vals.push(format!("[aliases: {all_als}]"));
1006        }
1007
1008        spec_vals.join(" ")
1009    }
1010
1011    fn subcommand_next_line_help(&self, cmd: &Command, spec_vals: &str, longest: usize) -> bool {
1012        // Ignore `self.use_long` since subcommands are only shown as short help
1013        if self.next_line_help {
1014            // setting_next_line
1015            true
1016        } else {
1017            // force_next_line
1018            let h = cmd
1019                .get_about()
1020                .or_else(|| cmd.get_long_about())
1021                .unwrap_or_default();
1022            let h_w = h.display_width() + display_width(spec_vals);
1023            let taken = longest + TAB_WIDTH * 2;
1024            self.term_w >= taken
1025                && (taken as f32 / self.term_w as f32) > 0.40
1026                && h_w > (self.term_w - taken)
1027        }
1028    }
1029
1030    /// Writes subcommand to the wrapped stream.
1031    fn subcmd(&mut self, sc_str: StyledStr, next_line_help: bool, longest: usize) {
1032        self.writer.push_str(TAB);
1033        self.writer.push_styled(&sc_str);
1034        if !next_line_help {
1035            let width = sc_str.display_width();
1036            let padding = longest + TAB_WIDTH - width;
1037            self.write_padding(padding);
1038        }
1039    }
1040}
1041
1042const NEXT_LINE_INDENT: &str = "        ";
1043
1044type ArgSortKey = fn(arg: &Arg) -> (usize, String);
1045
1046fn positional_sort_key(arg: &Arg) -> (usize, String) {
1047    (arg.get_index().unwrap_or(0), String::new())
1048}
1049
1050fn option_sort_key(arg: &Arg) -> (usize, String) {
1051    // Formatting key like this to ensure that:
1052    // 1. Argument has long flags are printed just after short flags.
1053    // 2. For two args both have short flags like `-c` and `-C`, the
1054    //    `-C` arg is printed just after the `-c` arg
1055    // 3. For args without short or long flag, print them at last(sorted
1056    //    by arg name).
1057    // Example order: -a, -b, -B, -s, --select-file, --select-folder, -x
1058
1059    let key = if let Some(x) = arg.get_short() {
1060        let mut s = x.to_ascii_lowercase().to_string();
1061        s.push(if x.is_ascii_lowercase() { '0' } else { '1' });
1062        s
1063    } else if let Some(x) = arg.get_long() {
1064        x.to_string()
1065    } else {
1066        let mut s = '{'.to_string();
1067        s.push_str(arg.get_id().as_str());
1068        s
1069    };
1070    (arg.get_display_order(), key)
1071}
1072
1073pub(crate) fn dimensions() -> (Option<usize>, Option<usize>) {
1074    #[cfg(not(feature = "wrap_help"))]
1075    return (None, None);
1076
1077    #[cfg(feature = "wrap_help")]
1078    terminal_size::terminal_size()
1079        .map(|(w, h)| (Some(w.0.into()), Some(h.0.into())))
1080        .unwrap_or_else(|| (parse_env("COLUMNS"), parse_env("LINES")))
1081}
1082
1083#[cfg(feature = "wrap_help")]
1084fn parse_env(var: &str) -> Option<usize> {
1085    some!(some!(std::env::var_os(var)).to_str())
1086        .parse::<usize>()
1087        .ok()
1088}
1089
1090fn should_show_arg(use_long: bool, arg: &Arg) -> bool {
1091    debug!(
1092        "should_show_arg: use_long={:?}, arg={}",
1093        use_long,
1094        arg.get_id()
1095    );
1096    if arg.is_hide_set() {
1097        return false;
1098    }
1099    (!arg.is_hide_long_help_set() && use_long)
1100        || (!arg.is_hide_short_help_set() && !use_long)
1101        || arg.is_next_line_help_set()
1102}
1103
1104fn should_show_subcommand(subcommand: &Command) -> bool {
1105    !subcommand.is_hide_set()
1106}
1107
1108fn longest_filter(arg: &Arg) -> bool {
1109    arg.is_takes_value_set() || arg.get_long().is_some() || arg.get_short().is_none()
1110}
1111
1112#[cfg(test)]
1113mod test {
1114    #[test]
1115    #[cfg(feature = "wrap_help")]
1116    fn wrap_help_last_word() {
1117        use super::*;
1118
1119        let help = String::from("foo bar baz");
1120        assert_eq!(wrap(&help, 5), "foo\nbar\nbaz");
1121    }
1122
1123    #[test]
1124    #[cfg(feature = "unicode")]
1125    fn display_width_handles_non_ascii() {
1126        use super::*;
1127
1128        // Popular Danish tongue-twister, the name of a fruit dessert.
1129        let text = "rødgrød med fløde";
1130        assert_eq!(display_width(text), 17);
1131        // Note that the string width is smaller than the string
1132        // length. This is due to the precomposed non-ASCII letters:
1133        assert_eq!(text.len(), 20);
1134    }
1135
1136    #[test]
1137    #[cfg(feature = "unicode")]
1138    fn display_width_handles_emojis() {
1139        use super::*;
1140
1141        let text = "😂";
1142        // There is a single `char`...
1143        assert_eq!(text.chars().count(), 1);
1144        // but it is double-width:
1145        assert_eq!(display_width(text), 2);
1146        // This is much less than the byte length:
1147        assert_eq!(text.len(), 4);
1148    }
1149}