Skip to main content

iceoryx2_cli/
cli.rs

1// Copyright (c) 2024 Contributors to the Eclipse Foundation
2//
3// See the NOTICE file(s) distributed with this work for additional
4// information regarding copyright ownership.
5//
6// This program and the accompanying materials are made available under the
7// terms of the Apache Software License 2.0 which is available at
8// https://www.apache.org/licenses/LICENSE-2.0, or the MIT license
9// which is available at https://opensource.org/licenses/MIT.
10//
11// SPDX-License-Identifier: Apache-2.0 OR MIT
12
13use colored::*;
14
15#[derive(Default)]
16pub struct HelpTemplate {
17    has_positionals: bool,
18    has_subcommands: bool,
19    show_external_command_hint: bool,
20}
21
22pub fn help_template() -> HelpTemplate {
23    HelpTemplate::default()
24}
25
26impl HelpTemplate {
27    pub fn with_positionals(mut self) -> Self {
28        self.has_positionals = true;
29        self
30    }
31
32    pub fn with_subcommands(mut self) -> Self {
33        self.has_subcommands = true;
34        self
35    }
36
37    pub fn with_external_command_hint(mut self) -> Self {
38        self.has_subcommands = true;
39        self.show_external_command_hint = true;
40        self
41    }
42
43    pub fn build(self) -> String {
44        let mut template = format!(
45            "{{about}}\n\n\
46             {} {{usage}}\n\n",
47            "Usage:".bright_green().bold(),
48        );
49
50        if self.has_positionals {
51            template.push_str("{positionals}\n\n");
52        }
53
54        template.push_str(&format!(
55            "{}\n\
56             {{options}}",
57            "Options:".bright_green().bold(),
58        ));
59
60        if self.has_subcommands {
61            template.push_str(&format!(
62                "\n\n\
63                 {}\n\
64                 {{subcommands}}",
65                "Commands:".bright_green().bold(),
66            ));
67
68            if self.show_external_command_hint {
69                template.push_str(&format!(
70                    "\n\
71                     {}{}",
72                    "  ...            ".bold(),
73                    "See external installed commands with --list",
74                ));
75            }
76        }
77
78        template
79    }
80}