-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathopts.rs
More file actions
109 lines (88 loc) · 3.02 KB
/
opts.rs
File metadata and controls
109 lines (88 loc) · 3.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
use std::fmt;
use std::{path::Path, str::FromStr};
use clap::Parser;
#[derive(Debug, Parser)]
#[command(name = "rcli", version, author, about, long_about = None)]
pub struct Opts {
#[command(subcommand)]
pub cmd: SubCommand,
}
#[derive(Debug, Parser)]
pub enum SubCommand {
#[command(name = "csv", about = "Show CSV, or convert CSV to other formats")]
Csv(CsvOpts),
#[command(name = "genpass", about = "Generate a random password")]
GenPass(GenPassOpts),
}
#[derive(Debug, Clone, Copy)]
pub enum OutputFormat {
Json,
Yaml,
Toml,
}
impl From<&OutputFormat> for &'static str {
fn from(format: &OutputFormat) -> Self {
match format {
OutputFormat::Json => "json",
OutputFormat::Yaml => "yaml",
OutputFormat::Toml => "toml",
}
}
}
impl FromStr for OutputFormat {
type Err = anyhow::Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s.to_lowercase().as_str() {
"json" => Ok(OutputFormat::Json),
"yaml" => Ok(OutputFormat::Yaml),
"toml" => Ok(OutputFormat::Toml),
_ => Err(anyhow::anyhow!("Unsupported output format: {}", s)),
}
}
}
impl fmt::Display for OutputFormat {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let s: &str = self.into();
write!(f, "{}", s)
}
}
#[derive(Debug, Parser)]
pub struct CsvOpts {
#[arg(short, long, value_parser = verify_input_file, help = "Input CSV file")]
pub input: String,
#[arg(short, long, help = "Output file")]
pub output: Option<String>,
#[arg(short, long, value_parser = parse_format, help = "Output format", default_value = "json")]
pub format: OutputFormat,
#[arg(
long,
help = "Indicates that the CSV file has a header row",
default_value_t = true
)]
pub header: bool,
#[arg(short, long, help = "Delimiter character", default_value_t = ',')]
pub delimiter: char,
}
#[derive(Debug, Parser)]
pub struct GenPassOpts {
#[arg(short, long, help = "Length of the password", default_value_t = 12)]
pub length: u8,
#[arg(long = "no-uppercase", help = "Exclude uppercase characters", default_value_t = true, action = clap::ArgAction::SetFalse)]
pub uppercase: bool,
#[arg(long = "no-lowercase", help = "Exclude lowercase characters", default_value_t = true, action = clap::ArgAction::SetFalse)]
pub lowercase: bool,
#[arg(long = "no-numbers", help = "Exclude numbers", default_value_t = true, action = clap::ArgAction::SetFalse)]
pub numbers: bool,
#[arg(long = "no-special", help = "Exclude special characters", default_value_t = true, action = clap::ArgAction::SetFalse)]
pub special: bool,
}
fn verify_input_file(filename: &str) -> Result<String, String> {
if Path::new(filename).exists() {
Ok(filename.to_string())
} else {
Err(format!("Input file '{}' does not exist.", filename))
}
}
fn parse_format(format: &str) -> Result<OutputFormat, anyhow::Error> {
format.parse()
}