zacchiro / beangrep

Beangrep - grep-like filter for Beancount
GNU General Public License v2.0
11 stars 2 forks source link

Allow multiple PATTERNs #11

Open tbm opened 5 months ago

tbm commented 5 months ago

Since you figure out PATTERN by certain characters (e.g. # for tag), it would be possible to specify multiple PATTERNs.

e.g. bean-grep 2024-05-01 "#foo" books/2024

zacchiro commented 5 months ago

Agreed, makes sense (and would bring bean-grep closer to grep behavior).

zacchiro commented 5 months ago

Note that #9 and #11 together can make the parsing of arguments ambiguous.

zacchiro commented 5 months ago

Now that #16 is done, implementing the semantics of this is trivial. However, the syntax problem remains: how to make it non-ambiguous and user-friendly? The main problem being that if one can provide multiple patterns and multiple files, it's not clear where the former stop and the latter begin.

(I've considered the possibility of testing for file existence to decide, but I fear that would be too smart not to be surprising, and can possibly backfire in weird cases.)

For reference, grep has a very weird way of supporting this, which I've never used myself, it says:

grep searches for PATTERNS in each FILE. PATTERNS is one or more patterns separated by newline characters

That would work for bean-grep too, and I'm not opposed to it, but it's quite clunky to use on the CLI.

tbm commented 5 months ago

One option would be to require one argument, so you'd have to quote it, e.g.

bean-grep "@foo #bar" books/2023 books/2024
zacchiro commented 5 months ago

That wouldn't work, because the last resort of pattern inference is -s/--somewhere which takes arbitrary strings that might legitimately contain spaces.

Yesterday, however, I've tried in practice the grep approach (patterns are newline-separated) and it is not that bad to type using double quotes as in your example:

bean-grep "@foo
bar baz
#qux" books/2023 books/2024

because shells (I've tried both bash and zsh) does not complain when they see a newline after a non-closed quoted string.

So the above appears to be a viable option, what do you think?

tbm commented 5 months ago

I guess, yes. I'm not really the best person to ask, though.

Goorzhel commented 4 months ago

For what it's worth, grep accepts multiple files but not multiple patterns. When I want both, I do grep -E 'foo|bar|baz' /file1 /file2.

zacchiro commented 4 months ago

You're confusing multiple OR-ed patterns (which are not needed due to regex semantics) with multiple AND-ed patterns. This issue is about the latter, which grep does support separated by newlines. See the citation from the grep manpage earlier in this issue.