Skip to content

jq: completion does not complete files when it should #510

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

Closed
rrthomas opened this issue Mar 27, 2021 · 3 comments
Closed

jq: completion does not complete files when it should #510

rrthomas opened this issue Mar 27, 2021 · 3 comments

Comments

@rrthomas
Copy link
Contributor

The current completion code tries to force the user to use a "mode" argument first, such as --slurp, by counting arguments, and only completing files when there are two or more arguments (including the command itself). However, the code as it stands does not count any mode arguments, so the number of arguments never exceeds 1 unless you manually type a file argument, so file arguments are never auto-completed.

Example session:

$ jq --sl<TAB>
$ jq --slurp # correct
$ jq --slurp <TAB> # nothing happens, should complete files

I had a look at the manual of jq to determine what these mode arguments are, because in theory adding a list of them as the third argument to _count_args would make the code work.

However, the concept of "mode" does not seem to exist in the manual. Indeed, no such argument is required. (You can run jq quite happily without any command-line option.)

So, I propose simply deleting the args-counting code, specifically this bit:

    local args
    # TODO: DTRT with args taking 2 options
    # -f|--from-file are not counted here because they supply the filter
    _count_args "" "@(--arg|--arg?(json|file)|--slurpfile|--indent|--run-tests|-!(-*)L)"

    # 1st arg is filter
    echo jq args: $args
    ((args == 1)) && return
    # 2... are input files

I am happy to make a PR if this is acceptable.

@scop
Copy link
Owner

scop commented Mar 27, 2021

$ jq --slurp <TAB> # nothing happens, should complete files

I don't think it should. At that point we're completing a filter, and nothing happening is intentional for that. From my man page:

--slurp/-s:

Instead  of running the filter for each JSON object in the input, read the entire input stream into a large array and
run the filter just once.

--slurp doesn't take a filename argument, and neither does a "bare" jq invocation before a filter has been given.

$ jq --slurp test.json
jq: error: test/0 is not defined at <top-level>, line 1:
test.json
jq: 1 compile error
$ jq test.json
jq: error: test/0 is not defined at <top-level>, line 1:
test.json
jq: 1 compile error

My jq is 1.5 on Ubuntu 18.04.

@rrthomas
Copy link
Contributor Author

Thanks for the analysis, @scop. I had a look at my command line history to try to find the command that made me think I had found a bug, but I can't. Closing.

@scop
Copy link
Owner

scop commented Mar 28, 2021

No problem, thanks for double checking.

I had a peek at jq 1.6 too, and it seems it does have some new usage patterns we should adjust to though, #511

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants