From e05922c6564d31f06600c83039dece8a425a0637 Mon Sep 17 00:00:00 2001 From: Itamar Shalev Date: Mon, 30 Jun 2025 16:31:54 +0300 Subject: [PATCH 1/7] tldr: env: Add TLDR_PLATFORM env var to override platform detection Add support for the TLDR_PLATFORM environment variable to let users specify the desired platform (e.g., "linux", "macos", "windows") for fetching TLDR pages. This overrides the default platform detection. The --platform option remains supported for per-command overrides. Signed-off-by: Itamar Shalev --- README.md | 14 ++++++++++++++ tldr.py | 7 +++++++ 2 files changed, 21 insertions(+) diff --git a/README.md b/README.md index 53a87ba..000bd58 100644 --- a/README.md +++ b/README.md @@ -91,8 +91,22 @@ export TLDR_CACHE_MAX_AGE=720 export TLDR_PAGES_SOURCE_LOCATION="https://raw.githubusercontent.com/tldr-pages/tldr/main/pages" export TLDR_DOWNLOAD_CACHE_LOCATION="https://github.com/tldr-pages/tldr/releases/latest/download/tldr.zip" export TLDR_OPTIONS=short +export TLDR_PLATFORM=linux ``` +### Platform +The platform that tldr will use is determined by the `TLDR_PLATFORM` environment variable. +If it is not set, the client will try to determine the platform automatically based on the system it is running on. +The following values are supported: +- `linux` +- `windows` +- `android` +- `freebsd` +- `netbsd` +- `openbsd` +- `osx` +- `sunos` + ### Cache Cache is downloaded from `TLDR_DOWNLOAD_CACHE_LOCATION` (defaults to the one described in [the client specification](https://github.com/tldr-pages/tldr/blob/main/CLIENT-SPECIFICATION.md#caching)), unzipped and extracted into the [local cache directory](#cache-location). Pages are loaded directly from `TLDR_PAGES_SOURCE_LOCATION` if `tldr ` is used. diff --git a/tldr.py b/tldr.py index 161e960..f8e5b06 100755 --- a/tldr.py +++ b/tldr.py @@ -202,6 +202,13 @@ def update_page_for_platform( def get_platform() -> str: + if platform := os.environ.get('TLDR_PLATFORM'): + platform = platform.lower() + if platform in OS_DIRECTORIES: + return OS_DIRECTORIES[platform] + else: + print("Warning: Invalid platform specified in environment variable TLDR_PLATFORM.") + for key in OS_DIRECTORIES: if sys.platform.startswith(key): return OS_DIRECTORIES[key] From fd8af2b5aa8097cd6b83548bb51097b1b310f42c Mon Sep 17 00:00:00 2001 From: Itamar Shalev Date: Mon, 30 Jun 2025 17:37:49 +0300 Subject: [PATCH 2/7] readme: Add additional supported os Signed-off-by: Itamar Shalev --- README.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/README.md b/README.md index 000bd58..89b6c70 100644 --- a/README.md +++ b/README.md @@ -100,8 +100,11 @@ If it is not set, the client will try to determine the platform automatically ba The following values are supported: - `linux` - `windows` +- `win32` - `android` +- `darwin` - `freebsd` +- `macos` - `netbsd` - `openbsd` - `osx` From 080137ca71e562812bb1955662e920bd053c9043 Mon Sep 17 00:00:00 2001 From: Itamar Shalev Date: Mon, 30 Jun 2025 21:23:31 +0300 Subject: [PATCH 3/7] fix: Support only origin platforms without symlinks and aliases Signed-off-by: Itamar Shalev --- README.md | 3 --- tldr.py | 15 ++++++++++----- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 89b6c70..000bd58 100644 --- a/README.md +++ b/README.md @@ -100,11 +100,8 @@ If it is not set, the client will try to determine the platform automatically ba The following values are supported: - `linux` - `windows` -- `win32` - `android` -- `darwin` - `freebsd` -- `macos` - `netbsd` - `openbsd` - `osx` diff --git a/tldr.py b/tldr.py index f8e5b06..5d6af3b 100755 --- a/tldr.py +++ b/tldr.py @@ -59,6 +59,8 @@ "windows": "windows" } +SUPPORTED_PLATFORMS = sorted(set(OS_DIRECTORIES.values())) + class CacheNotExist(Exception): pass @@ -202,12 +204,15 @@ def update_page_for_platform( def get_platform() -> str: - if platform := os.environ.get('TLDR_PLATFORM'): - platform = platform.lower() - if platform in OS_DIRECTORIES: - return OS_DIRECTORIES[platform] + platform_env = os.environ.get('TLDR_PLATFORM', '').strip().lower() + if platform_env: + if platform_env in SUPPORTED_PLATFORMS: + return platform_env else: - print("Warning: Invalid platform specified in environment variable TLDR_PLATFORM.") + print( + f"Warning: '{platform_env}' is not a supported TLDR_PLATFORM env value." + "\nFalling back to auto-detection." + ) for key in OS_DIRECTORIES: if sys.platform.startswith(key): From 0430d34661760b5c32daf7cb4b6056848126bd65 Mon Sep 17 00:00:00 2001 From: Itamar Shalev Date: Tue, 1 Jul 2025 12:03:12 +0300 Subject: [PATCH 4/7] env: Use env platform if --platform wasn't given Signed-off-by: Itamar Shalev --- README.md | 13 ++----------- tldr.py | 29 ++++++++++++++--------------- 2 files changed, 16 insertions(+), 26 deletions(-) diff --git a/README.md b/README.md index 000bd58..f678337 100644 --- a/README.md +++ b/README.md @@ -95,17 +95,8 @@ export TLDR_PLATFORM=linux ``` ### Platform -The platform that tldr will use is determined by the `TLDR_PLATFORM` environment variable. -If it is not set, the client will try to determine the platform automatically based on the system it is running on. -The following values are supported: -- `linux` -- `windows` -- `android` -- `freebsd` -- `netbsd` -- `openbsd` -- `osx` -- `sunos` +Determines the platform that tldr will use based on the `TLDR_PLATFORM` environment variable or system detection. +For a complete list of supported platform values, refer to the help for the `--platform` option flag. ### Cache diff --git a/tldr.py b/tldr.py index 5d6af3b..ad3ed51 100755 --- a/tldr.py +++ b/tldr.py @@ -56,12 +56,10 @@ "osx": "osx", "sunos": "sunos", "win32": "windows", - "windows": "windows" + "windows": "windows", + "common": "common", } -SUPPORTED_PLATFORMS = sorted(set(OS_DIRECTORIES.values())) - - class CacheNotExist(Exception): pass @@ -204,16 +202,6 @@ def update_page_for_platform( def get_platform() -> str: - platform_env = os.environ.get('TLDR_PLATFORM', '').strip().lower() - if platform_env: - if platform_env in SUPPORTED_PLATFORMS: - return platform_env - else: - print( - f"Warning: '{platform_env}' is not a supported TLDR_PLATFORM env value." - "\nFalling back to auto-detection." - ) - for key in OS_DIRECTORIES: if sys.platform.startswith(key): return OS_DIRECTORIES[key] @@ -221,7 +209,7 @@ def get_platform() -> str: def get_platform_list() -> List[str]: - platforms = ['common'] + list(set(OS_DIRECTORIES.values())) + platforms = list(set(OS_DIRECTORIES.values())) current_platform = get_platform() platforms.remove(current_platform) platforms.insert(0, current_platform) @@ -681,6 +669,17 @@ def main() -> None: options = parser.parse_args() + + if options.platform is None: + platform_env = os.environ.get('TLDR_PLATFORM', '').strip().lower() + if platform_env in OS_DIRECTORIES: + options.platform = [platform_env] + elif platform_env: + print( + f"Warning: '{platform_env}' is not a supported TLDR_PLATFORM env value." + "\nFalling back to auto-detection." + ) + display_option_length = "long" if os.environ.get('TLDR_OPTIONS') == "short": display_option_length = "short" From 07484c89b13b3784483d10a224b782da4e2f9ece Mon Sep 17 00:00:00 2001 From: Itamar Shalev Date: Tue, 1 Jul 2025 12:06:45 +0300 Subject: [PATCH 5/7] tldr: platform: Avoid duplicate code and manual os meantions Signed-off-by: Itamar Shalev --- tldr.py | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/tldr.py b/tldr.py index ad3ed51..925a9cb 100755 --- a/tldr.py +++ b/tldr.py @@ -597,12 +597,9 @@ def create_parser() -> ArgumentParser: nargs=1, default=None, type=str, - choices=['android', 'freebsd', 'linux', 'netbsd', 'openbsd', 'osx', 'sunos', - 'windows', 'common'], + choices=list(OS_DIRECTORIES), metavar='PLATFORM', - help="Override the operating system " - "[android, freebsd, linux, netbsd, openbsd," - " osx, sunos, windows, common]" + help="Override the operating system " + str(list(OS_DIRECTORIES)) ) parser.add_argument('-l', '--list', From 06355c220337427d23af77677258b4a3cd39ed5b Mon Sep 17 00:00:00 2001 From: Itamar Shalev Date: Tue, 1 Jul 2025 12:55:38 +0300 Subject: [PATCH 6/7] fix linter issues Signed-off-by: Itamar Shalev --- tldr.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tldr.py b/tldr.py index 925a9cb..4c5f0ae 100755 --- a/tldr.py +++ b/tldr.py @@ -60,6 +60,7 @@ "common": "common", } + class CacheNotExist(Exception): pass @@ -666,7 +667,6 @@ def main() -> None: options = parser.parse_args() - if options.platform is None: platform_env = os.environ.get('TLDR_PLATFORM', '').strip().lower() if platform_env in OS_DIRECTORIES: From 8f54f452e181c71a2b0454860a3b4bc6f4c4acfe Mon Sep 17 00:00:00 2001 From: Itamar Shalev Date: Tue, 1 Jul 2025 14:10:53 +0300 Subject: [PATCH 7/7] tldr: platform: allow only original values Signed-off-by: Itamar Shalev --- tldr.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/tldr.py b/tldr.py index 4c5f0ae..852f9ad 100755 --- a/tldr.py +++ b/tldr.py @@ -593,14 +593,17 @@ def create_parser() -> ArgumentParser: action='store_true', help="Delete the local cache of pages and exit") + all_platforms = sorted(set(OS_DIRECTORIES.values())) + platforms_str = "[" + ", ".join(all_platforms) + "]" + parser.add_argument( '-p', '--platform', nargs=1, default=None, type=str, - choices=list(OS_DIRECTORIES), + choices=all_platforms, metavar='PLATFORM', - help="Override the operating system " + str(list(OS_DIRECTORIES)) + help=f"Override the operating system {platforms_str}" ) parser.add_argument('-l', '--list',