-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #21 from alexwlchan/update-clap
Update to the newest version of clap
- Loading branch information
Showing
5 changed files
with
106 additions
and
83 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 <[email protected]>") | ||
.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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 <[email protected]>") | ||
.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" | ||
); | ||
} | ||
|
||
|