Skip to content

Add autocomplete and --list option for listing commands (#69) #126

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ optional arguments:
Override the default page source
-c, --color Override color stripping
-r, --render Render local markdown files
-l, --list List all available commands for operating system
-L LANGUAGE, --language LANGUAGE
Override the default language
```
Expand Down Expand Up @@ -79,6 +80,10 @@ In order of precedence:

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

#### Autocomplete

`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.

### SSL Inspection

For networks that sit behind a proxy, it may be necessary to disable SSL verification for the client to function. Setting the following:
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"tldr = tldr:cli"
]
},
install_requires=['termcolor', 'colorama'],
install_requires=['termcolor', 'colorama', 'argcomplete'],
tests_require=[
'pytest',
'pytest-runner',
Expand Down
29 changes: 26 additions & 3 deletions tldr.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/usr/bin/env python3
# PYTHON_ARGCOMPLETE_OK

import sys
import os
Expand All @@ -13,6 +14,8 @@
from urllib.error import HTTPError, URLError
from termcolor import colored
import colorama # Required for Windows
import argcomplete
from glob import glob

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

COMMAND_SPLIT_REGEX = re.compile(r'(?P<param>{{.+?}})')
PARAM_REGEX = re.compile(r'(?:{{)(?P<param>.+?)(?:}})')
CACHE_FILE_REGEX = re.compile(r'.*\/(.*)\.md')


def get_commands(platforms=None):
if platforms is None:
platforms = get_platform_list()

cache_files = []
for platform in platforms:
cache_files += glob(os.path.join(get_cache_dir(), 'pages', platform, '*.md'))
return [re.search(CACHE_FILE_REGEX, x).group(1) for x in cache_files]


def colors_of(key):
Expand Down Expand Up @@ -312,7 +326,7 @@ def update_cache(language=None):
def main():
parser = ArgumentParser(
prog="tldr",
usage="tldr [-u] [-p PLATFORM] [-s SOURCE] [-c] [-r] [-L LANGUAGE] " +
usage="tldr [-u] [-p PLATFORM] [-l] [-s SOURCE] [-c] [-r] [-L LANGUAGE]" +
"command",
description="Python command line client for tldr"
)
Expand All @@ -339,6 +353,11 @@ def main():
help="Override the operating system [linux, osx, sunos, windows, common]"
)

parser.add_argument('-l', '--list',
default=False,
action='store_true',
help="List all available commands for operating system")

parser.add_argument('-s', '--source',
default=PAGES_SOURCE_LOCATION,
type=str,
Expand All @@ -363,9 +382,11 @@ def main():
help='Override the default language')

parser.add_argument(
'command', type=str, nargs='*', help="command to lookup"
'command', type=str, nargs='*', help="command to lookup", metavar='command',
choices=get_commands() + [[]]
)

argcomplete.autocomplete(parser)
options = parser.parse_args()

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

if options.render:
if options.list:
print(get_commands(options.platform))
elif options.render:
for command in options.command:
if os.path.exists(command):
with open(command, encoding='utf-8') as open_file:
Expand Down