xref: /third_party/rust/crates/clap/src/output/help.rs (revision 19625d8c)
1#![cfg_attr(not(feature = "help"), allow(unused_variables))]
2
3// Internal
4use crate::builder::Command;
5use crate::builder::StyledStr;
6use crate::output::Usage;
7
8/// Writes the parser help to the wrapped stream.
9pub(crate) fn write_help(writer: &mut StyledStr, cmd: &Command, usage: &Usage<'_>, use_long: bool) {
10    debug!("write_help");
11
12    if let Some(h) = cmd.get_override_help() {
13        writer.extend(h.iter());
14    } else {
15        #[cfg(feature = "help")]
16        {
17            use super::AutoHelp;
18            use super::HelpTemplate;
19            if let Some(tmpl) = cmd.get_help_template() {
20                for (style, content) in tmpl.iter() {
21                    if style.is_none() {
22                        HelpTemplate::new(writer, cmd, usage, use_long)
23                            .write_templated_help(content);
24                    } else {
25                        writer.stylize(style, content);
26                    }
27                }
28            } else {
29                AutoHelp::new(writer, cmd, usage, use_long).write_help();
30            }
31        }
32
33        #[cfg(not(feature = "help"))]
34        {
35            debug!("write_help: no help, `Command::override_help` and `help` is missing");
36        }
37    }
38
39    // Remove any extra lines caused by book keeping
40    writer.trim();
41    // Ensure there is still a trailing newline
42    writer.none("\n");
43}
44