cargo_component_core/
command.rs

1//! Module for common command implementation.
2use std::path::PathBuf;
3
4use clap::{ArgAction, Args};
5
6use crate::terminal::{Color, Terminal, Verbosity};
7
8/// The environment variable name for setting a cache directory location
9pub const CACHE_DIR_ENV_VAR: &str = "CARGO_COMPONENT_CACHE_DIR";
10/// The environment variable name for setting a path to a config file
11pub const CONFIG_FILE_ENV_VAR: &str = "CARGO_COMPONENT_CONFIG_FILE";
12
13/// Common options for commands.
14#[derive(Args)]
15#[command(
16    after_help = "Unrecognized subcommands will be passed to cargo verbatim after relevant component bindings are updated."
17)]
18pub struct CommonOptions {
19    /// Do not print log messages
20    #[clap(long = "quiet", short = 'q')]
21    pub quiet: bool,
22
23    /// Use verbose output (-vv very verbose output)
24    #[clap(
25        long = "verbose",
26        short = 'v',
27        action = ArgAction::Count
28    )]
29    pub verbose: u8,
30
31    /// Coloring: auto, always, never
32    #[clap(long = "color", value_name = "WHEN")]
33    pub color: Option<Color>,
34
35    /// The path to the cache directory to store component dependencies.
36    #[clap(long = "cache-dir", env = CACHE_DIR_ENV_VAR)]
37    pub cache_dir: Option<PathBuf>,
38
39    /// The path to the pkg-tools config file
40    #[clap(long = "config", env = CONFIG_FILE_ENV_VAR)]
41    pub config: Option<PathBuf>,
42}
43
44impl CommonOptions {
45    /// Creates a new terminal from the common options.
46    pub fn new_terminal(&self) -> Terminal {
47        Terminal::new(
48            if self.quiet {
49                Verbosity::Quiet
50            } else {
51                match self.verbose {
52                    0 => Verbosity::Normal,
53                    _ => Verbosity::Verbose,
54                }
55            },
56            self.color.unwrap_or_default(),
57        )
58    }
59}