Repository: dbrgn/tealdeer
Pull request #217 (2021-10-17 22:10:05)

Rename `-o / --os` to `-p / --platform`
This makes it compliant with the client spec (see #209).

The `-p` flag was previously used for the pager, using the pager now requires using the long flag (`--pager`). Alternatively the pager can be configured through the config file.


Patch:
diff --git a/bash_tealdeer b/bash_tealdeer
index 6ed44cd..e45c886 100644
--- a/bash_tealdeer
+++ b/bash_tealdeer
@@ -13,7 +13,7 @@ _tealdeer()
 			_filedir
 			return
 			;;
-		-o|--os)
+		-p|--platform)
 			COMPREPLY=( $(compgen -W 'linux osx sunos windows' -- "${cur}") )
 			return
 			;;
diff --git a/docs/src/usage.txt b/docs/src/usage.txt
index e677621..a88af6f 100644
--- a/docs/src/usage.txt
+++ b/docs/src/usage.txt
@@ -13,12 +13,14 @@ ARGS:
 OPTIONS:
     -l, --list                   List all commands in the cache
     -f, --render <FILE>          Render a specific markdown file
-    -o, --os <OS>                Override the operating system [possible values: linux, osx, sunos,
+    -p, --platform <PLATFORM>    Override the operating system [possible values: linux, osx, sunos,
+                                 windows]
+    -o, --os <OS>                Deprecated alias of `platform` [possible values: linux, osx, sunos,
                                  windows]
     -L, --language <LANGUAGE>    Override the language
     -u, --update                 Update the local cache
     -c, --clear-cache            Clear the local cache
-    -p, --pager                  Use a pager to page output
+        --pager                  Use a pager to page output
     -r, --raw                    Display the raw markdown instead of rendering it
     -q, --quiet                  Suppress informational messages
         --show-paths             Show file and directory paths used by tealdeer
diff --git a/fish_tealdeer b/fish_tealdeer
index cad6cc2..57d3e31 100644
--- a/fish_tealdeer
+++ b/fish_tealdeer
@@ -7,7 +7,7 @@ complete -c tldr -s h -l help        -d 'Print the help message.' -f
 complete -c tldr -s v -l version     -d 'Show version information.' -f
 complete -c tldr -s l -l list        -d 'List all commands in the cache.' -f
 complete -c tldr -s f -l render      -d 'Render a specific markdown file.' -r
-complete -c tldr -s o -l os          -d 'Override the operating system.' -xa 'linux osx sunos windows'
+complete -c tldr -s p -l platform    -d 'Override the operating system.' -xa 'linux osx sunos windows'
 complete -c tldr -s u -l update      -d 'Update the local cache.' -f
 complete -c tldr -s c -l clear-cache -d 'Clear the local cache.' -f
 complete -c tldr -s p -l pager       -d 'Use a pager to page output.' -f
diff --git a/src/cache.rs b/src/cache.rs
index a4a2100..ac6347c 100644
--- a/src/cache.rs
+++ b/src/cache.rs
@@ -16,7 +16,7 @@ use zip::ZipArchive;
 
 use crate::{
     error::TealdeerError::{self, CacheError, UpdateError},
-    types::{OsType, PathSource},
+    types::{PathSource, PlatformType},
 };
 
 static CACHE_DIR_ENV_VAR: &str = "TEALDEER_CACHE_DIR";
@@ -27,7 +27,7 @@ static TLDR_OLD_PAGES_DIR: &str = "tldr-master";
 #[derive(Debug)]
 pub struct Cache {
     url: String,
-    os: OsType,
+    platform: PlatformType,
 }
 
 #[derive(Debug)]
@@ -64,13 +64,13 @@ pub enum CacheFreshness {
 }
 
 impl Cache {
-    pub fn new<S>(url: S, os: OsType) -> Self
+    pub fn new<S>(url: S, platform: PlatformType) -> Self
     where
         S: Into<String>,
     {
         Self {
             url: url.into(),
-            os,
+            platform,
         }
     }
 
@@ -196,11 +196,11 @@ impl Cache {
 
     /// Return the platform directory.
     fn get_platform_dir(&self) -> &'static str {
-        match self.os {
-            OsType::Linux => "linux",
-            OsType::OsX => "osx",
-            OsType::SunOs => "sunos",
-            OsType::Windows => "windows",
+        match self.platform {
+            PlatformType::Linux => "linux",
+            PlatformType::OsX => "osx",
+            PlatformType::SunOs => "sunos",
+            PlatformType::Windows => "windows",
         }
     }
 
diff --git a/src/main.rs b/src/main.rs
index 6f182b7..e899d39 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -40,7 +40,7 @@ use crate::{
     error::TealdeerError::ConfigError,
     extensions::Dedup,
     output::print_page,
-    types::{ColorOptions, OsType},
+    types::{ColorOptions, PlatformType},
 };
 
 const NAME: &str = "tealdeer";
@@ -81,13 +81,22 @@ struct Args {
     render: Option<PathBuf>,
 
     /// Override the operating system
+    #[clap(
+        short = 'p',
+        long = "platform",
+        requires = "command",
+        possible_values = ["linux", "osx", "sunos", "windows"],
+    )]
+    platform: Option<PlatformType>,
+
+    /// Deprecated alias of `platform`
     #[clap(
         short = 'o',
         long = "os",
         requires = "command",
-        possible_values = ["linux", "osx", "sunos", "windows"]
+        possible_values = ["linux", "osx", "sunos", "windows"],
     )]
