From 2e9f2c625dc66728ece46964e3dabd8763303239 Mon Sep 17 00:00:00 2001 From: Danilo Bargen Date: Sat, 7 Mar 2020 17:50:12 +0100 Subject: [PATCH] Argument parsing: Switch from docopt to clap --- Cargo.lock | 103 +++++++++++++++++++++++++++---- Cargo.toml | 2 +- bash_tealdeer | 2 +- fish_tealdeer | 2 +- src/cache.rs | 1 - src/main.rs | 167 ++++++++++++++++++++------------------------------ src/types.rs | 26 +++++--- zsh_tealdeer | 5 +- 8 files changed, 182 insertions(+), 126 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index ad455660..1b0d8d74 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -101,6 +101,35 @@ name = "cfg-if" version = "0.1.10" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "clap" +version = "3.0.0-beta.1" +source = "git+https://github.com/clap-rs/clap/#19c20f7c00310900ce6667baedfe1190e20eeeab" +dependencies = [ + "ansi_term 0.12.1 (registry+https://github.com/rust-lang/crates.io-index)", + "atty 0.2.14 (registry+https://github.com/rust-lang/crates.io-index)", + "bitflags 1.2.1 (registry+https://github.com/rust-lang/crates.io-index)", + "clap_derive 3.0.0-beta.1 (git+https://github.com/clap-rs/clap/)", + "indexmap 1.3.2 (registry+https://github.com/rust-lang/crates.io-index)", + "lazy_static 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)", + "strsim 0.9.3 (registry+https://github.com/rust-lang/crates.io-index)", + "textwrap 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)", + "unicode-width 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)", + "vec_map 0.8.1 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "clap_derive" +version = "3.0.0-beta.1" +source = "git+https://github.com/clap-rs/clap/#19c20f7c00310900ce6667baedfe1190e20eeeab" +dependencies = [ + "heck 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)", + "proc-macro-error 0.4.11 (registry+https://github.com/rust-lang/crates.io-index)", + "proc-macro2 1.0.9 (registry+https://github.com/rust-lang/crates.io-index)", + "quote 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)", + "syn 1.0.16 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "core-foundation" version = "0.6.4" @@ -128,17 +157,6 @@ name = "difference" version = "2.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -[[package]] -name = "docopt" -version = "1.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -dependencies = [ - "lazy_static 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)", - "regex 1.3.4 (registry+https://github.com/rust-lang/crates.io-index)", - "serde 1.0.104 (registry+https://github.com/rust-lang/crates.io-index)", - "strsim 0.9.3 (registry+https://github.com/rust-lang/crates.io-index)", -] - [[package]] name = "dtoa" version = "0.4.5" @@ -718,6 +736,30 @@ dependencies = [ "treeline 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "proc-macro-error" +version = "0.4.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "proc-macro-error-attr 0.4.11 (registry+https://github.com/rust-lang/crates.io-index)", + "proc-macro2 1.0.9 (registry+https://github.com/rust-lang/crates.io-index)", + "quote 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)", + "syn 1.0.16 (registry+https://github.com/rust-lang/crates.io-index)", + "version_check 0.9.1 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "proc-macro-error-attr" +version = "0.4.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "proc-macro2 1.0.9 (registry+https://github.com/rust-lang/crates.io-index)", + "quote 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)", + "syn 1.0.16 (registry+https://github.com/rust-lang/crates.io-index)", + "syn-mid 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", + "version_check 0.9.1 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "proc-macro2" version = "1.0.9" @@ -991,6 +1033,16 @@ dependencies = [ "unicode-xid 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "syn-mid" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "proc-macro2 1.0.9 (registry+https://github.com/rust-lang/crates.io-index)", + "quote 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)", + "syn 1.0.16 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "tar" version = "0.4.26" @@ -1009,7 +1061,7 @@ dependencies = [ "ansi_term 0.12.1 (registry+https://github.com/rust-lang/crates.io-index)", "app_dirs 1.2.1 (registry+https://github.com/rust-lang/crates.io-index)", "assert_cmd 0.10.2 (registry+https://github.com/rust-lang/crates.io-index)", - "docopt 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)", + "clap 3.0.0-beta.1 (git+https://github.com/clap-rs/clap/)", "env_logger 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)", "escargot 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)", "flate2 1.0.13 (registry+https://github.com/rust-lang/crates.io-index)", @@ -1057,6 +1109,14 @@ dependencies = [ "winapi-util 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "textwrap" +version = "0.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "unicode-width 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "thread_local" version = "1.0.1" @@ -1165,6 +1225,11 @@ name = "unicode-segmentation" version = "1.6.0" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "unicode-width" +version = "0.1.7" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "unicode-xid" version = "0.2.0" @@ -1195,6 +1260,11 @@ name = "vcpkg" version = "0.2.8" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "vec_map" +version = "0.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "version_check" version = "0.1.5" @@ -1409,11 +1479,12 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum c2-chacha 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)" = "214238caa1bf3a496ec3392968969cab8549f96ff30652c9e56885329315f6bb" "checksum cc 1.0.50 (registry+https://github.com/rust-lang/crates.io-index)" = "95e28fa049fda1c330bcf9d723be7663a899c4679724b34c81e9f5a326aab8cd" "checksum cfg-if 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)" = "4785bdd1c96b2a846b2bd7cc02e86b6b3dbf14e7e53446c4f54c92a361040822" +"checksum clap 3.0.0-beta.1 (git+https://github.com/clap-rs/clap/)" = "" +"checksum clap_derive 3.0.0-beta.1 (git+https://github.com/clap-rs/clap/)" = "" "checksum core-foundation 0.6.4 (registry+https://github.com/rust-lang/crates.io-index)" = "25b9e03f145fd4f2bf705e07b900cd41fc636598fe5dc452fd0db1441c3f496d" "checksum core-foundation-sys 0.6.2 (registry+https://github.com/rust-lang/crates.io-index)" = "e7ca8a5221364ef15ce201e8ed2f609fc312682a8f4e0e3d4aa5879764e0fa3b" "checksum crc32fast 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ba125de2af0df55319f41944744ad91c71113bf74a4646efff39afe1f6842db1" "checksum difference 2.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "524cbf6897b527295dff137cec09ecf3a05f4fddffd7dfcd1585403449e74198" -"checksum docopt 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "7f525a586d310c87df72ebcd98009e57f1cc030c8c268305287a476beb653969" "checksum dtoa 0.4.5 (registry+https://github.com/rust-lang/crates.io-index)" = "4358a9e11b9a09cf52383b451b49a169e8d797b68aa02301ff586d70d9661ea3" "checksum encoding_rs 0.8.22 (registry+https://github.com/rust-lang/crates.io-index)" = "cd8d03faa7fe0c1431609dfad7bbe827af30f82e1e2ae6f7ee4fca6bd764bc28" "checksum env_logger 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)" = "44533bbbb3bb3c1fa17d9f2e4e38bbbaf8396ba82193c4cb1b6445d711445d36" @@ -1483,6 +1554,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum predicates 1.0.3 (registry+https://github.com/rust-lang/crates.io-index)" = "1188bf092c81c18228c383b190c069a8a613c18a046ffa9fdfc0f5fc8fb2da8a" "checksum predicates-core 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "06075c3a3e92559ff8929e7a280684489ea27fe44805174c3ebd9328dcb37178" "checksum predicates-tree 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "8e63c4859013b38a76eca2414c64911fba30def9e3202ac461a2d22831220124" +"checksum proc-macro-error 0.4.11 (registry+https://github.com/rust-lang/crates.io-index)" = "e7959c6467d962050d639361f7703b2051c43036d03493c36f01d440fdd3138a" +"checksum proc-macro-error-attr 0.4.11 (registry+https://github.com/rust-lang/crates.io-index)" = "e4002d9f55991d5e019fb940a90e1a95eb80c24e77cb2462dd4dc869604d543a" "checksum proc-macro2 1.0.9 (registry+https://github.com/rust-lang/crates.io-index)" = "6c09721c6781493a2a492a96b5a5bf19b65917fe6728884e7c44dd0c60ca3435" "checksum quick-error 1.2.3 (registry+https://github.com/rust-lang/crates.io-index)" = "a1d01941d82fa2ab50be1e79e6714289dd7cde78eba4c074bc5a4374f650dfe0" "checksum quote 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "053a8c8bcc71fcce321828dc897a98ab9760bef03a4fc36693c231e5b3216cfe" @@ -1514,10 +1587,12 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum sourcefile 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)" = "4bf77cb82ba8453b42b6ae1d692e4cdc92f9a47beaf89a847c8be83f4e328ad3" "checksum strsim 0.9.3 (registry+https://github.com/rust-lang/crates.io-index)" = "6446ced80d6c486436db5c078dde11a9f73d42b57fb273121e160b84f63d894c" "checksum syn 1.0.16 (registry+https://github.com/rust-lang/crates.io-index)" = "123bd9499cfb380418d509322d7a6d52e5315f064fe4b3ad18a53d6b92c07859" +"checksum syn-mid 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)" = "7be3539f6c128a931cf19dcee741c1af532c7fd387baa739c03dd2e96479338a" "checksum tar 0.4.26 (registry+https://github.com/rust-lang/crates.io-index)" = "b3196bfbffbba3e57481b6ea32249fbaf590396a52505a2615adbb79d9d826d3" "checksum tempdir 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)" = "15f2b5fb00ccdf689e0149d1b1b3c03fead81c2b37735d812fa8bddbbf41b6d8" "checksum tempfile 3.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "7a6e24d9338a0a5be79593e2fa15a648add6138caa803e2d5bc782c371732ca9" "checksum termcolor 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "bb6bfa289a4d7c5766392812c0a1f4c1ba45afa1ad47803c11e1f407d846d75f" +"checksum textwrap 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)" = "d326610f408c7a4eb6f51c37c330e496b08506c9457c9d34287ecc38809fb060" "checksum thread_local 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = "d40c6d1b69745a6ec6fb1ca717914848da4b44ae29d9b3080cbee91d72a69b14" "checksum time 0.1.42 (registry+https://github.com/rust-lang/crates.io-index)" = "db8dcfca086c1143c9270ac42a2bbd8a7ee477b78ac8e45b19abfb0cbede4b6f" "checksum tokio 0.2.12 (registry+https://github.com/rust-lang/crates.io-index)" = "b34bee1facdc352fba10c9c58b654e6ecb6a2250167772bf86071f7c5f2f5061" @@ -1531,10 +1606,12 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum unicode-bidi 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)" = "49f2bd0c6468a8230e1db229cff8029217cf623c767ea5d60bfbd42729ea54d5" "checksum unicode-normalization 0.1.12 (registry+https://github.com/rust-lang/crates.io-index)" = "5479532badd04e128284890390c1e876ef7a993d0570b3597ae43dfa1d59afa4" "checksum unicode-segmentation 1.6.0 (registry+https://github.com/rust-lang/crates.io-index)" = "e83e153d1053cbb5a118eeff7fd5be06ed99153f00dbcd8ae310c5fb2b22edc0" +"checksum unicode-width 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)" = "caaa9d531767d1ff2150b9332433f32a24622147e5ebb1f26409d5da67afd479" "checksum unicode-xid 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "826e7639553986605ec5979c7dd957c7895e93eabed50ab2ffa7f6128a75097c" "checksum url 2.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "829d4a8476c35c9bf0bbce5a3b23f4106f79728039b726d292bb93bc106787cb" "checksum utime 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "055058552ca15c566082fc61da433ae678f78986a6f16957e33162d1b218792a" "checksum vcpkg 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)" = "3fc439f2794e98976c88a2a2dafce96b930fe8010b0a256b3c2199a773933168" +"checksum vec_map 0.8.1 (registry+https://github.com/rust-lang/crates.io-index)" = "05c78687fb1a80548ae3250346c3db86a80a7cdd77bda190189f2d0a0987c81a" "checksum version_check 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "914b1a6776c4c929a602fafd8bc742e06365d4bcbe48c30f9cca5824f70dc9dd" "checksum version_check 0.9.1 (registry+https://github.com/rust-lang/crates.io-index)" = "078775d0255232fb988e6fccf26ddc9d1ac274299aaedcedce21c6f72cc533ce" "checksum walkdir 2.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "777182bc735b6424e1a57516d35ed72cb8019d85c8c9bf536dccb3445c1a2f7d" diff --git a/Cargo.toml b/Cargo.toml index 5af42ff3..caaed51f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -17,7 +17,7 @@ path = "src/main.rs" [dependencies] ansi_term = "0.12.0" app_dirs = "1.2.1" -docopt = "1" +clap = { git = "https://github.com/clap-rs/clap/" } env_logger = { version = "0.7", optional = true } flate2 = "1" log = "0.4" diff --git a/bash_tealdeer b/bash_tealdeer index ba28806d..bc096ec4 100644 --- a/bash_tealdeer +++ b/bash_tealdeer @@ -14,7 +14,7 @@ _tealdeer() return ;; -o|--os) - COMPREPLY=( $(compgen -W 'linux osx sunos windows' -- "${cur}") ) + COMPREPLY=( $(compgen -W 'linux osx macos sunos windows' -- "${cur}") ) return ;; esac diff --git a/fish_tealdeer b/fish_tealdeer index 880b0152..9365b117 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 other' +complete -c tldr -s o -l os -d 'Override the operating system.' -xa 'linux osx macos 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 1689ec31..9583ef36 100644 --- a/src/cache.rs +++ b/src/cache.rs @@ -136,7 +136,6 @@ impl Cache { OsType::OsX => Some("osx"), OsType::SunOs => None, // TODO: Does Rust support SunOS? OsType::Windows => Some("windows"), - OsType::Other => None, } } diff --git a/src/main.rs b/src/main.rs index af48547c..99375e2a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -25,10 +25,9 @@ use std::time::Duration; use ansi_term::Color; use app_dirs::AppInfo; -use docopt::Docopt; +use clap::Clap; #[cfg(not(target_os = "windows"))] use pager::Pager; -use serde_derive::Deserialize; mod cache; mod config; @@ -49,62 +48,60 @@ const APP_INFO: AppInfo = AppInfo { name: NAME, author: NAME, }; -const VERSION: &str = env!("CARGO_PKG_VERSION"); -const USAGE: &str = " -Usage: +const ARCHIVE_URL: &str = "https://github.com/tldr-pages/tldr/archive/master.tar.gz"; +const MAX_CACHE_AGE: Duration = Duration::from_secs(2_592_000); // 30 days +#[cfg(not(target_os = "windows"))] +const PAGER_COMMAND: &str = "less -R"; - tldr [options] ... - tldr [options] +#[derive(Clap, Debug)] +#[clap(about = "A fast TLDR client", author, version)] +struct Args { + /// The command to show (e.g. `tar` or `git log`) + command: Vec, -Options: + /// List all commands in the cache + #[clap(short = "l", long = "list")] + list: bool, - -h --help Show this screen - -v --version Show version information - -l --list List all commands in the cache - -f --render Render a specific markdown file - -o --os Override the operating system [linux, osx, sunos, windows] - -u --update Update the local cache - -c --clear-cache Clear the local cache - -p --pager Use a pager to page output - -m --markdown Display the raw markdown instead of rendering it - -q --quiet Suppress informational messages - --config-path Show config file path - --seed-config Create a basic config + /// Render a specific markdown file + #[clap(short = "f", long = "render")] + render: Option, -Examples: + /// Override the operating system [linux, macos, sunos, windows] + #[clap(short = "o", long = "os")] + os: Option, - $ tldr tar - $ tldr --list + /// Update the local cache + #[clap(short = "u", long = "update")] + update: bool, -To control the cache: + /// Clear the local cache + #[clap(short = "c", long = "clear-cache")] + clear_cache: bool, - $ tldr --update - $ tldr --clear-cache + /// Use a pager to page output + #[clap(short = "p", long = "pager")] + pager: bool, -To render a local file (for testing): + /// Suppress informational messages + #[clap(short = "q", long = "quiet")] + quiet: bool, - $ tldr --render /path/to/file.md -"; -const ARCHIVE_URL: &str = "https://github.com/tldr-pages/tldr/archive/master.tar.gz"; -const MAX_CACHE_AGE: Duration = Duration::from_secs(2_592_000); // 30 days -#[cfg(not(target_os = "windows"))] -const PAGER_COMMAND: &str = "less -R"; + /// Display the raw markdown instead of rendering it + #[clap(short = "m", long = "markdown")] + markdown: bool, -#[derive(Debug, Deserialize)] -struct Args { - arg_command: Option>, - flag_help: bool, - flag_version: bool, - flag_list: bool, - flag_render: Option, - flag_os: Option, - flag_update: bool, - flag_clear_cache: bool, - flag_pager: bool, - flag_quiet: bool, - flag_config_path: bool, - flag_seed_config: bool, - flag_markdown: bool, + /// Show config file path + #[clap(long = "config-path")] + config_path: bool, + + /// Create a basic config + #[clap(long = "seed-config")] + seed_config: bool, + + /// Prints the version + #[clap(short = "v", long = "version")] + version: bool, } /// Print page by path @@ -144,7 +141,7 @@ fn print_page(path: &Path, enable_markdown: bool, enable_styles: bool) -> Result #[cfg(not(target_os = "windows"))] fn configure_pager(args: &Args, enable_styles: bool) { // Flags have precedence - if args.flag_pager { + if args.pager { Pager::with_default_pager(PAGER_COMMAND).setup(); return; } @@ -174,10 +171,10 @@ fn configure_pager(_args: &Args, _enable_styles: bool) { /// Check the cache for freshness fn check_cache(args: &Args) { - if !args.flag_update { + if !args.update { match Cache::last_update() { Some(ago) if ago > MAX_CACHE_AGE => { - if args.flag_quiet { + if args.quiet { return; } println!( @@ -309,16 +306,7 @@ fn main() { init_log(); // Parse arguments - let args: Args = Docopt::new(USAGE) - .and_then(|d| d.deserialize()) - .unwrap_or_else(|e| e.exit()); - - // Show version and exit - if args.flag_version { - let os = get_os(); - println!("{} v{} ({})", NAME, VERSION, os); - process::exit(0); - } + let args: Args = Args::parse(); // Determine the usage of styles #[cfg(target_os = "windows")] @@ -330,7 +318,7 @@ fn main() { configure_pager(&args, enable_styles); // Specify target OS - let os: OsType = match args.flag_os { + let os: OsType = match args.os { Some(os) => os, None => get_os(), }; @@ -339,29 +327,29 @@ fn main() { let cache = Cache::new(ARCHIVE_URL, os); // Clear cache, pass through - if args.flag_clear_cache { - clear_cache(args.flag_quiet); + if args.clear_cache { + clear_cache(args.quiet); } // Update cache, pass through - if args.flag_update { - update_cache(&cache, args.flag_quiet); + if args.update { + update_cache(&cache, args.quiet); } // Show config file and path, pass through - if args.flag_config_path { + if args.config_path { show_config_path(); } // Create a basic config and exit - if args.flag_seed_config { + if args.seed_config { create_config_and_exit(); } // Render local file and exit - if let Some(ref file) = args.flag_render { + if let Some(ref file) = args.render { let path = PathBuf::from(file); - if let Err(msg) = print_page(&path, args.flag_markdown, enable_styles) { + if let Err(msg) = print_page(&path, args.markdown, enable_styles) { eprintln!("{}", msg); process::exit(1); } else { @@ -370,7 +358,7 @@ fn main() { } // List cached commands and exit - if args.flag_list { + if args.list { // Check cache for freshness check_cache(&args); @@ -390,21 +378,22 @@ fn main() { } // Show command from cache - if let Some(ref command) = args.arg_command { - let command = command.join("-"); + if !args.command.is_empty() { + let command = args.command.join("-"); + // Check cache for freshness check_cache(&args); // Search for command in cache if let Some(path) = cache.find_page(&command) { - if let Err(msg) = print_page(&path, args.flag_markdown, enable_styles) { + if let Err(msg) = print_page(&path, args.markdown, enable_styles) { eprintln!("{}", msg); process::exit(1); } else { process::exit(0); } } else { - if !args.flag_quiet { + if !args.quiet { println!("Page {} not found in cache", &command); println!("Try updating with `tldr --update`, or submit a pull request to:"); println!("https://github.com/tldr-pages/tldr"); @@ -414,31 +403,9 @@ fn main() { } // Some flags can be run without a command. - if !(args.flag_update || args.flag_clear_cache || args.flag_config_path) { - eprintln!("{}", USAGE); + if !(args.update || args.clear_cache || args.config_path) { + //args.clap().print_help(); + println!("TODO: Print usage"); process::exit(1); } } - -#[cfg(test)] -mod test { - use docopt::{Docopt, Error}; - use crate::{Args, OsType, USAGE}; - - fn test_helper(argv: &[&str]) -> Result { - Docopt::new(USAGE).and_then(|d| d.argv(argv.iter()).deserialize()) - } - - #[test] - fn test_docopt_os_case_insensitive() { - let argv = vec!["cp", "--os", "LiNuX"]; - let os = test_helper(&argv).unwrap().flag_os.unwrap(); - assert_eq!(OsType::Linux, os); - } - - #[test] - fn test_docopt_expect_error() { - let argv = vec!["cp", "--os", "lindows"]; - assert!(!test_helper(&argv).is_ok()); - } -} diff --git a/src/types.rs b/src/types.rs index 380b7706..c9e43a6c 100644 --- a/src/types.rs +++ b/src/types.rs @@ -1,6 +1,7 @@ //! Types used in the client. use std::fmt; +use std::str; use serde_derive::{Deserialize, Serialize}; @@ -12,7 +13,6 @@ pub enum OsType { OsX, SunOs, Windows, - Other, } impl fmt::Display for OsType { @@ -22,7 +22,23 @@ impl fmt::Display for OsType { Self::OsX => write!(f, "macOS / BSD"), Self::SunOs => write!(f, "SunOS"), Self::Windows => write!(f, "Windows"), - Self::Other => write!(f, "Unknown OS"), + } + } +} + +impl str::FromStr for OsType { + type Err = String; + + fn from_str(s: &str) -> Result { + match s { + "linux" => Ok(Self::Linux), + "osx" | "macos" => Ok(Self::OsX), + "sunos" => Ok(Self::SunOs), + "windows" => Ok(Self::Windows), + other => Err(format!( + "Unknown OS: {}. Possible values: linux, macos, osx, sunos, windows", + other + )), } } } @@ -54,11 +70,7 @@ impl<'a> From<&'a str> for LineType { .trim_start_matches(|chr: char| chr == '>' || chr.is_whitespace()) .into(), ), - Some(' ') => Self::ExampleCode( - trimmed - .trim_start_matches(char::is_whitespace) - .into(), - ), + Some(' ') => Self::ExampleCode(trimmed.trim_start_matches(char::is_whitespace).into()), _ => Self::ExampleText(trimmed.into()), } } diff --git a/zsh_tealdeer b/zsh_tealdeer index 53f2609a..69d8a7f0 100644 --- a/zsh_tealdeer +++ b/zsh_tealdeer @@ -14,9 +14,10 @@ _tealdeer() { "($I -f --render)"{-f,--render}"[Render a specific markdown file]:file:_files" "($I -o --os)"{-o,--os}'[Override the operating system]:os:(( linux + macos osx - sunos - windows + sunos + windows ))' "($I -u --update)"{-u,--update}"[Update the local cache]" "($I -c --clear-cache)"{-c,--clear-cache}"[Clear the local cache]"