Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't require filter argument when an expression file is given #653

Merged
merged 3 commits into from
Jul 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
/// `003@.0?`.
/// 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 = ""
Loading