Skip to main content

Merge pull request #21 from alexwlchan/update-clap

ID
4ecb6ec
date
2022-10-24 19:01:49+00:00
author
Alex Chan <alex@alexwlchan.net>
parents
c98a8ae, 2836aee
message
Merge pull request #21 from alexwlchan/update-clap

Update to the newest version of clap
changed files
5 files, 106 additions, 83 deletions

Changed files

CHANGELOG.md (1073) → CHANGELOG.md (1190)

diff --git a/CHANGELOG.md b/CHANGELOG.md
index 4785fa8..6342d92 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,5 +1,11 @@
 # Changelog
 
+## v1.1.7 - 2022-10-24
+
+Some internal refactoring to use newer versions of libraries.
+
+This has no feature changes.
+
 ## v1.1.6 - 2022-10-23
 
 Provide precompiled binaries for more targets, so the following targets are now supported:

Cargo.lock (13457) → Cargo.lock (13462)

diff --git a/Cargo.lock b/Cargo.lock
index fc6bb5b..c03fa2c 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -9,15 +9,6 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "f26201604c87b1e01bd3d98f8d5d9a8fcbb815e8cedb41ffccbeb4bf593a35fe"
 
 [[package]]
-name = "ansi_term"
-version = "0.12.1"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "d52a9bb7ec0cf484c551830a7ce27bd20d67eac647e1befb56b0be4ee39a55d2"
-dependencies = [
- "winapi",
-]
-
-[[package]]
 name = "approx"
 version = "0.3.2"
 source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -28,9 +19,9 @@ dependencies = [
 
 [[package]]
 name = "assert_cmd"
-version = "2.0.4"
+version = "2.0.5"
 source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "93ae1ddd39efd67689deb1979d80bad3bf7f2b09c6e6117c8d1f2443b5e2f83e"
+checksum = "d5c2ca00549910ec251e3bd15f87aeeb206c9456b9a77b43ff6c97c54042a472"
 dependencies = [
  "bstr",
  "doc-comment",
@@ -94,17 +85,24 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
 
 [[package]]
 name = "clap"
-version = "2.34.0"
+version = "4.0.18"
 source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "a0610544180c38b88101fecf2dd634b174a62eef6946f84dfc6a7127512b381c"
+checksum = "335867764ed2de42325fafe6d18b8af74ba97ee0c590fa016f157535b42ab04b"
 dependencies = [
- "ansi_term",
  "atty",
  "bitflags",
+ "clap_lex",
  "strsim",
- "textwrap",
- "unicode-width",
- "vec_map",
+ "termcolor",
+]
+
+[[package]]
+name = "clap_lex"
+version = "0.3.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "0d4198f73e42b4936b35b5bb248d81d2b595ecb170da0bac7655c54eedfa8da8"
+dependencies = [
+ "os_str_bytes",
 ]
 
 [[package]]
@@ -136,7 +134,7 @@ checksum = "fea41bba32d969b513997752735605054bc0dfa92b4c56bf1189f2e174be7a10"
 
 [[package]]
 name = "dominant_colours"
-version = "1.1.6"
+version = "1.1.7"
 dependencies = [
  "assert_cmd",
  "clap",
@@ -292,6 +290,12 @@ dependencies = [
 ]
 
 [[package]]
+name = "os_str_bytes"
+version = "6.3.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "9ff7415e9ae3fff1225851df9e0d9e4e5479f947619774677a63572e55e80eff"
+
+[[package]]
 name = "palette"
 version = "0.5.0"
 source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -414,9 +418,9 @@ checksum = "6c230d73fb8d8c1b9c0b3135c5142a8acee3a0558fb8db5cf1cb65f8d7862132"
 
 [[package]]
 name = "strsim"
-version = "0.8.0"
+version = "0.10.0"
 source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "8ea5119cdb4c55b55d432abb513a0429384878c15dde60cc77b1c99de1a95a6a"
+checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623"
 
 [[package]]
 name = "syn"
@@ -430,19 +434,19 @@ dependencies = [
 ]
 
 [[package]]
-name = "termtree"
-version = "0.2.4"
+name = "termcolor"
+version = "1.1.3"
 source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "507e9898683b6c43a9aa55b64259b721b52ba226e0f3779137e50ad114a4c90b"
+checksum = "bab24d30b911b2376f3a13cc2cd443142f0c81dda04c118693e35b3835757755"
+dependencies = [
+ "winapi-util",
+]
 
 [[package]]
-name = "textwrap"
-version = "0.11.0"
+name = "termtree"
+version = "0.2.4"
 source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "d326610f408c7a4eb6f51c37c330e496b08506c9457c9d34287ecc38809fb060"
-dependencies = [
- "unicode-width",
-]
+checksum = "507e9898683b6c43a9aa55b64259b721b52ba226e0f3779137e50ad114a4c90b"
 
 [[package]]
 name = "tiff"
@@ -462,18 +466,6 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "6ceab39d59e4c9499d4e5a8ee0e2735b891bb7308ac83dfb4e80cad195c9f6f3"
 
 [[package]]
-name = "unicode-width"
-version = "0.1.10"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "c0edd1e5b14653f783770bce4a4dabb4a5108a5370a5f5d8cfe8710c361f6c8b"
-
-[[package]]
-name = "vec_map"
-version = "0.8.2"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "f1bddf1187be692e79c5ffeab891132dfb0f236ed36a43c7ed39f1165ee20191"
-
-[[package]]
 name = "wait-timeout"
 version = "0.2.0"
 source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -511,6 +503,15 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
 
 [[package]]
+name = "winapi-util"
+version = "0.1.5"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "70ec6ce85bb158151cae5e5c87f95a8e97d2c0c4b001223f33a334e3ce5de178"
+dependencies = [
+ "winapi",
+]
+
+[[package]]
 name = "winapi-x86_64-pc-windows-gnu"
 version = "0.4.0"
 source = "registry+https://github.com/rust-lang/crates.io-index"

Cargo.toml (414) → Cargo.toml (416)

diff --git a/Cargo.toml b/Cargo.toml
index 1e728aa..703b834 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -1,11 +1,11 @@
 [package]
 name = "dominant_colours"
-version = "1.1.6"
+version = "1.1.7"
 edition = "2018"
 
 [dependencies]
-assert_cmd = "2.0.2"
-clap = "2.33"
+assert_cmd = "2.0.5"
+clap = "4.0.18"
 
 [dependencies.kmeans_colors]
 version = "0.4.0"

src/cli.rs (0) → src/cli.rs (1115)

diff --git a/src/cli.rs b/src/cli.rs
new file mode 100644
index 0000000..b51a0ad
--- /dev/null
+++ b/src/cli.rs
@@ -0,0 +1,40 @@
+use clap::{Arg, ArgAction, Command};
+
+const VERSION: &str = env!("CARGO_PKG_VERSION");
+
+pub fn app() -> clap::Command {
+    Command::new("dominant_colours")
+        .version(VERSION)
+        .author("Alex Chan <alex@alexwlchan.net>")
+        .about("Find the dominant colours in an image")
+        .arg(
+            Arg::new("PATH")
+                .help("path to the image to inspect")
+                .required(true)
+                .index(1),
+        )
+        .arg(
+            Arg::new("MAX-COLOURS")
+                .long("max-colours")
+                .help("how many colours to find")
+                .value_parser(value_parser!(usize))
+                .default_value("5"),
+        )
+        .arg(
+            Arg::new("no-palette")
+                .long("no-palette")
+                .help("Just print the hex values, not colour previews")
+                .action(ArgAction::SetTrue),
+        )
+}
+
+#[cfg(test)]
+mod tests {
+    use crate::cli::app;
+
+    // See https://github.com/clap-rs/clap/blob/master/CHANGELOG.md#300---2021-12-31
+    #[test]
+    fn verify_app() {
+        app().debug_assert();
+    }
+}

src/main.rs (9257) → src/main.rs (8386)

diff --git a/src/main.rs b/src/main.rs
index 8e37f47..a0dc256 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -3,53 +3,29 @@
 #[macro_use]
 extern crate clap;
 
-use clap::{App, Arg};
 use kmeans_colors::get_kmeans_hamerly;
 use palette::{Lab, Pixel, Srgb, Srgba};
 
+mod cli;
 mod get_bytes;
 
-const VERSION: &str = env!("CARGO_PKG_VERSION");
-
 fn main() {
-    let matches = App::new("dominant_colours")
-        .version(VERSION)
-        .author("Alex Chan <alex@alexwlchan.net>")
-        .about("Find the dominant colours in an image")
-        .arg(
-            Arg::with_name("PATH")
-                .help("path to the image to inspect")
-                .required(true)
-                .index(1),
-        )
-        .arg(
-            Arg::with_name("MAX-COLOURS")
-                .long("max-colours")
-                .help("how many colours to find")
-                .default_value("5")
-                .takes_value(true),
-        )
-        .arg(
-            Arg::with_name("no-palette")
-                .long("no-palette")
-                .help("Just print the hex values, not colour previews")
-                .takes_value(false),
-        )
-        .get_matches();
-
-    // This .unwrap() is safe because "path" is a required param
-    let path = matches.value_of("PATH").unwrap();
-
-    // Get the max colours as a number.
-    // See https://github.com/clap-rs/clap/blob/v2.33.1/examples/12_typed_values.rs
-    let max_colours = value_t!(matches, "MAX-COLOURS", usize).unwrap_or_else(|e| e.exit());
+    let matches = cli::app().get_matches();
+
+    let path = matches
+        .get_one::<String>("PATH")
+        .expect("`path` is required");
+
+    let max_colours: usize = *matches
+        .get_one::<usize>("MAX-COLOURS")
+        .expect("`max-colours` is required");
 
     // There's different code for fetching bytes from GIF images because
     // GIFs are often animated, and we want a selection of frames.
     let img_bytes = if path.to_lowercase().ends_with(".gif") {
-        get_bytes::get_bytes_for_gif(path)
+        get_bytes::get_bytes_for_gif(&path)
     } else {
-        get_bytes::get_bytes_for_image(path)
+        get_bytes::get_bytes_for_image(&path)
     };
 
     // This is based on code from the kmeans-colors binary, but with a bunch of
@@ -79,7 +55,7 @@ fn main() {
     for c in rgb {
         let display_value = format!("#{:02x}{:02x}{:02x}", c.red, c.green, c.blue);
 
-        if matches.is_present("no-palette") {
+        if matches.get_flag("no-palette") {
             println!("{}", display_value);
         } else {
             println!(
@@ -213,11 +189,11 @@ mod tests {
     fn it_fails_if_you_pass_an_invalid_max_colours() {
         let output = get_failure(&["./src/tests/red.png", "--max-colours=NaN"]);
 
-        assert_eq!(output.exit_code, 1);
+        assert_eq!(output.exit_code, 2);
         assert_eq!(output.stdout, "");
         assert_eq!(
             output.stderr,
-            "error: Invalid value: The argument 'NaN' isn't a valid value\n"
+            "error: Invalid value 'NaN' for '--max-colours <MAX-COLOURS>': invalid digit found in string\n\nFor more information try '--help'\n"
         );
     }