2
"""allow bash-completion for argparse with argcomplete if installed
3
needs argcomplete>=0.5.6 for python 3.2/3.3 (older versions fail
4
to find the magic string, so _ARGCOMPLETE env. var is never set, and
5
this does not need special code.
7
argcomplete does not support python 2.5 (although the changes for that
10
Function try_argcomplete(parser) should be called directly before
11
the call to ArgumentParser.parse_args().
13
The filescompleter is what you normally would use on the positional
14
arguments specification, in order to get "dirname/" after "dirn<TAB>"
15
instead of the default "dirname ":
17
optparser.add_argument(Config._file_or_dir, nargs='*'
18
).completer=filescompleter
20
Other, application specific, completers should go in the file
21
doing the add_argument calls as they need to be specified as .completer
22
attributes as well. (If argcomplete is not installed, the function the
23
attribute points to will not be used).
27
The generic argcomplete script for bash-completion
28
(/etc/bash_completion.d/python-argcomplete.sh )
29
uses a python program to determine startup script generated by pip.
30
You can speed up completion somewhat by changing this script to include
31
# PYTHON_ARGCOMPLETE_OK
32
so the the python-argcomplete-check-easy-install-script does not
33
need to be called to find the entry point of the code and see if that is
34
marked with PYTHON_ARGCOMPLETE_OK
38
To include this support in another application that has setup.py generated
41
# PYTHON_ARGCOMPLETE_OK
42
near the top of the main python entry point
43
- include in the file calling parse_args():
44
from _argcomplete import try_argcomplete, filescompleter
45
, call try_argcomplete just before parse_args(), and optionally add
46
filescompleter to the positional arguments' add_argument()
47
If things do not work right away:
48
- switch on argcomplete debugging with (also helpful when doing custom
52
python-argcomplete-check-easy-install-script $(which appname)
54
will echo 0 if the magic line has been found, 1 if not
55
- sometimes it helps to find early on errors using:
56
_ARGCOMPLETE=1 _ARC_DEBUG=1 appname
57
which should throw a KeyError: 'COMPLINE' (which is properly set by the
58
global argcomplete script).
65
class FastFilesCompleter:
66
'Fast file completer class'
67
def __init__(self, directories=True):
68
self.directories = directories
70
def __call__(self, prefix, **kwargs):
71
"""only called on non option completions"""
72
if os.path.sep in prefix[1:]: #
73
prefix_dir = len(os.path.dirname(prefix) + os.path.sep)
78
if '*' not in prefix and '?' not in prefix:
79
if prefix[-1] == os.path.sep: # we are on unix, otherwise no bash
80
globbed.extend(glob(prefix + '.*'))
82
globbed.extend(glob(prefix))
83
for x in sorted(globbed):
86
# append stripping the prefix (like bash, not like compgen)
87
completion.append(x[prefix_dir:])
91
if os.environ.get('_ARGCOMPLETE'):
93
import argcomplete.completers
96
filescompleter = FastFilesCompleter()
98
def try_argcomplete(parser):
99
argcomplete.autocomplete(parser)
101
def try_argcomplete(parser): pass
102
filescompleter = None