dia_args/docs/
cmd.rs

1/*
2==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--
3
4Dia-Args
5
6Copyright (C) 2018-2019, 2021-2025  Anonymous
7
8There are several releases over multiple years,
9they are listed as ranges, such as: "2018-2019".
10
11This program is free software: you can redistribute it and/or modify
12it under the terms of the GNU Lesser General Public License as published by
13the Free Software Foundation, either version 3 of the License, or
14(at your option) any later version.
15
16This program is distributed in the hope that it will be useful,
17but WITHOUT ANY WARRANTY; without even the implied warranty of
18MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19GNU Lesser General Public License for more details.
20
21You should have received a copy of the GNU Lesser General Public License
22along with this program.  If not, see <https://www.gnu.org/licenses/>.
23
24::--::--::--::--::--::--::--::--::--::--::--::--::--::--::--::--
25*/
26
27//! # Command
28
29use {
30    core::{
31        fmt,
32        option::Option as RustOption,
33    },
34    std::borrow::Cow,
35    super::{Cfg, I18n, Option},
36};
37
38/// # Command
39///
40/// ## Examples
41///
42/// ```
43/// use std::borrow::Cow;
44/// use dia_args::docs::Cmd;
45///
46/// // All these constants are convenient while working with Args.
47/// // And you can also use them for Cmd.
48///
49/// const CMD_HELP: &str = "help";
50/// const CMD_HELP_DOCS: Cow<str> = Cow::Borrowed("Prints help and exits.");
51///
52/// let _cmd = Cmd::new(CMD_HELP, CMD_HELP_DOCS, None);
53/// // Here you can pass this command to Docs::new(...)
54/// ```
55#[derive(Clone)]
56pub struct Cmd<'a> {
57    name: &'a str,
58    docs: Cow<'a, str>,
59    options: RustOption<Vec<Cow<'a, Option<'a>>>>,
60}
61
62impl<'a> Cmd<'a> {
63
64    /// # Makes new instance
65    pub const fn new(name: &'a str, docs: Cow<'a, str>, options: RustOption<Vec<Cow<'a, Option<'a>>>>) -> Self {
66        Self {
67            name,
68            docs,
69            options,
70        }
71    }
72
73    /// # Formats self
74    pub fn format(&self, cfg: &Cfg, i18n: &I18n, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
75        // Name
76        let tab = cfg.tab_len().saturating_mul(cfg.tab_level().into());
77        f.write_str(&super::format(self.name, tab, cfg.columns()))?;
78        f.write_str(super::LINE_BREAK)?;
79
80        // Docs
81        let cfg = cfg.increment_level();
82        let tab = cfg.tab_len().saturating_mul(cfg.tab_level().into());
83        f.write_str(&super::format(&self.docs, tab, cfg.columns()))?;
84        f.write_str(super::LINE_BREAK)?;
85
86        // Options
87        if let Some(options) = self.options.as_ref() {
88            f.write_str(&super::format(i18n.options.to_uppercase(), tab, cfg.columns()))?;
89            f.write_str(super::LINE_BREAK)?;
90
91            let cfg = cfg.increment_level();
92            for option in options {
93                option.format(&cfg, &i18n, f)?;
94            }
95        }
96
97        Ok(())
98    }
99
100}
101
102impl<'a> From<Cmd<'a>> for Cow<'a, Cmd<'a>> {
103
104    fn from(cmd: Cmd<'a>) -> Self {
105        Cow::Owned(cmd)
106    }
107
108}
109
110impl<'a> From<&'a Cmd<'a>> for Cow<'a, Cmd<'a>> {
111
112    fn from(cmd: &'a Cmd<'a>) -> Self {
113        Cow::Borrowed(cmd)
114    }
115
116}