click-contrib / click-completion

Add or enhance bash, fish, zsh and powershell completion in Click
MIT License
288 stars 32 forks source link

file/path completion #26

Open flokno opened 5 years ago

flokno commented 5 years ago

This is a follow up on https://github.com/click-contrib/click-completion/issues/6

I think the -f flag in the very end of https://github.com/click-contrib/click-completion/blob/master/click_completion/fish.j2#L1 inhibits file completion in my app.

I think this is related to a change in the behavior of the -f flag in fish itself after Nov. 2017, see this PR: https://github.com/fish-shell/fish-shell/pull/4560

The ninja command here is older. When I remove the -f locally, I see file completion.

flokno commented 5 years ago

I feel the -f is there to supress file suggestions when one is still fishing for options (with -f removed one sees file suggestions always). Thus I think it should be triggered from the app and not being hard coded to the command.

flokno commented 5 years ago

I use this fork for now: https://github.com/flokno/click-completion/tree/fish_complete_files

Any comments/suggestions on how to approach this?

exploide commented 4 years ago

While using pipenv, which makes use of click-completion, I encountered this problem, too. E.g. pipenv install -r should complete files, but doesn't, which is annoying. A related issue was reported there https://github.com/pypa/pipenv/issues/3478

I took a look at the code of click-completion. The -f option mentioned in the first comment is obviously the cause for this. But just removing the -f would imply that unnecessary file arguments get completed when, for example, just subcommands make sense.

So either the stub fish completion template needs to become more complex (don't know yet how to do that in an universal way) or click-completion needs to emit file completions by itself (and stay with the global -f).

It looks like this is done for the zsh completion automatically when there are no other explicit arguments to complete. However, the way this is done seems not to work with fish. So maybe this needs indeed to be done with an additional function. The completions for pip (without click-completion) do something like that: https://github.com/pypa/pip/blob/69a811cec64d16f4a3c84f685c50ac319f938772/src/pip/_internal/cli/autocompletion.py#L136

Instead of doing this when there are no other explicit arguments, it could be reasonable to check if the to be completed option is a click file argument https://click.palletsprojects.com/en/master/arguments/#file-arguments (though, I have no experience with click).

Would be great if this can eventually be resolved somehow.