use std::fmt::Display;
use libpt::log::{Level, Logger};
use clap::{Parser, Subcommand};
use clap_verbosity_flag::{InfoLevel, Verbosity};
#[derive(Debug, Clone, Parser)]
#[command(
author,
version,
about,
long_about,
help_template = r#"{about-section}
{usage-heading} {usage}
{all-args}{tab}
autocrate: {version}
Author: {author-with-newline}
"#
)]
pub struct Cli {
#[command(flatten)]
pub verbose: Verbosity<InfoLevel>,
#[arg(long)]
pub meta: bool,
#[command(subcommand)]
pub command: Commands,
}
#[derive(Debug, Clone, Subcommand)]
pub enum Commands {
Changelog {},
Release {
#[arg(short, long)]
message: Option<Vec<String>>,
changelog: bool,
publish: bool,
},
Publish {
#[arg(short, long)]
message: Option<Vec<String>>,
},
Version {},
Init {},
}
impl Display for Commands {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"{}",
match self {
Self::Changelog { .. } => "Changelog",
Self::Release { .. } => "Release",
Self::Publish { .. } => "Publish",
Self::Version { .. } => "Version",
Self::Init { .. } => "Init",
}
)
}
}
impl Cli {
pub fn cli_parse() -> Self {
let cli = Self::parse();
let ll: Level = match cli.verbose.log_level().unwrap().as_str() {
"TRACE" => Level::TRACE,
"DEBUG" => Level::DEBUG,
"INFO" => Level::INFO,
"WARN" => Level::WARN,
"ERROR" => Level::ERROR,
_ => {
unreachable!();
}
};
if cli.meta {
Logger::init(None, Some(ll), true).expect("could not initialize Logger");
} else {
Logger::init_mini(Some(ll)).expect("could not initialize Logger");
}
cli
}
}