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