cargo_component_core/
command.rs1use std::path::PathBuf;
3
4use clap::{ArgAction, Args};
5
6use crate::terminal::{Color, Terminal, Verbosity};
7
8pub const CACHE_DIR_ENV_VAR: &str = "CARGO_COMPONENT_CACHE_DIR";
10pub const CONFIG_FILE_ENV_VAR: &str = "CARGO_COMPONENT_CONFIG_FILE";
12
13#[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 #[clap(long = "quiet", short = 'q')]
21 pub quiet: bool,
22
23 #[clap(
25 long = "verbose",
26 short = 'v',
27 action = ArgAction::Count
28 )]
29 pub verbose: u8,
30
31 #[clap(long = "color", value_name = "WHEN")]
33 pub color: Option<Color>,
34
35 #[clap(long = "cache-dir", env = CACHE_DIR_ENV_VAR)]
37 pub cache_dir: Option<PathBuf>,
38
39 #[clap(long = "config", env = CONFIG_FILE_ENV_VAR)]
41 pub config: Option<PathBuf>,
42}
43
44impl CommonOptions {
45 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}