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

using both inner and outer quotes confuses expr: query. Bug ? #2279

Open
simonmichael opened this issue Oct 31, 2024 · 1 comment
Open

using both inner and outer quotes confuses expr: query. Bug ? #2279

simonmichael opened this issue Oct 31, 2024 · 1 comment
Labels
A-WISH Some kind of improvement request, hare-brained proposal, or plea. cli Command line parsing, options, arguments and suchlike. queries Filtering options, query arguments..

Comments

@simonmichael
Copy link
Owner

simonmichael commented Oct 31, 2024

In chat, Pixelized reports:

hledger bs expr:"assets:cash or not assets"

leads to exactly the results i'd expect, while

hledger bs 'expr:"assets:cash or not assets"'

leads to a completely empty balance sheet. is this intended behavior?

It happens because parseQueryTerm expects to receive expr:'s argument as an unquoted string; it does not do another round of enclosing-quotes-stripping. So it interprets "assets:cash or not assets" as a single query term, equivalent to acct:"assets:cash or not assets".

This is arguably correct behaviour, on the other hand the expectation is understandable. In general, enclosing the full command line argument in quotes is the most robust, like

hledger bs "expr:assets:cash or not assets"

but we are used to this also working (eg, query docs use this style):

hledger bs expr:"assets:cash or not assets"

so it seems reasonable to think that this would also work:

hledger bs 'expr:"assets:cash or not assets"'

It sounds easy enough to have parseQueryTerm check for enclosing quotes and remove them. But will this complicate something else ? Testing needed.

@simonmichael simonmichael added cli Command line parsing, options, arguments and suchlike. queries Filtering options, query arguments.. labels Oct 31, 2024
@simonmichael simonmichael changed the title inner and outer enclosing quotes confuses expr: query combined inner and outer quotes confuses expr: query Oct 31, 2024
@simonmichael simonmichael added A-BUG Something wrong, confusing or sub-standard in the software, docs, or user experience. A-WISH Some kind of improvement request, hare-brained proposal, or plea. labels Oct 31, 2024
@simonmichael simonmichael changed the title combined inner and outer quotes confuses expr: query using both inner and outer quotes confuses expr: query Oct 31, 2024
@simonmichael
Copy link
Owner Author

simonmichael commented Nov 2, 2024

Here's the three cases above with hledger-1.40, the third one failing:

~/src/hledger$ hledger bs "expr :assets:cash or not assets"
Balance Sheet 2008-02-02 

             || 2008-02-02 
=============++============
 Assets      ||            
-------------++------------
 assets:cash ||        $-1 
-------------++------------
             ||        $-1 
=============++============
 Liabilities ||            
-------------++------------
-------------++------------
             ||          0 
=============++============
 Net:        ||        $-1 
~/src/hledger$ hledger-1.40 bs expr:"assets:cash or not assets"
Balance Sheet 2008-02-02

             || 2008-02-02 
=============++============
 Assets      ||            
-------------++------------
 assets:cash ||        $-1 
-------------++------------
             ||        $-1 
=============++============
 Liabilities ||            
-------------++------------
-------------++------------
             ||          0 
=============++============
 Net:        ||        $-1 
~/src/hledger$ hledger-1.40 bs 'expr:"assets:cash or not assets"'
Balance Sheet 2008-02-02

             || 2008-02-02 
=============++============
 Assets      ||            
-------------++------------
-------------++------------
             ||          0 
=============++============
 Liabilities ||            
-------------++------------
-------------++------------
             ||          0 
=============++============
 Net:        ||          0 

Here's a custom build that strips (one pair of) enclosing quotes from expr:'s argument. This makes the third case work as OP expected:

~/src/hledger$ hledger bs 'expr:"assets:cash or not assets"'
Balance Sheet 2008-02-02

             || 2008-02-02 
=============++============
 Assets      ||            
-------------++------------
 assets:cash ||        $-1 
-------------++------------
             ||        $-1 
=============++============
 Liabilities ||            
-------------++------------
-------------++------------
             ||          0 
=============++============
 Net:        ||        $-1 

Questions:
What about all the other query prefixes, like acct:, desc:, payee: etc. ? Should they have a similar quote-stripping behaviour ?
What about multiple levels of quoting ?
What about unbalanced/mismatched quotes ?
What about the backslash style of quoting ?

Here's the test journal I'm using. I'm imagining some unusual quotes in the data,
let's say from some Beancount data that was improperly imported:

2008-01-01 "beancount txn"
    Assets:Bank:Checking           1 USD
    Income:Salary

2008-02-02 hledger txn
    expenses:food                 $1
    assets:cash

Question: How would a user match those Beancount entries ?

This works in old and new versions:

~/src/hledger$ hledger-1.40 print desc:'".*"'
2008-01-01 "beancount txn"
    Assets:Bank:Checking           1 USD
    Income:Salary

~/src/hledger$ hledger print desc:'".*"'
2008-01-01 "beancount txn"
    Assets:Bank:Checking           1 USD
    Income:Salary

What if you're doing that within an expr: query ?

Finding a spelling that does this wasn't easy, but here's one. It still works with both old and new versions (somehow):

~/src/hledger$ hledger-1.40 print "expr:'desc:\".*\"'"
2008-01-01 "beancount txn"
    Assets:Bank:Checking           1 USD
    Income:Salary

~/src/hledger$ hledger print "expr:'desc:\".*\"'"
2008-01-01 "beancount txn"
    Assets:Bank:Checking           1 USD
    Income:Salary

There are enough questions that I'm inclined not to touch this myself. I expect I'd merge a PR, if it came with tests clarifying some of these cases.

@simonmichael simonmichael removed the A-BUG Something wrong, confusing or sub-standard in the software, docs, or user experience. label Nov 2, 2024
@simonmichael simonmichael changed the title using both inner and outer quotes confuses expr: query using both inner and outer quotes confuses expr: query. Bug ? Nov 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-WISH Some kind of improvement request, hare-brained proposal, or plea. cli Command line parsing, options, arguments and suchlike. queries Filtering options, query arguments..
Projects
None yet
Development

No branches or pull requests

1 participant