Skip to content

Commit 4ecb6ec

Browse files
authored
Merge pull request #21 from alexwlchan/update-clap
Update to the newest version of clap
2 parents c98a8ae + 2836aee commit 4ecb6ec

File tree

5 files changed

+106
-83
lines changed

5 files changed

+106
-83
lines changed

CHANGELOG.md

+6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Changelog
22

3+
## v1.1.7 - 2022-10-24
4+
5+
Some internal refactoring to use newer versions of libraries.
6+
7+
This has no feature changes.
8+
39
## v1.1.6 - 2022-10-23
410

511
Provide precompiled binaries for more targets, so the following targets are now supported:

Cargo.lock

+42-41
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
[package]
22
name = "dominant_colours"
3-
version = "1.1.6"
3+
version = "1.1.7"
44
edition = "2018"
55

66
[dependencies]
7-
assert_cmd = "2.0.2"
8-
clap = "2.33"
7+
assert_cmd = "2.0.5"
8+
clap = "4.0.18"
99

1010
[dependencies.kmeans_colors]
1111
version = "0.4.0"

src/cli.rs

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
use clap::{Arg, ArgAction, Command};
2+
3+
const VERSION: &str = env!("CARGO_PKG_VERSION");
4+
5+
pub fn app() -> clap::Command {
6+
Command::new("dominant_colours")
7+
.version(VERSION)
8+
.author("Alex Chan <[email protected]>")
9+
.about("Find the dominant colours in an image")
10+
.arg(
11+
Arg::new("PATH")
12+
.help("path to the image to inspect")
13+
.required(true)
14+
.index(1),
15+
)
16+
.arg(
17+
Arg::new("MAX-COLOURS")
18+
.long("max-colours")
19+
.help("how many colours to find")
20+
.value_parser(value_parser!(usize))
21+
.default_value("5"),
22+
)
23+
.arg(
24+
Arg::new("no-palette")
25+
.long("no-palette")
26+
.help("Just print the hex values, not colour previews")
27+
.action(ArgAction::SetTrue),
28+
)
29+
}
30+
31+
#[cfg(test)]
32+
mod tests {
33+
use crate::cli::app;
34+
35+
// See https://github.com/clap-rs/clap/blob/master/CHANGELOG.md#300---2021-12-31
36+
#[test]
37+
fn verify_app() {
38+
app().debug_assert();
39+
}
40+
}

src/main.rs

+15-39
Original file line numberDiff line numberDiff line change
@@ -3,53 +3,29 @@
33
#[macro_use]
44
extern crate clap;
55

6-
use clap::{App, Arg};
76
use kmeans_colors::get_kmeans_hamerly;
87
use palette::{Lab, Pixel, Srgb, Srgba};
98

9+
mod cli;
1010
mod get_bytes;
1111

12-
const VERSION: &str = env!("CARGO_PKG_VERSION");
13-
1412
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");
4622

4723
// There's different code for fetching bytes from GIF images because
4824
// GIFs are often animated, and we want a selection of frames.
4925
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)
5127
} else {
52-
get_bytes::get_bytes_for_image(path)
28+
get_bytes::get_bytes_for_image(&path)
5329
};
5430

5531
// This is based on code from the kmeans-colors binary, but with a bunch of
@@ -79,7 +55,7 @@ fn main() {
7955
for c in rgb {
8056
let display_value = format!("#{:02x}{:02x}{:02x}", c.red, c.green, c.blue);
8157

82-
if matches.is_present("no-palette") {
58+
if matches.get_flag("no-palette") {
8359
println!("{}", display_value);
8460
} else {
8561
println!(
@@ -213,11 +189,11 @@ mod tests {
213189
fn it_fails_if_you_pass_an_invalid_max_colours() {
214190
let output = get_failure(&["./src/tests/red.png", "--max-colours=NaN"]);
215191

216-
assert_eq!(output.exit_code, 1);
192+
assert_eq!(output.exit_code, 2);
217193
assert_eq!(output.stdout, "");
218194
assert_eq!(
219195
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"
221197
);
222198
}
223199

0 commit comments

Comments
 (0)