Skip to main content

Use the newest version of clap

ID
90b9ce5
date
2022-10-24 18:45:45+00:00
author
Alex Chan <alex@alexwlchan.net>
parent
89c40b0
message
Use the newest version of clap
changed files
5 files, 31 additions, 52 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 (14112) → Cargo.lock (13462)

diff --git a/Cargo.lock b/Cargo.lock
index 37186e8..c03fa2c 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -19,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",
@@ -85,24 +85,22 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
 
 [[package]]
 name = "clap"
-version = "3.2.23"
+version = "4.0.18"
 source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "71655c45cb9845d3270c9d6df84ebe72b4dad3c2ba3f7023ad47c144e4e473a5"
+checksum = "335867764ed2de42325fafe6d18b8af74ba97ee0c590fa016f157535b42ab04b"
 dependencies = [
  "atty",
  "bitflags",
  "clap_lex",
- "indexmap",
  "strsim",
  "termcolor",
- "textwrap",
 ]
 
 [[package]]
 name = "clap_lex"
-version = "0.2.4"
+version = "0.3.0"
 source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "2850f2f5a82cbf437dd5af4d49848fbdfc27c157c3d010345776f952765261c5"
+checksum = "0d4198f73e42b4936b35b5bb248d81d2b595ecb170da0bac7655c54eedfa8da8"
 dependencies = [
  "os_str_bytes",
 ]
@@ -136,7 +134,7 @@ checksum = "fea41bba32d969b513997752735605054bc0dfa92b4c56bf1189f2e174be7a10"
 
 [[package]]
 name = "dominant_colours"
-version = "1.1.6"
+version = "1.1.7"
 dependencies = [
  "assert_cmd",
  "clap",
@@ -183,12 +181,6 @@ dependencies = [
 ]
 
 [[package]]
-name = "hashbrown"
-version = "0.12.3"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "8a9ee70c43aaf417c914396645a0fa852624801b24ebb7ae78fe8272889ac888"
-
-[[package]]
 name = "hermit-abi"
 version = "0.1.19"
 source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -215,16 +207,6 @@ dependencies = [
 ]
 
 [[package]]
-name = "indexmap"
-version = "1.9.1"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "10a35a97730320ffe8e2d410b5d3b69279b98d2c14bdb8b70ea89ecf7888d41e"
-dependencies = [
- "autocfg",
- "hashbrown",
-]
-
-[[package]]
 name = "itertools"
 version = "0.10.5"
 source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -467,12 +449,6 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "507e9898683b6c43a9aa55b64259b721b52ba226e0f3779137e50ad114a4c90b"
 
 [[package]]
-name = "textwrap"
-version = "0.16.0"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "222a222a5bfe1bba4a77b45ec488a741b3cb8872e5e499451fd7d0129c9c7c3d"
-
-[[package]]
 name = "tiff"
 version = "0.7.3"
 source = "registry+https://github.com/rust-lang/crates.io-index"

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

diff --git a/Cargo.toml b/Cargo.toml
index 2263283..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 = "3.2.18"
+assert_cmd = "2.0.5"
+clap = "4.0.18"
 
 [dependencies.kmeans_colors]
 version = "0.4.0"

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

diff --git a/src/cli.rs b/src/cli.rs
index 2b80668..b51a0ad 100644
--- a/src/cli.rs
+++ b/src/cli.rs
@@ -1,30 +1,30 @@
-use clap::{App, Arg};
+use clap::{Arg, ArgAction, Command};
 
 const VERSION: &str = env!("CARGO_PKG_VERSION");
 
-pub fn app() -> clap::App<'static> {
-    App::new("dominant_colours")
+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::with_name("PATH")
+            Arg::new("PATH")
                 .help("path to the image to inspect")
                 .required(true)
                 .index(1),
         )
         .arg(
-            Arg::with_name("MAX-COLOURS")
+            Arg::new("MAX-COLOURS")
                 .long("max-colours")
                 .help("how many colours to find")
-                .default_value("5")
-                .takes_value(true),
+                .value_parser(value_parser!(usize))
+                .default_value("5"),
         )
         .arg(
-            Arg::with_name("no-palette")
+            Arg::new("no-palette")
                 .long("no-palette")
                 .help("Just print the hex values, not colour previews")
-                .takes_value(false),
+                .action(ArgAction::SetTrue),
         )
 }
 

src/main.rs (8502) → src/main.rs (8350)

diff --git a/src/main.rs b/src/main.rs
index 5e33ab4..a91ae46 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -12,19 +12,16 @@ mod get_bytes;
 fn main() {
     let matches = cli::app().get_matches();
 
-    // This .unwrap() is safe because "path" is a required param
-    let path = matches.value_of("PATH").unwrap();
+    let path = matches.get_one::<String>("PATH").expect("`path` is required");
 
-    // 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 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
@@ -54,7 +51,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!(
@@ -192,7 +189,7 @@ mod tests {
         assert_eq!(output.stdout, "");
         assert_eq!(
             output.stderr,
-            "error: Invalid value \"NaN\" for 'MAX-COLOURS': The argument 'NaN' isn't a valid value for 'MAX-COLOURS': invalid digit found in string\n"
+            "error: Invalid value 'NaN' for '--max-colours <MAX-COLOURS>': invalid digit found in string\n\nFor more information try '--help'\n"
         );
     }