Skip to content

tldr: env: Add TLDR_PLATFORM env var to override platform detection #292

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

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
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 @@ -91,8 +91,13 @@ 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
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

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 <command>` is used.
Expand Down
25 changes: 18 additions & 7 deletions tldr.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@
"osx": "osx",
"sunos": "sunos",
"win32": "windows",
"windows": "windows"
"windows": "windows",
"common": "common",
}


Expand Down Expand Up @@ -209,7 +210,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)
Expand Down Expand Up @@ -592,17 +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=['android', 'freebsd', 'linux', 'netbsd', 'openbsd', 'osx', 'sunos',
'windows', 'common'],
choices=all_platforms,
metavar='PLATFORM',
help="Override the operating system "
"[android, freebsd, linux, netbsd, openbsd,"
" osx, sunos, windows, common]"
help=f"Override the operating system {platforms_str}"
)

parser.add_argument('-l', '--list',
Expand Down Expand Up @@ -669,6 +670,16 @@ 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"
Expand Down