image_optimizer/cli/
cli_args.rs1use clap::Parser;
2use std::path::PathBuf;
3
4#[derive(Parser)]
19#[command(name = "image-optimizer")]
20#[command(about = "CLI tool for optimizing images (JPEG, PNG, WebP)")]
21#[command(long_about = None)]
22#[command(version = env!("CARGO_PKG_VERSION"))]
23#[allow(clippy::struct_excessive_bools)]
24pub struct Cli {
25 #[arg(short, long)]
27 pub input: Option<PathBuf>,
28
29 #[arg(short, long)]
31 pub output: Option<PathBuf>,
32
33 #[arg(long)]
35 pub backup: bool,
36
37 #[arg(long)]
39 pub lossless: bool,
40
41 #[arg(short, long, default_value = "85")]
43 pub quality: u8,
44
45 #[arg(short, long)]
47 pub recursive: bool,
48
49 #[arg(long)]
51 pub max_size: Option<u32>,
52
53 #[arg(long)]
55 pub update: bool,
56}
57
58#[cfg(test)]
59mod tests {
60 use super::*;
61 use clap::CommandFactory;
62
63 #[test]
64 fn test_cli_defaults() {
65 let cli = Cli::parse_from(&["image-optimizer"]);
66 assert_eq!(cli.input, None);
67 assert_eq!(cli.output, None);
68 assert!(!cli.backup);
69 assert!(!cli.lossless);
70 assert_eq!(cli.quality, 85);
71 assert!(!cli.recursive);
72 assert_eq!(cli.max_size, None);
73 assert!(!cli.update);
74 }
75
76 #[test]
77 fn test_cli_with_input() {
78 let cli = Cli::parse_from(&["image-optimizer", "-i", "/path/to/images"]);
79 assert_eq!(cli.input, Some(PathBuf::from("/path/to/images")));
80 }
81
82 #[test]
83 fn test_cli_with_all_flags() {
84 let cli = Cli::parse_from(&[
85 "image-optimizer",
86 "-i",
87 "/input",
88 "-o",
89 "/output",
90 "--backup",
91 "--lossless",
92 "-q",
93 "90",
94 "--recursive",
95 "--max-size",
96 "1024",
97 "--update",
98 ]);
99
100 assert_eq!(cli.input, Some(PathBuf::from("/input")));
101 assert_eq!(cli.output, Some(PathBuf::from("/output")));
102 assert!(cli.backup);
103 assert!(cli.lossless);
104 assert_eq!(cli.quality, 90);
105 assert!(cli.recursive);
106 assert_eq!(cli.max_size, Some(1024));
107 assert!(cli.update);
108 }
109
110 #[test]
111 fn test_cli_quality_bounds() {
112 let cli = Cli::parse_from(&["image-optimizer", "-q", "1"]);
113 assert_eq!(cli.quality, 1);
114
115 let cli = Cli::parse_from(&["image-optimizer", "-q", "100"]);
116 assert_eq!(cli.quality, 100);
117 }
118
119 #[test]
120 fn test_cli_help_generation() {
121 let mut cmd = Cli::command();
122 let help = cmd.render_help();
123 assert!(help.to_string().contains("CLI tool for optimizing images"));
124 }
125}