From 4f5914ed75353089fca5cb7e17c22c5f67a89542 Mon Sep 17 00:00:00 2001 From: Lynne Date: Wed, 28 Apr 2021 18:09:44 +1000 Subject: [PATCH] -E and -X can now take multiple sets --- CHANGELOG.md | 3 ++- src/parameters.rs | 20 ++++++++++---------- src/tests/mod.rs | 18 +++++++++++++++++- 3 files changed, 29 insertions(+), 12 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 40d4a58..bcd111b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,9 +8,10 @@ Dates are given in YYYY-MM-DD format. files, except ".jpg" files) - Added `-X`/`--exclude-set` flag for excluding sets of files, with the same syntax and sets as `-E` - In addition to supplying included extensions as a comma separated list (like `-e jpg,png`), it is now possible to - supply them through multiple uses of the `-e` flag (like `-e jpg -e png`). This also applies to `-x`. + supply them through multiple uses of the `-e` flag (like `-e jpg -e png`). This also applies to `-x` - `-e` and `-E` no longer conflict with each other, and can now be used together. For example, `-E images -e mp3` will scan all images *and* all MP3 files +- It is now possible to specify multiple extension sets at once: `-E images,system` will scan all images and archives #### Other - Published my fork of ['mime_guess'] as ['new_mime_guess'], allowing it to be used properly with [crates.io](https://crates.io) diff --git a/src/parameters.rs b/src/parameters.rs index 511f1d8..45ef9c7 100644 --- a/src/parameters.rs +++ b/src/parameters.rs @@ -48,24 +48,24 @@ pub struct Parameters { /// Only examine files with these extensions. /// Multiple extensions can be specified by either using the flag multiple times (`-e jpg -e png -e gif`), or by /// separating them with commas (`-e jpg,png,gif`). - #[clap(short, long, use_delimiter = true, takes_value = true)] + #[clap(short, long, use_delimiter = true, require_delimiter = true)] pub exts: Option>, /// Use these preset lists of extensions as the search filter (comma-separated list). /// `media` includes all extensions from the `audio`, `video`, and `images` sets, making `-E media` equivalent to /// `-E audio,video,images`. - #[clap(short = 'E', long, arg_enum)] - pub ext_set: Option, + #[clap(short = 'E', long, arg_enum, use_delimiter = true, require_delimiter = true)] + pub ext_set: Vec, /// Don't scan files with these extensions. /// This option takes precedence over extensions specified with `-e` or `-E`. - #[clap(short = 'x', long, use_delimiter = true)] + #[clap(short = 'x', long, use_delimiter = true, require_delimiter = true)] pub exclude: Option>, /// Exclude files using a preset list of extensions. /// This option takes precedence over extensions specified with `-e` or `-E`. - #[clap(short = 'X', long, arg_enum)] - pub exclude_set: Option, + #[clap(short = 'X', long, arg_enum, use_delimiter = true, require_delimiter = true)] + pub exclude_set: Vec, /// Don't skip hidden files and directories. /// Even if this flag is not present, fif will still recurse into a hidden root directory - for example, `fif @@ -137,8 +137,8 @@ impl Parameters { included.extend(exts.iter().map(|ext| ext.as_str())); } - if let Some(ext_set) = &self.ext_set { // -E - included.extend_from_slice(&ext_set.extensions()); + if !&self.ext_set.is_empty() { // -E + included.extend(self.ext_set.iter().flat_map(|set| set.extensions())); } match included { @@ -154,8 +154,8 @@ impl Parameters { excluded.extend(exclude.iter().map(|ext| ext.as_str())); } - if let Some(exclude_set) = &self.exclude_set { // -X - excluded.extend_from_slice(&exclude_set.extensions()); + if !&self.exclude_set.is_empty() { // -X + excluded.extend(self.exclude_set.iter().flat_map(|set| set.extensions())); } // excluded doesn't sound like a word anymore diff --git a/src/tests/mod.rs b/src/tests/mod.rs index c5809fb..3fbd9f3 100644 --- a/src/tests/mod.rs +++ b/src/tests/mod.rs @@ -12,7 +12,7 @@ use clap::Clap; use std::collections::HashMap; use std::ffi::OsStr; -use std::path::Path; +use std::path::{Path, PathBuf}; use crate::parameters::ExtensionSet; const JPEG_BYTES: &[u8] = b"\xFF\xD8\xFF"; @@ -205,6 +205,17 @@ fn argument_parsing() { ) } +#[test] +/// Ensure that `fif -e jpg dir` is interpreted as "scan for jpg files in dir" and not "scan for jpg and dir files" +fn positional_args() { + for flag in &["-x", "-e", "-X", "-E"] { + assert_eq!( + Parameters::parse_from(vec!["fif", flag, "images", "directory"]).dirs, + PathBuf::from("directory") + ) + } +} + #[test] /// Ensure the `exclude` flag (`-x`) overrides `-e` and `-E`. fn exclude_overrides() { @@ -349,6 +360,11 @@ fn media_contains_audio_video_images() { .concat() .into_iter() .for_each(|ext| assert!(media_exts.contains(&ext))); + + // assert_eq!( + // Parameters::parse_from(&["fif", "-E", "media"]).extensions(), + // Parameters::parse_from(&["fif", "-E", "audio,video,images"]).extensions() + // ) } #[test]