|
| 1 | +"""allow bash-completion for argparse with argcomplete if installed |
| 2 | +needs argcomplete>=0.5.6 for python 3.2/3.3 (older versions fail |
| 3 | +to find the magic string, so _ARGCOMPLETE env. var is never set, and |
| 4 | +this does not need special code. |
| 5 | +
|
| 6 | +Function try_argcomplete(parser) should be called directly before |
| 7 | +the call to ArgumentParser.parse_args(). |
| 8 | +
|
| 9 | +The filescompleter is what you normally would use on the positional |
| 10 | +arguments specification, in order to get "dirname/" after "dirn<TAB>" |
| 11 | +instead of the default "dirname ": |
| 12 | +
|
| 13 | + optparser.add_argument(Config._file_or_dir, nargs='*' |
| 14 | + ).completer=filescompleter |
| 15 | +
|
| 16 | +Other, application specific, completers should go in the file |
| 17 | +doing the add_argument calls as they need to be specified as .completer |
| 18 | +attributes as well. (If argcomplete is not installed, the function the |
| 19 | +attribute points to will not be used). |
| 20 | +
|
| 21 | +SPEEDUP |
| 22 | +======= |
| 23 | +The generic argcomplete script for bash-completion |
| 24 | +(/etc/bash_completion.d/python-argcomplete.sh ) |
| 25 | +uses a python program to determine startup script generated by pip. |
| 26 | +You can speed up completion somewhat by changing this script to include |
| 27 | + # PYTHON_ARGCOMPLETE_OK |
| 28 | +so the the python-argcomplete-check-easy-install-script does not |
| 29 | +need to be called to find the entry point of the code and see if that is |
| 30 | +marked with PYTHON_ARGCOMPLETE_OK |
| 31 | +
|
| 32 | +INSTALL/DEBUGGING |
| 33 | +================= |
| 34 | +To include this support in another application that has setup.py generated |
| 35 | +scripts: |
| 36 | +- add the line: |
| 37 | + # PYTHON_ARGCOMPLETE_OK |
| 38 | + near the top of the main python entry point |
| 39 | +- include in the file calling parse_args(): |
| 40 | + from _argcomplete import try_argcomplete, filescompleter |
| 41 | + , call try_argcomplete just before parse_args(), and optionally add |
| 42 | + filescompleter to the positional arguments' add_argument() |
| 43 | +If things do not work right away: |
| 44 | +- switch on argcomplete debugging with (also helpful when doing custom |
| 45 | + completers): |
| 46 | + export _ARC_DEBUG=1 |
| 47 | +- run: |
| 48 | + python-argcomplete-check-easy-install-script $(which appname) |
| 49 | + echo $? |
| 50 | + will echo 0 if the magic line has been found, 1 if not |
| 51 | +- sometimes it helps to find early on errors using: |
| 52 | + _ARGCOMPLETE=1 _ARC_DEBUG=1 appname |
| 53 | + which should throw a KeyError: 'COMPLINE' (which is properly set by the |
| 54 | + global argcomplete script). |
| 55 | +""" |
| 56 | +import argparse |
| 57 | +import os |
| 58 | +import sys |
| 59 | +from glob import glob |
| 60 | +from typing import Any |
| 61 | +from typing import List |
| 62 | +from typing import Optional |
| 63 | + |
| 64 | + |
| 65 | +class FastFilesCompleter: |
| 66 | + "Fast file completer class" |
| 67 | + |
| 68 | + def __init__(self, directories: bool = True) -> None: |
| 69 | + self.directories = directories |
| 70 | + |
| 71 | + def __call__(self, prefix: str, **kwargs: Any) -> List[str]: |
| 72 | + """only called on non option completions""" |
| 73 | + if os.path.sep in prefix[1:]: |
| 74 | + prefix_dir = len(os.path.dirname(prefix) + os.path.sep) |
| 75 | + else: |
| 76 | + prefix_dir = 0 |
| 77 | + completion = [] |
| 78 | + globbed = [] |
| 79 | + if "*" not in prefix and "?" not in prefix: |
| 80 | + # we are on unix, otherwise no bash |
| 81 | + if not prefix or prefix[-1] == os.path.sep: |
| 82 | + globbed.extend(glob(prefix + ".*")) |
| 83 | + prefix += "*" |
| 84 | + globbed.extend(glob(prefix)) |
| 85 | + for x in sorted(globbed): |
| 86 | + if os.path.isdir(x): |
| 87 | + x += "/" |
| 88 | + # append stripping the prefix (like bash, not like compgen) |
| 89 | + completion.append(x[prefix_dir:]) |
| 90 | + return completion |
| 91 | + |
| 92 | + |
| 93 | +if os.environ.get("_ARGCOMPLETE"): |
| 94 | + try: |
| 95 | + import argcomplete.completers |
| 96 | + except ImportError: |
| 97 | + sys.exit(-1) |
| 98 | + filescompleter = FastFilesCompleter() # type: Optional[FastFilesCompleter] |
| 99 | + |
| 100 | + def try_argcomplete(parser: argparse.ArgumentParser) -> None: |
| 101 | + argcomplete.autocomplete(parser, always_complete_options=False) |
| 102 | + |
| 103 | + |
| 104 | +else: |
| 105 | + |
| 106 | + def try_argcomplete(parser: argparse.ArgumentParser) -> None: |
| 107 | + pass |
| 108 | + |
| 109 | + filescompleter = None |
0 commit comments