kislyuk / argcomplete

Python and tab completion, better together.
https://kislyuk.github.io/argcomplete/
Apache License 2.0
1.39k stars 129 forks source link

Global completion mode always set to `_minimal` #346

Open celynw opened 3 years ago

celynw commented 3 years ago

When I use the global completion mode, completion does not work. Pressing tab with no arguments it returns a file list of the current directory Pressing tab with a - or -- seems to return base python completion.

But when I register a script directly, it works fine. I seem to have run into the issue mentioned in the docs.

If global completion is not completing your script, bash may have registered a default completion function:

$ complete | grep my-awesome-script
complete -F _minimal my-awesome-script

You can fix this by restarting your shell, or by running complete -r my-awesome-script.

Global mode (doesn't work):

$ complete | grep py
...
complete -F _minimal ./test.py

Explicit eval "$(register-python-argcomplete ./test.py)" (works):

$ complete | grep py
...
complete -o bashdefault -o default -o nospace -F _python_argcomplete ./test.py

The advice (added in #198) says when this happens try removing the completion with complete -r ./test.py. But it's not clear how this solves the problem - Trying this again in the same or a new shell presents the same issue for me. Did I miss something?

I also tried specifying the full path to ./test.py, which doesn't make a difference.

test.py is a pretty minimal example. ```python #!/usr/bin/env python3 # PYTHON_ARGCOMPLETE_OK import argparse import argcomplete from pathlib import Path if __name__ == "__main__": parser = argparse.ArgumentParser() parser.add_argument("-d", "--directory", type=Path) argcomplete.autocomplete(parser) args = parser.parse_args() print(args) ```
evanunderscore commented 3 years ago

If Bash continues to register default completion functions, then argcomplete's global completion function isn't actually running. What did you do to install it? It's been a while since I've used this, but from memory I usually test it with eval "$(activate-global-python-argcomplete --dest=-)".

celynw commented 3 years ago

I followed the instructions in the README, which was a one-time run of activate-global-python-argcomplete.

According to my bash history, I also tried

The latter contains my custom completion files, as well as python-argcomplete - I assume that was put there as part of the normal installation but I can't be certain.

Your method seems to work! I do have to run that for every new shell though - but I can just make .bashrc do that. I'll leave this issue open for now since it only seems to be a workaround, but it's definitely useful. Thanks!

vanadinit commented 2 years ago

I ran into the same issue.

I first tried the explicit (eval) method for my script, which worked fine. Then I tried the global activation without success so far:

$ complete | grep argcomplete
complete -o bashdefault -o default -F _python_argcomplete_global -D
$ complete | grep automatix
complete -F _minimal automatix
$ complete -r automatix
$ complete | grep automatix
$ automatix -<tab>
$ complete | grep automatix
complete -F _minimal automatix
$ eval "$(register-python-argcomplete automatix)"
$ complete | grep automatix
complete -o bashdefault -o default -o nospace -F _python_argcomplete automatix
$ automatix -<tab>
-d                -f                -h                -i                -j                -p                --systems         --vars-file       
--debug           --force           --help            --interactive     --jump-to         --print-overview  --vars

Seems that the default registration is added at the time I try to tab complete, even if the global activation was already registered. I see also no other default entry in the output of complete.

Since I have only one script so far I am going to use the explicit method in my bashrc.