kislyuk / argcomplete

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

No autocomplete when using sudo #65

Open markeganfuller opened 10 years ago

markeganfuller commented 10 years ago

When using sudo I get no autocompletion despite getting autocompletion for other commands (eg. git). Autocompletion works when running directly as root.

Tested on Ubuntu 12.04.3 LTS

$ example.py <TAB>
-h                                 --help

$ sudo example.py <TAB>

$ sudo git <TAB>
add                 bundle              credential-cache    format-patch        lg                  notes               request-pull        stash 
am                  checkout            credential-store    fsck                log                 pull                reset               status 
annotate            cherry              describe            gc                  lol                 push                revert              submodule 
apply               cherry-pick         df                  get-tar-commit-id   lola                rebase              rm                  tag 
archive             ci                  diff                grep                ls                  reflog              shortlog            whatchanged 
bisect              clean               difftool            help                merge               relink              show                
blame               clone               fetch               imap-send           mergetool           remote              show-branch         
br                  commit              filter-branch       init                mv                  repack              st                  
branch              config              flow                instaweb            name-rev            replace             stage 

# example.py <TAB>
-h                                 --help

Example Code

#!/usr/bin/env python
# PYTHON_ARGCOMPLETE_OK
import argparse
import argcomplete

parser = argparse.ArgumentParser()
argcomplete.autocomplete(parser)
args = vars(parser.parse_args())
kislyuk commented 10 years ago

Argcomplete completion works with sudo when registering completion targets individually with register-python-argcomplete (e.g., eval "$(register-python-argcomplete $(pwd)/my-awesome-script.py)").

Unfortunately, fixing it with global completion is tricky. In global completion, the function is not relying on bash to recognize the completion target by name like "my-awesome-script.py" or "git", instead it's given a name and must find out on its own whether it's a Python script that should be completed. This relies on complete -D, which works fine in general, but when you type sudo <...><TAB>, bash completion actually invokes _command_offset, which appears to not be compatible with complete -D at all.

kislyuk commented 10 years ago

So basically, if I understand correctly, the only way to make argcomplete global completion work with sudo is to patch _command_offset, which is part of the core bash-completion distribution, to be compatible with complete -D.

The next step then is to open an issue upstream and start a patch against bash-completion.

markeganfuller commented 10 years ago

Cool, thanks for looking into this. I'll leave the issue submission to you as you're better informed of the underlying issues.

Thanks,