1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
//! Module for common command implementation.

use crate::terminal::{Color, Terminal, Verbosity};
use clap::{ArgAction, Args};

/// Common options for commands.
#[derive(Args)]
#[command(
    after_help = "Unrecognized subcommands will be passed to cargo verbatim after relevant component bindings are updated."
)]
pub struct CommonOptions {
    /// Do not print log messages
    #[clap(long = "quiet", short = 'q')]
    pub quiet: bool,

    /// Use verbose output (-vv very verbose output)
    #[clap(
        long = "verbose",
        short = 'v',
        action = ArgAction::Count
    )]
    pub verbose: u8,

    /// Coloring: auto, always, never
    #[clap(long = "color", value_name = "WHEN")]
    pub color: Option<Color>,
}

impl CommonOptions {
    /// Creates a new terminal from the common options.
    pub fn new_terminal(&self) -> Terminal {
        Terminal::new(
            if self.quiet {
                Verbosity::Quiet
            } else {
                match self.verbose {
                    0 => Verbosity::Normal,
                    _ => Verbosity::Verbose,
                }
            },
            self.color.unwrap_or_default(),
        )
    }
}