zacchiro / beangrep

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

Add `-v/--invert-match` to return non matching entries #12

Closed tbm closed 6 months ago

tbm commented 6 months ago

It would be nice if there was a way to remove transactions that match a specific pattern, e.g. show me all transactions from this payee but not if it has this tag.

I'm not sure what the syntax would be, but maybe the pattern could start with a -.

zacchiro commented 6 months ago

grep has -v/--invert-match for this. We can add this to bean-grep as well.

tbm commented 6 months ago

I'm not sure if that would work. Because I want to say: search for this, but exclude this. So some should match, some should be invert match.

tbm commented 6 months ago

... although I guess one option would be to use a pipe and have two calls.

zacchiro commented 6 months ago

I'm not sure if that would work. Because I want to say: search for this, but exclude this. So some should match, some should be invert match.

I really don't want to go there, because it opens the door to fully boolean AND/OR/NOT queries, which is too complex for a decent UI.

So if you're fine with implementing -v, we can make this issue about that. If not it will be wontfix (for now at least).

tbm commented 6 months ago

-v with a pipe should work, right?

bean-grep foo | bean-grep -v "#foo"
zacchiro commented 6 months ago

On Thu, May 16, 2024 at 01:00:04AM -0700, Martin Michlmayr wrote:

-v with a pipe should work, right?

bean-grep foo | bean-grep -v "#foo"

You need an explicit "-" as filename, but yes, it should work.

zacchiro commented 6 months ago

Caveat to watch out for in implementing this: the default criteria that selects transactions. It should probably not be the default when -v is given. (Needs some more thoughts in terms of UX…)

zacchiro commented 6 months ago

Note also that -v is currently use for --verbose, so we will need to decide who gets to keep the short flag. (Probably --invert-match, as that is grep behavior and is a quite popular CLI mnemonic for many of us.)

zacchiro commented 6 months ago

@tbm so -v/--invert-match is implemented in the branch feature/invert-match, if you want to give it a try.

But it currently doesn't work as (you) expected with the following:

$ bean-grep @Bar tests/data/example.beancount | bean-grep '#chicago' -

because the filtered out output of the first invocation cannot be loaded without errors in the second invocation (e.g., there are no open directives for accounts there).

This goes in the direction of what you suggested in #5, i.e., be more lenient when loading (but I would still like to file if, say, requested file does not exist).

If the explicit if ledger[1]: block in cli.py is commented out, the above double/piped invocation works like a charm.