Skip to content

Commit

Permalink
Don't require filter argument when an expression file is given (#653)
Browse files Browse the repository at this point in the history
  • Loading branch information
nwagner84 committed Jul 10, 2023
1 parent 74f114d commit fb7b87a
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
22 changes: 18 additions & 4 deletions pica-toolkit/src/commands/filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,10 @@ pub(crate) struct Filter {
#[arg(long, short)]
discard: Option<String>,

/// Take filter expressions from <EXPR_FILE>
/// Take a filter expression from <EXPR_FILE>
///
/// Note: Using a expression file still requires a filter; e.g
/// `[email protected]?`.
/// Note: Do not provide an additional filter expression as an CLI
/// argument!
#[arg(long = "file", short = 'f')]
expr_file: Option<PathBuf>,

Expand Down Expand Up @@ -127,6 +127,7 @@ pub(crate) struct Filter {
output: Option<OsString>,

/// A filter expression used for searching
#[arg(default_value = "", hide_default_value = true)]
filter: String,

/// Read one or more files in normalized PICA+ format
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)?;

Expand Down
2 changes: 1 addition & 1 deletion pica-toolkit/tests/snapshot/filter/0204-filter-file-f.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
bin.name = "pica"
args = "filter -f filter.txt \"003@?\""
args = "filter -f filter.txt"
status = "success"
stdout = ""
stderr = ""
2 changes: 1 addition & 1 deletion pica-toolkit/tests/snapshot/filter/0204-filter-file-t.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
bin.name = "pica"
args = "filter -f filter.txt \"003@?\""
args = "filter -f filter.txt"
status = "success"
stderr = ""

0 comments on commit fb7b87a

Please sign in to comment.