1use std::fmt::Display;
2use std::path::PathBuf;
3
4use clap::ArgGroup;
5use clap::{Parser, Subcommand};
6use clap_verbosity_flag::Verbosity;
7
8use crate::property_file_reader::Delimiter;
9
10#[derive(Parser, Debug)]
11#[command(
12 author,
13 version,
14 about,
15 long_about = "Procon (Pro)perty (Con)verter \
16 \nA program to convert between different property formats.
17 \nExamples:
18 \nProperty -> Json
19 \n\tprocon json example.properties
20 \nProperty -> Yaml
21 \n\tprocon yaml example.properties
22 \nJson -> Properties
23 \n\tprocon properties example.json
24 "
25)]
26#[command(propagate_version = true)]
27#[command(group(ArgGroup::new("from")
28.multiple(false)
29.args(["from_property_file", "from_yaml_file", "from_json_file"]),
30))]
31#[command(group(ArgGroup::new("dry-run")
32.multiple(false)
33.args(["dry_run", "output_filename"]),
34))]
35pub struct Args {
36 #[command(subcommand)]
37 pub target_format: TargetFormat,
38
39 #[arg(short, long, default_value_t = false)]
45 pub dry_run: bool,
46
47 #[arg(short = 'p', long)]
51 pub from_property_file: bool,
52
53 #[arg(short = 'y', long)]
57 pub from_yaml_file: bool,
58
59 #[arg(short = 'j', long)]
63 pub from_json_file: bool,
64
65 #[arg(short, long)]
69 pub output_filename: Option<String>,
70
71 #[clap(flatten)]
72 pub verbose: Verbosity,
73}
74
75#[derive(Subcommand, Debug)]
76pub enum TargetFormat {
77 Properties {
79 #[arg(short, long, default_value_t = Delimiter::Equals)]
83 property_delimiter: Delimiter,
84 file: PathBuf,
86 },
87
88 Yaml {
90 #[arg(short, long, default_value_t = Delimiter::Equals)]
94 property_delimiter: Delimiter,
95
96 file: PathBuf,
98 },
99
100 Json {
102 #[arg(short, long, default_value_t = Delimiter::Equals)]
106 property_delimiter: Delimiter,
107
108 file: PathBuf,
110 },
111}
112
113impl Display for TargetFormat {
114 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
115 write!(f, "{:?}", self)
116 }
117}
118
119#[allow(dead_code)]
120impl TargetFormat {
121 pub fn path_buf(&self) -> &PathBuf {
122 match self {
123 TargetFormat::Properties { file, .. } => file,
124 TargetFormat::Json { file, .. } => file,
125 TargetFormat::Yaml { file, .. } => file,
126 }
127 }
128 pub fn delimiter(&self) -> Option<&Delimiter> {
129 match self {
130 TargetFormat::Properties {
131 property_delimiter, ..
132 } => Some(property_delimiter),
133 TargetFormat::Yaml {
134 property_delimiter, ..
135 } => Some(property_delimiter),
136 TargetFormat::Json {
137 property_delimiter, ..
138 } => Some(property_delimiter),
139 }
140 }
141}