From 3beed4ad489856a323848e8ac2f3ca96930df3ee Mon Sep 17 00:00:00 2001 From: Marcin Puc <5671049+tranzystorek-io@users.noreply.github.com> Date: Sun, 24 Oct 2021 23:17:43 +0200 Subject: [PATCH] Simplify clap derive definition (#221) - Use `possible_values` - Change `render` field type to `PathBuf` - Add comment about explicit field names --- docs/src/usage.txt | 5 +++-- src/main.rs | 25 ++++++++++++++++++------- 2 files changed, 21 insertions(+), 9 deletions(-) diff --git a/docs/src/usage.txt b/docs/src/usage.txt index 7e13562..e677621 100644 --- a/docs/src/usage.txt +++ b/docs/src/usage.txt @@ -13,7 +13,8 @@ ARGS: OPTIONS: -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] + -o, --os Override the operating system [possible values: linux, osx, sunos, + windows] -L, --language Override the language -u, --update Update the local cache -c, --clear-cache Clear the local cache @@ -23,7 +24,7 @@ OPTIONS: --show-paths Show file and directory paths used by tealdeer --config-path Show config file path --seed-config Create a basic config - --color Control whether to use color [always, auto, never] + --color Control whether to use color [possible values: always, auto, never] -v, --version Print the version -h, --help Print help information diff --git a/src/main.rs b/src/main.rs index 7b74e64..0895fd7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -51,6 +51,8 @@ const ARCHIVE_URL: &str = "https://tldr.sh/assets/tldr.zip"; #[cfg(not(target_os = "windows"))] const PAGER_COMMAND: &str = "less -R"; +// Note: flag names are specified explicitly in clap attributes +// to improve readability and allow contributors to grep names like "clear-cache" #[derive(Parser, Debug)] #[clap(about = "A fast TLDR client", author, version)] #[clap(setting = AppSettings::ArgRequiredElseHelp)] @@ -75,10 +77,15 @@ struct Args { value_name = "FILE", conflicts_with = "command" )] - render: Option, + render: Option, - /// Override the operating system [linux, osx, sunos, windows] - #[clap(short = 'o', long = "os", requires = "command")] + /// Override the operating system + #[clap( + short = 'o', + long = "os", + requires = "command", + possible_values = ["linux", "osx", "sunos", "windows"] + )] os: Option, /// Override the language @@ -121,8 +128,12 @@ struct Args { #[clap(long = "seed-config")] seed_config: bool, - /// Control whether to use color [always, auto, never] - #[clap(long = "color", value_name = "WHEN")] + /// Control whether to use color + #[clap( + long = "color", + value_name = "WHEN", + possible_values = ["always", "auto", "never"] + )] color: Option, /// Print the version @@ -406,8 +417,8 @@ fn main() { }; // If a local file was passed in, render it and exit - if let Some(ref file) = args.render { - let path = PageLookupResult::with_page(PathBuf::from(file)); + if let Some(file) = args.render { + let path = PageLookupResult::with_page(file); if let Err(msg) = print_page(&path, args.raw, &config) { eprintln!("{}", msg); process::exit(1);