|
3 | 3 | #[macro_use]
|
4 | 4 | extern crate clap;
|
5 | 5 |
|
6 |
| -use clap::{App, Arg}; |
7 | 6 | use kmeans_colors::get_kmeans_hamerly;
|
8 | 7 | use palette::{Lab, Pixel, Srgb, Srgba};
|
9 | 8 |
|
| 9 | +mod cli; |
10 | 10 | mod get_bytes;
|
11 | 11 |
|
12 |
| -const VERSION: &str = env!("CARGO_PKG_VERSION"); |
13 |
| - |
14 | 12 | fn main() {
|
15 |
| - let matches = App::new("dominant_colours") |
16 |
| - .version(VERSION) |
17 |
| - .author("Alex Chan <[email protected]>") |
18 |
| - .about("Find the dominant colours in an image") |
19 |
| - .arg( |
20 |
| - Arg::with_name("PATH") |
21 |
| - .help("path to the image to inspect") |
22 |
| - .required(true) |
23 |
| - .index(1), |
24 |
| - ) |
25 |
| - .arg( |
26 |
| - Arg::with_name("MAX-COLOURS") |
27 |
| - .long("max-colours") |
28 |
| - .help("how many colours to find") |
29 |
| - .default_value("5") |
30 |
| - .takes_value(true), |
31 |
| - ) |
32 |
| - .arg( |
33 |
| - Arg::with_name("no-palette") |
34 |
| - .long("no-palette") |
35 |
| - .help("Just print the hex values, not colour previews") |
36 |
| - .takes_value(false), |
37 |
| - ) |
38 |
| - .get_matches(); |
39 |
| - |
40 |
| - // This .unwrap() is safe because "path" is a required param |
41 |
| - let path = matches.value_of("PATH").unwrap(); |
42 |
| - |
43 |
| - // Get the max colours as a number. |
44 |
| - // See https://github.com/clap-rs/clap/blob/v2.33.1/examples/12_typed_values.rs |
45 |
| - let max_colours = value_t!(matches, "MAX-COLOURS", usize).unwrap_or_else(|e| e.exit()); |
| 13 | + let matches = cli::app().get_matches(); |
| 14 | + |
| 15 | + let path = matches |
| 16 | + .get_one::<String>("PATH") |
| 17 | + .expect("`path` is required"); |
| 18 | + |
| 19 | + let max_colours: usize = *matches |
| 20 | + .get_one::<usize>("MAX-COLOURS") |
| 21 | + .expect("`max-colours` is required"); |
46 | 22 |
|
47 | 23 | // There's different code for fetching bytes from GIF images because
|
48 | 24 | // GIFs are often animated, and we want a selection of frames.
|
49 | 25 | let img_bytes = if path.to_lowercase().ends_with(".gif") {
|
50 |
| - get_bytes::get_bytes_for_gif(path) |
| 26 | + get_bytes::get_bytes_for_gif(&path) |
51 | 27 | } else {
|
52 |
| - get_bytes::get_bytes_for_image(path) |
| 28 | + get_bytes::get_bytes_for_image(&path) |
53 | 29 | };
|
54 | 30 |
|
55 | 31 | // This is based on code from the kmeans-colors binary, but with a bunch of
|
@@ -79,7 +55,7 @@ fn main() {
|
79 | 55 | for c in rgb {
|
80 | 56 | let display_value = format!("#{:02x}{:02x}{:02x}", c.red, c.green, c.blue);
|
81 | 57 |
|
82 |
| - if matches.is_present("no-palette") { |
| 58 | + if matches.get_flag("no-palette") { |
83 | 59 | println!("{}", display_value);
|
84 | 60 | } else {
|
85 | 61 | println!(
|
@@ -213,11 +189,11 @@ mod tests {
|
213 | 189 | fn it_fails_if_you_pass_an_invalid_max_colours() {
|
214 | 190 | let output = get_failure(&["./src/tests/red.png", "--max-colours=NaN"]);
|
215 | 191 |
|
216 |
| - assert_eq!(output.exit_code, 1); |
| 192 | + assert_eq!(output.exit_code, 2); |
217 | 193 | assert_eq!(output.stdout, "");
|
218 | 194 | assert_eq!(
|
219 | 195 | output.stderr,
|
220 |
| - "error: Invalid value: The argument 'NaN' isn't a valid value\n" |
| 196 | + "error: Invalid value 'NaN' for '--max-colours <MAX-COLOURS>': invalid digit found in string\n\nFor more information try '--help'\n" |
221 | 197 | );
|
222 | 198 | }
|
223 | 199 |
|
|
0 commit comments