use clap::Parser;
use std::path::PathBuf;
#[derive(Debug, Clone, Copy, Default, clap::ValueEnum)]
pub enum ColorChoice {
#[default]
Auto,
Always,
Never,
}
impl ColorChoice {
pub fn apply(self) {
match self {
Self::Auto => {} Self::Always => owo_colors::set_override(true),
Self::Never => owo_colors::set_override(false),
}
}
}
#[derive(Parser, Debug)]
pub struct CommonArgs {
#[arg(long)]
pub version_only: bool,
#[arg(short = 'C', long, global = true)]
pub chdir: Option<PathBuf>,
#[arg(short, long, global = true)]
pub quiet: bool,
#[arg(short, long, global = true, action = clap::ArgAction::Count)]
pub verbose: u8,
#[arg(long, global = true, value_enum, default_value_t)]
pub color: ColorChoice,
#[arg(long, global = true)]
pub json: bool,
}
impl CommonArgs {
pub fn apply_color(&self) {
self.color.apply();
}
pub fn apply_chdir(&self) -> std::io::Result<()> {
if let Some(ref dir) = self.chdir {
std::env::set_current_dir(dir)?;
}
Ok(())
}
}
pub fn with_help_short(cmd: clap::Command) -> clap::Command {
cmd.arg(
clap::Arg::new("help")
.short('h')
.long("help")
.help("Print help")
.global(true)
.action(clap::ArgAction::HelpShort),
)
}