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