Skip to content

Commit a82812a

Browse files
Switch to shtab for shell completion (#135)
Co-authored-by: Matthew Peveler <[email protected]>
1 parent 2f6ede5 commit a82812a

File tree

4 files changed

+23
-6
lines changed

4 files changed

+23
-6
lines changed

.github/workflows/test.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ jobs:
2020

2121
- name: Install developer dependencies
2222
run: |
23-
python3 -m pip install -U pip
23+
python3 -m pip install -U pip setuptools
2424
python3 -m pip install -U pytest pytest-runner flake8
2525
2626
- name: Install sphinx dependencies

README.md

+10-1
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,16 @@ If you are experiencing issues with *tldr*, consider deleting the cache files be
9090

9191
#### Autocomplete
9292

93-
`argcomplete` is required for autocompletion. See the `argcomplete` [docs](https://pypi.org/project/argcomplete/) for how to enable `argcomplete`. Cache will also need to be enabled and downloaded.
93+
`shtab` is required for autocompletion using the `--print-completion` argument.
94+
95+
```bash
96+
# bash
97+
tldr --print-completion bash | sudo tee "$BASH_COMPLETION_COMPAT_DIR"/tldr
98+
# zsh
99+
tldr --print-completion zsh | sudo tee /usr/local/share/zsh/site-functions/_tldr
100+
```
101+
102+
See the `shtab` [docs](https://pypi.org/project/shtab/#usage) for other installation methods.
94103

95104
### SSL Inspection
96105

requirements.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
termcolor
22
colorama
3-
argcomplete
3+
shtab>=1.3.10

tldr.py

+11-3
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
from urllib.error import HTTPError, URLError
1515
from termcolor import colored
1616
import colorama # Required for Windows
17-
import argcomplete
17+
import shtab
1818

1919
__version__ = "2.0.0"
2020
__client_specification__ = "1.4"
@@ -435,15 +435,23 @@ def create_parser():
435435

436436
parser.add_argument(
437437
'command', type=str, nargs='*', help="command to lookup", metavar='command'
438-
).completer = argcomplete.completers.ChoicesCompleter(get_commands())
438+
).complete = {"bash": "shtab_tldr_cmd_list", "zsh": "shtab_tldr_cmd_list"}
439+
440+
shtab.add_argument_to(parser, preamble={
441+
'bash': r'''shtab_tldr_cmd_list(){{
442+
compgen -W "$("{py}" -m tldr --list | sed 's/\W/ /g')" -- "$1"
443+
}}'''.format(py=sys.executable),
444+
'zsh': r'''shtab_tldr_cmd_list(){{
445+
_describe 'command' "($("{py}" -m tldr --list | sed 's/\W/ /g'))"
446+
}}'''.format(py=sys.executable)
447+
})
439448

440449
return parser
441450

442451

443452
def main():
444453
parser = create_parser()
445454

446-
argcomplete.autocomplete(parser)
447455
options = parser.parse_args()
448456

449457
colorama.init(strip=options.color)

0 commit comments

Comments
 (0)