Skip to content

Commit d273fcd

Browse files
wcheangMasterOdin
andauthored
Add autocomplete and --list option for listing commands (#126)
* Add autocomplete and --list option for listing commands (#69) * Fix bug in get_commands() from code review suggestion. * Default platforms to None for get_commands() Co-authored-by: Matthew Peveler <[email protected]>
1 parent 21a712e commit d273fcd

File tree

3 files changed

+32
-4
lines changed

3 files changed

+32
-4
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ optional arguments:
4242
Override the default page source
4343
-c, --color Override color stripping
4444
-r, --render Render local markdown files
45+
-l, --list List all available commands for operating system
4546
-L LANGUAGE, --language LANGUAGE
4647
Override the default language
4748
```
@@ -79,6 +80,10 @@ In order of precedence:
7980

8081
If you are experiencing issues with *tldr*, consider deleting the cache files before trying other measures.
8182

83+
#### Autocomplete
84+
85+
`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.
86+
8287
### SSL Inspection
8388

8489
For networks that sit behind a proxy, it may be necessary to disable SSL verification for the client to function. Setting the following:

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
"tldr = tldr:cli"
2727
]
2828
},
29-
install_requires=['termcolor', 'colorama'],
29+
install_requires=['termcolor', 'colorama', 'argcomplete'],
3030
tests_require=[
3131
'pytest',
3232
'pytest-runner',

tldr.py

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#!/usr/bin/env python3
2+
# PYTHON_ARGCOMPLETE_OK
23

34
import sys
45
import os
@@ -13,6 +14,8 @@
1314
from urllib.error import HTTPError, URLError
1415
from termcolor import colored
1516
import colorama # Required for Windows
17+
import argcomplete
18+
from glob import glob
1619

1720
__version__ = "1.0.0"
1821
__client_specification__ = "1.2"
@@ -221,6 +224,17 @@ def get_page(command, remote=None, platforms=None, languages=None):
221224

222225
COMMAND_SPLIT_REGEX = re.compile(r'(?P<param>{{.+?}})')
223226
PARAM_REGEX = re.compile(r'(?:{{)(?P<param>.+?)(?:}})')
227+
CACHE_FILE_REGEX = re.compile(r'.*\/(.*)\.md')
228+
229+
230+
def get_commands(platforms=None):
231+
if platforms is None:
232+
platforms = get_platform_list()
233+
234+
cache_files = []
235+
for platform in platforms:
236+
cache_files += glob(os.path.join(get_cache_dir(), 'pages', platform, '*.md'))
237+
return [re.search(CACHE_FILE_REGEX, x).group(1) for x in cache_files]
224238

225239

226240
def colors_of(key):
@@ -312,7 +326,7 @@ def update_cache(language=None):
312326
def main():
313327
parser = ArgumentParser(
314328
prog="tldr",
315-
usage="tldr [-u] [-p PLATFORM] [-s SOURCE] [-c] [-r] [-L LANGUAGE] " +
329+
usage="tldr [-u] [-p PLATFORM] [-l] [-s SOURCE] [-c] [-r] [-L LANGUAGE]" +
316330
"command",
317331
description="Python command line client for tldr"
318332
)
@@ -339,6 +353,11 @@ def main():
339353
help="Override the operating system [linux, osx, sunos, windows, common]"
340354
)
341355

356+
parser.add_argument('-l', '--list',
357+
default=False,
358+
action='store_true',
359+
help="List all available commands for operating system")
360+
342361
parser.add_argument('-s', '--source',
343362
default=PAGES_SOURCE_LOCATION,
344363
type=str,
@@ -363,9 +382,11 @@ def main():
363382
help='Override the default language')
364383

365384
parser.add_argument(
366-
'command', type=str, nargs='*', help="command to lookup"
385+
'command', type=str, nargs='*', help="command to lookup", metavar='command',
386+
choices=get_commands() + [[]]
367387
)
368388

389+
argcomplete.autocomplete(parser)
369390
options = parser.parse_args()
370391

371392
colorama.init(strip=options.color)
@@ -377,7 +398,9 @@ def main():
377398
parser.print_help(sys.stderr)
378399
sys.exit(1)
379400

380-
if options.render:
401+
if options.list:
402+
print(get_commands(options.platform))
403+
elif options.render:
381404
for command in options.command:
382405
if os.path.exists(command):
383406
with open(command, encoding='utf-8') as open_file:

0 commit comments

Comments
 (0)