Skip to content

Commit d51239f

Browse files
patricedeniskbdharunvitorhcl
authored
feat: add --clear-cache option (#249)
* feat : add clear_cache feature - add clear_cache function to clear the local cache for files and directories across languages - add the parser link to the function * README: add description for --clear-cache option and minor fixes * feat: use `shutil.rmtree` to remove cache --------- Signed-off-by: K.B.Dharun Krishna <[email protected]> Co-authored-by: K.B.Dharun Krishna <[email protected]> Co-authored-by: Vitor Henrique <[email protected]>
1 parent 1a565b0 commit d51239f

File tree

2 files changed

+38
-3
lines changed

2 files changed

+38
-3
lines changed

README.md

+10-3
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ sudo snap install tldr
4343

4444
## Usage
4545

46-
```txt
46+
```bash
4747
usage: tldr command [options]
4848

4949
Python command line client for tldr
@@ -57,6 +57,7 @@ options:
5757
--search "KEYWORDS" Search for a specific command from a query
5858
-u, --update, --update_cache
5959
Update the local cache of pages and exit
60+
-k, --clear-cache Delete the local cache of pages and exit
6061
-p PLATFORM, --platform PLATFORM
6162
Override the operating system [android, freebsd, linux, netbsd, openbsd, osx, sunos, windows, common]
6263
-l, --list List all available commands for operating system
@@ -67,6 +68,8 @@ options:
6768
-L LANGUAGE, --language LANGUAGE
6869
Override the default language
6970
-m, --markdown Just print the plain page file.
71+
--short-options Display shortform options over longform
72+
--long-options Display longform options over shortform
7073
--print-completion {bash,zsh,tcsh}
7174
print shell completion script
7275
```
@@ -106,7 +109,11 @@ In order of precedence:
106109
- `$HOME/.cache/tldr`
107110
- `~/.cache/tldr`
108111

109-
If you are experiencing issues with *tldr*, consider deleting the cache files before trying other measures.
112+
If you are experiencing issues with *tldr*, consider deleting the cache files before trying other measures:
113+
114+
```bash
115+
tldr --clear-cache
116+
```
110117

111118
#### Autocomplete
112119

@@ -139,7 +146,7 @@ will disable SSL certificate inspection. This __should be avoided__ unless absol
139146

140147
Alternatively, It is possible to use a different certificate store/bundle by setting:
141148

142-
* `TLDR_CERT=/path/to/certificates.crt`
149+
- `TLDR_CERT=/path/to/certificates.crt`
143150

144151
### Colors
145152

tldr.py

+28
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
from urllib.error import HTTPError, URLError
1717
from termcolor import colored
1818
import shtab
19+
import shutil
1920

2021
__version__ = "3.3.0"
2122
__client_specification__ = "2.2"
@@ -542,6 +543,23 @@ def update_cache(language: Optional[List[str]] = None) -> None:
542543
)
543544

544545

546+
def clear_cache(language: Optional[List[str]] = None) -> None:
547+
languages = get_language_list()
548+
if language and language[0] not in languages:
549+
languages.append(language[0])
550+
for language in languages:
551+
pages_dir = f'pages.{language}' if language != 'en' else 'pages'
552+
cache_dir = get_cache_dir() / pages_dir
553+
if cache_dir.exists() and cache_dir.is_dir():
554+
try:
555+
shutil.rmtree(cache_dir)
556+
print(f"Cleared cache for language {language}")
557+
except Exception as e:
558+
print(f"Error: Unable to delete cache directory {cache_dir}: {e}")
559+
else:
560+
print(f"No cache directory found for language {language}")
561+
562+
545563
def create_parser() -> ArgumentParser:
546564
parser = ArgumentParser(
547565
prog="tldr",
@@ -566,6 +584,10 @@ def create_parser() -> ArgumentParser:
566584
action='store_true',
567585
help="Update the local cache of pages and exit")
568586

587+
parser.add_argument('-k', '--clear-cache',
588+
action='store_true',
589+
help="Delete the local cache of pages and exit")
590+
569591
parser.add_argument(
570592
'-p', '--platform',
571593
nargs=1,
@@ -670,6 +692,12 @@ def main() -> None:
670692
elif len(sys.argv) == 1:
671693
parser.print_help(sys.stderr)
672694
sys.exit(1)
695+
if options.clear_cache:
696+
clear_cache(language=options.language)
697+
return
698+
elif len(sys.argv) == 1:
699+
parser.print_help(sys.stderr)
700+
sys.exit(1)
673701
if options.list:
674702
print('\n'.join(get_commands(options.platform, options.language)))
675703
elif options.render:

0 commit comments

Comments
 (0)