use clap::{Parser, Subcommand, ValueHint};
use super::cmds;
use super::model;
use super::path;
#[derive(Clone, Debug, Default, Parser)]
#[command(name = "garden")]
#[command(author, version, about, long_about = None)]
pub struct MainOptions {
#[arg(
long,
require_equals = true,
num_args = 0..=1,
default_value_t = model::ColorMode::Auto,
default_missing_value = "true",
value_name = "WHEN",
value_parser = model::ColorMode::parse_from_str,
)]
color: model::ColorMode,
#[arg(long, short, value_hint = ValueHint::FilePath)]
pub config: Option<std::path::PathBuf>,
#[arg(long, short = 'C', value_hint = ValueHint::DirPath)]
chdir: Option<std::path::PathBuf>,
#[arg(long, short, action = clap::ArgAction::Append)]
pub debug: Vec<String>,
#[arg(long, short = 'D')]
pub define: Vec<String>,
#[arg(long, short, value_hint = ValueHint::DirPath)]
pub root: Option<std::path::PathBuf>,
#[arg(short, long)]
pub quiet: bool,
#[arg(short, long, action = clap::ArgAction::Count)]
pub verbose: u8,
#[command(subcommand)]
pub command: Command,
}
impl MainOptions {
pub fn new() -> Self {
Self::default()
}
pub fn update(&mut self) {
self.color.update();
if let Some(config) = &self.config {
if config.exists() {
self.config = Some(path::abspath(config));
}
}
if let Some(root) = &self.root {
self.root = Some(path::abspath(root));
}
if let Some(chdir) = &self.chdir {
if let Err(err) = std::env::set_current_dir(chdir) {
error!("could not chdir to {:?}: {}", chdir, err);
}
}
}
pub fn debug_level(&self, name: &str) -> u8 {
debug_level(&self.debug, name)
}
}
pub fn debug_level(debug: &[String], name: &str) -> u8 {
debug.iter().filter(|&x| x == name).count() as u8
}
#[derive(Clone, Debug, Subcommand)]
pub enum Command {
Cmd(cmds::cmd::CmdOptions),
Completion(cmds::completion::CompletionOptions),
#[command(external_subcommand)]
Custom(Vec<String>),
Eval(cmds::eval::EvalOptions),
Exec(cmds::exec::ExecOptions),
Grow(cmds::grow::GrowOptions),
Init(cmds::init::InitOptions),
Inspect(cmds::inspect::InspectOptions),
#[command(alias = "ls")]
List(cmds::list::ListOptions),
Plant(cmds::plant::PlantOptions),
Prune(cmds::prune::PruneOptions),
#[command(alias = "sh")]
Shell(cmds::shell::ShellOptions),
}
impl std::default::Default for Command {
fn default() -> Self {
Command::Custom(vec![])
}
}