diff --git a/README.md b/README.md index 99f804a..b06705a 100644 --- a/README.md +++ b/README.md @@ -43,7 +43,7 @@ sudo snap install tldr ## Usage -```txt +```bash usage: tldr command [options] Python command line client for tldr @@ -57,6 +57,7 @@ options: --search "KEYWORDS" Search for a specific command from a query -u, --update, --update_cache Update the local cache of pages and exit + -k, --clear-cache Delete the local cache of pages and exit -p PLATFORM, --platform PLATFORM Override the operating system [android, freebsd, linux, netbsd, openbsd, osx, sunos, windows, common] -l, --list List all available commands for operating system @@ -67,6 +68,8 @@ options: -L LANGUAGE, --language LANGUAGE Override the default language -m, --markdown Just print the plain page file. + --short-options Display shortform options over longform + --long-options Display longform options over shortform --print-completion {bash,zsh,tcsh} print shell completion script ``` @@ -106,7 +109,11 @@ In order of precedence: - `$HOME/.cache/tldr` - `~/.cache/tldr` -If you are experiencing issues with *tldr*, consider deleting the cache files before trying other measures. +If you are experiencing issues with *tldr*, consider deleting the cache files before trying other measures: + +```bash +tldr --clear-cache +``` #### Autocomplete @@ -139,7 +146,7 @@ will disable SSL certificate inspection. This __should be avoided__ unless absol Alternatively, It is possible to use a different certificate store/bundle by setting: -* `TLDR_CERT=/path/to/certificates.crt` +- `TLDR_CERT=/path/to/certificates.crt` ### Colors diff --git a/tldr.py b/tldr.py index afbde86..4b596a6 100755 --- a/tldr.py +++ b/tldr.py @@ -16,6 +16,7 @@ from urllib.error import HTTPError, URLError from termcolor import colored import shtab +import shutil __version__ = "3.3.0" __client_specification__ = "2.2" @@ -542,6 +543,23 @@ def update_cache(language: Optional[List[str]] = None) -> None: ) +def clear_cache(language: Optional[List[str]] = None) -> None: + languages = get_language_list() + if language and language[0] not in languages: + languages.append(language[0]) + for language in languages: + pages_dir = f'pages.{language}' if language != 'en' else 'pages' + cache_dir = get_cache_dir() / pages_dir + if cache_dir.exists() and cache_dir.is_dir(): + try: + shutil.rmtree(cache_dir) + print(f"Cleared cache for language {language}") + except Exception as e: + print(f"Error: Unable to delete cache directory {cache_dir}: {e}") + else: + print(f"No cache directory found for language {language}") + + def create_parser() -> ArgumentParser: parser = ArgumentParser( prog="tldr", @@ -566,6 +584,10 @@ def create_parser() -> ArgumentParser: action='store_true', help="Update the local cache of pages and exit") + parser.add_argument('-k', '--clear-cache', + action='store_true', + help="Delete the local cache of pages and exit") + parser.add_argument( '-p', '--platform', nargs=1, @@ -670,6 +692,12 @@ def main() -> None: elif len(sys.argv) == 1: parser.print_help(sys.stderr) sys.exit(1) + if options.clear_cache: + clear_cache(language=options.language) + return + elif len(sys.argv) == 1: + parser.print_help(sys.stderr) + sys.exit(1) if options.list: print('\n'.join(get_commands(options.platform, options.language))) elif options.render: