diff --git a/CHANGELOG.md b/CHANGELOG.md index 80b6f0b08..8b843166c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,6 +17,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Changed * #643 Print more helpful error message on `ParsePicaError` +* #653 Don't require filter argument when an expression file is given ### Removed diff --git a/pica-toolkit/src/commands/filter.rs b/pica-toolkit/src/commands/filter.rs index 1ac3521c1..ca50d49d1 100644 --- a/pica-toolkit/src/commands/filter.rs +++ b/pica-toolkit/src/commands/filter.rs @@ -52,10 +52,10 @@ pub(crate) struct Filter { #[arg(long, short)] discard: Option, - /// Take filter expressions from + /// Take a filter expression from /// - /// Note: Using a expression file still requires a filter; e.g - /// `003@.0?`. + /// Note: Do not provide an additional filter expression as an CLI + /// argument! #[arg(long = "file", short = 'F')] expr_file: Option, @@ -127,6 +127,7 @@ pub(crate) struct Filter { output: Option, /// A filter expression used for searching + #[arg(default_value = "", hide_default_value = true)] filter: String, /// Read one or more files in normalized PICA+ format @@ -166,7 +167,20 @@ impl Filter { let keep_predicates = parse_predicates(&self.keep.unwrap_or_default())?; + let mut filenames = self.filenames; let filter_str = if let Some(filename) = self.expr_file { + // This "hack" is necessary, because it's not possible to + // distinguish between filter and filenames. If a expression + // file is given, it makes no sense to provide + // an filter expression as CLI argument. + if !self.filter.is_empty() { + if filenames != ["-"] { + filenames.insert(0, self.filter.into()); + } else { + filenames = vec![self.filter.into()]; + } + } + read_to_string(filename).unwrap() } else { self.filter @@ -222,7 +236,7 @@ impl Filter { .strsim_threshold(self.strsim_threshold as f64 / 100.0) .case_ignore(self.ignore_case); - 'outer: for filename in self.filenames { + 'outer: for filename in filenames { let mut reader = ReaderBuilder::new().from_path(filename)?; diff --git a/pica-toolkit/tests/snapshot/filter/0204-filter-file-f.toml b/pica-toolkit/tests/snapshot/filter/0204-filter-file-f.toml index aab8d258b..0b6a433c2 100644 --- a/pica-toolkit/tests/snapshot/filter/0204-filter-file-f.toml +++ b/pica-toolkit/tests/snapshot/filter/0204-filter-file-f.toml @@ -1,5 +1,5 @@ bin.name = "pica" -args = "filter -F filter.txt \"003@?\"" +args = "filter -F filter.txt" status = "success" stdout = "" stderr = "" diff --git a/pica-toolkit/tests/snapshot/filter/0204-filter-file-t.toml b/pica-toolkit/tests/snapshot/filter/0204-filter-file-t.toml index 048ce841d..a102146f4 100644 --- a/pica-toolkit/tests/snapshot/filter/0204-filter-file-t.toml +++ b/pica-toolkit/tests/snapshot/filter/0204-filter-file-t.toml @@ -1,4 +1,4 @@ bin.name = "pica" -args = "filter -F filter.txt \"003@?\"" +args = "filter -F filter.txt" status = "success" stderr = ""