-    os: Option<OsType>,
+    os: Option<PlatformType>,
 
     /// Override the language
     #[clap(short = 'L', long = "language")]
@@ -102,7 +111,7 @@ struct Args {
     clear_cache: bool,
 
     /// Use a pager to page output
-    #[clap(short = 'p', long = "pager", requires = "command")]
+    #[clap(long = "pager", requires = "command")]
     pager: bool,
 
     /// Display the raw markdown instead of rendering it
@@ -356,6 +365,10 @@ fn main() {
             args.raw = true;
             eprintln!("Warning: The -m / --markdown flag is deprecated, use -r / --raw instead");
         }
+        if args.os.is_some() {
+            eprintln!("Warning: The -o / --os flag is deprecated, use -p / --platform instead");
+        }
+        args.platform = args.platform.or(args.os);
 
         args
     };
@@ -412,10 +425,7 @@ fn main() {
     }
 
     // Specify target OS
-    let os: OsType = match args.os {
-        Some(os) => os,
-        None => OsType::current(),
-    };
+    let platform: PlatformType = args.platform.unwrap_or_else(PlatformType::current);
 
     // If a local file was passed in, render it and exit
     if let Some(file) = args.render {
@@ -429,7 +439,7 @@ fn main() {
     }
 
     // Initialize cache
-    let cache = Cache::new(ARCHIVE_URL, os);
+    let cache = Cache::new(ARCHIVE_URL, platform);
 
     // Clear cache, pass through
     if args.clear_cache {
diff --git a/src/types.rs b/src/types.rs
index 3e6a161..7d018a8 100644
--- a/src/types.rs
+++ b/src/types.rs
@@ -7,14 +7,14 @@ use serde_derive::{Deserialize, Serialize};
 #[derive(Debug, Eq, PartialEq, Copy, Clone, Serialize, Deserialize)]
 #[serde(rename_all = "lowercase")]
 #[allow(dead_code)]
-pub enum OsType {
+pub enum PlatformType {
     Linux,
     OsX,
     SunOs,
     Windows,
 }
 
-impl fmt::Display for OsType {
+impl fmt::Display for PlatformType {
     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
         match self {
             Self::Linux => write!(f, "Linux"),
@@ -25,7 +25,7 @@ impl fmt::Display for OsType {
     }
 }
 
-impl str::FromStr for OsType {
+impl str::FromStr for PlatformType {
     type Err = String;
 
     fn from_str(s: &str) -> Result<Self, Self::Err> {
@@ -42,7 +42,7 @@ impl str::FromStr for OsType {
     }
 }
 
-impl OsType {
+impl PlatformType {
     #[cfg(target_os = "linux")]
     pub fn current() -> Self {
         Self::Linux
diff --git a/zsh_tealdeer b/zsh_tealdeer
index 0674063..cd4e2a5 100644
--- a/zsh_tealdeer
+++ b/zsh_tealdeer
@@ -14,7 +14,7 @@ _tealdeer() {
     args+=(
         "($I -l --list)"{-l,--list}"[List all commands in the cache]"
         "($I -f --render)"{-f,--render}"[Render a specific markdown file]:file:_files"
-        "($I -o --os)"{-o,--os}'[Override the operating system]:os:((
+        "($I -p --platform)"{-p,--platform}'[Override the operating system]:platform:((
             linux
             osx
             sunos


Test patch:
diff --git a/tests/lib.rs b/tests/lib.rs
index 6ff88d2..de212d2 100644
--- a/tests/lib.rs
+++ b/tests/lib.rs
@@ -325,7 +325,7 @@ fn test_os_specific_page() {
 
     testenv
         .command()
-        .args(["--os", "sunos", "truss"])
+        .args(["--platform", "sunos", "truss"])
         .assert()
         .success();
 }
@@ -685,7 +685,7 @@ fn test_pager_warning() {
     // But it should be shown if the pager flag is true
     testenv
         .command()
-        .args(["which", "-p"])
+        .args(["--pager", "which"])
         .assert()
         .success()
         .stderr(contains("pager flag not available on Windows"));