use super::cmd::Command;
use crate::Context;
#[derive(
Clone,
Debug,
Default,
Eq,
Hash,
Ord,
PartialEq,
PartialOrd,
clap::Parser,
serde::Deserialize,
serde::Serialize,
)]
#[clap(about, author, long_about = None, version)]
#[command(arg_required_else_help(true), allow_missing_positional(true))]
pub struct Cli {
#[clap(subcommand)]
pub command: Option<Command>,
#[clap(long, short = 'C', default_value_t = String::from("Puzzled.toml"))]
pub config: String,
#[clap(action = clap::ArgAction::Count, long, short)]
pub release: u8,
#[arg(action = clap::ArgAction::Count, long, short)]
pub update: u8,
#[arg(action = clap::ArgAction::Count, long, short)]
pub verbose: u8,
}
impl Cli {
#[tracing::instrument(target = "cli")]
pub fn parse() -> Self {
tracing::debug! { "Parsing command line arguments..." }
<Self as clap::Parser>::parse()
}
pub fn command(&self) -> &Option<Command> {
&self.command
}
pub const fn release(&self) -> u8 {
self.release
}
pub const fn verbose(&self) -> u8 {
self.verbose
}
pub const fn update(&self) -> u8 {
self.update
}
pub const fn is_release(&self) -> bool {
self.release > 0
}
pub const fn is_verbose(&self) -> bool {
self.verbose > 0
}
pub const fn is_update(&self) -> bool {
self.update > 0
}
#[tracing::instrument(skip_all, target = "cli")]
pub async fn handle(&self, ctx: &mut Context) -> anyhow::Result<()> {
if let Some(cmd) = &self.command {
tracing::info! { "Executing command: {:?}", cmd.name() }
cmd.handle(ctx).await?;
}
if self.is_release() {
tracing::info!(
"Release flag has been toggled {n} times",
n = self.release()
);
}
if self.is_verbose() {
tracing::info! { "Verbose flag has been toggled {n} times", n = self.verbose() }
}
if self.is_update() {
tracing::info! { "Update flag has been toggled {n} times", n = self.update() }
}
Ok(())
}
}