Skip to content

feat: add --clear-cache option #249

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 8 commits into from
Mar 27, 2025
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
13 changes: 10 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ sudo snap install tldr

## Usage

```txt
```bash
usage: tldr command [options]

Python command line client for tldr
Expand All @@ -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
Expand All @@ -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
```
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand Down
28 changes: 28 additions & 0 deletions tldr.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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",
Expand All @@ -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,
Expand Down Expand Up @@ -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:
Expand Down