use std::fmt::Display;
use std::path::PathBuf;
use clap::ArgGroup;
use clap::{Parser, Subcommand};
use clap_verbosity_flag::Verbosity;
use crate::property_file_reader::Delimiter;
#[derive(Parser, Debug)]
#[command(
author,
version,
about,
long_about = "Procon (Pro)perty (Con)verter \
\nA program to convert between different property formats.
\nExamples:
\nProperty -> Json
\n\tprocon json example.properties
\nProperty -> Yaml
\n\tprocon yaml example.properties
\nJson -> Properties
\n\tprocon properties example.json
"
)]
#[command(propagate_version = true)]
#[command(group(ArgGroup::new("from")
.multiple(false)
.args(["from_property_file", "from_yaml_file", "from_json_file"]),
))]
#[command(group(ArgGroup::new("dry-run")
.multiple(false)
.args(["dry_run", "output_filename"]),
))]
pub struct Args {
#[command(subcommand)]
pub target_format: TargetFormat,
#[arg(short, long, default_value_t = false)]
pub dry_run: bool,
#[arg(short = 'p', long)]
pub from_property_file: bool,
#[arg(short = 'y', long)]
pub from_yaml_file: bool,
#[arg(short = 'j', long)]
pub from_json_file: bool,
#[arg(short, long)]
pub output_filename: Option<String>,
#[clap(flatten)]
pub verbose: Verbosity,
}
#[derive(Subcommand, Debug)]
pub enum TargetFormat {
Properties {
#[arg(short, long, default_value_t = Delimiter::Equals)]
property_delimiter: Delimiter,
file: PathBuf,
},
Yaml {
#[arg(short, long, default_value_t = Delimiter::Equals)]
property_delimiter: Delimiter,
file: PathBuf,
},
Json {
#[arg(short, long, default_value_t = Delimiter::Equals)]
property_delimiter: Delimiter,
file: PathBuf,
},
}
impl Display for TargetFormat {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{:?}", self)
}
}
#[allow(dead_code)]
impl TargetFormat {
pub fn path_buf(&self) -> &PathBuf {
match self {
TargetFormat::Properties { file, .. } => file,
TargetFormat::Json { file, .. } => file,
TargetFormat::Yaml { file, .. } => file,
}
}
pub fn delimiter(&self) -> Option<&Delimiter> {
match self {
TargetFormat::Properties {
property_delimiter, ..
} => Some(property_delimiter),
TargetFormat::Yaml {
property_delimiter, ..
} => Some(property_delimiter),
TargetFormat::Json {
property_delimiter, ..
} => Some(property_delimiter),
}
}
}