Skip to content

Commit

Permalink
Update to clap v4
Browse files Browse the repository at this point in the history
  • Loading branch information
dbrgn committed Feb 11, 2024
1 parent 2e731d7 commit 110dfd7
Show file tree
Hide file tree
Showing 6 changed files with 122 additions and 112 deletions.
56 changes: 29 additions & 27 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ path = "src/main.rs"
anyhow = "1"
app_dirs = { version = "2", package = "app_dirs2" }
atty = "0.2"
clap = { version = "3", features = ["std", "derive", "suggestions", "color"], default-features = false }
clap = { version = "4", features = ["std", "derive", "help", "usage", "cargo", "error-context", "color", "wrap_help"], default-features = false }
env_logger = { version = "0.10", optional = true }
log = "0.4"
reqwest = { version = "0.11.3", features = ["blocking"], default-features = false }
Expand Down
46 changes: 21 additions & 25 deletions docs/src/usage.txt
Original file line number Diff line number Diff line change
@@ -1,30 +1,26 @@
tealdeer 1.6.1
Danilo Bargen <mail@dbrgn.ch>, Niklas Mohrin <dev@niklasmohrin.de>
A fast TLDR client
tealdeer 1.2.3: A fast TLDR client

USAGE:
tldr [OPTIONS] [COMMAND]...
Usage: tldr [OPTIONS] [COMMAND]...

ARGS:
<COMMAND>... The command to show (e.g. `tar` or `git log`)
Arguments:
[COMMAND]... The command to show (e.g. `tar` or `git log`)

OPTIONS:
-l, --list List all commands in the cache
-f, --render <FILE> Render a specific markdown file
-p, --platform <PLATFORMS> Override the operating system [possible values: linux, macos,
windows, sunos, osx, android]
-L, --language <LANGUAGE> Override the language
-u, --update Update the local cache
--no-auto-update If auto update is configured, disable it for this run
-c, --clear-cache Clear the local cache
--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
--seed-config Create a basic config
--color <WHEN> Control whether to use color [possible values: always, auto,
never]
-v, --version Print the version
-h, --help Print help information
Options:
-l, --list List all commands in the cache
-f, --render <FILE> Render a specific markdown file
-p, --platform <PLATFORMS> Override the operating system [possible values: linux, macos, windows,
sunos, android]
-L, --language <LANGUAGE> Override the language
-u, --update Update the local cache
--no-auto-update If auto update is configured, disable it for this run
-c, --clear-cache Clear the local cache
--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
--seed-config Create a basic config
--color <WHEN> Control whether to use color [possible values: always, auto, never]
-v, --version Print the version
-h, --help Print help

To view the user documentation, please visit https://dbrgn.github.io/tealdeer/.
65 changes: 38 additions & 27 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,42 @@
use std::path::PathBuf;

use clap::{AppSettings, ArgAction, ArgGroup, Parser};
use clap::{
arg,
builder::{ArgAction, EnumValueParser},
command, ArgGroup, Parser,
};

use crate::types::{ColorOptions, PlatformType};

// 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(
after_help = "To view the user documentation, please visit https://dbrgn.github.io/tealdeer/."
#[command(
about = "A fast TLDR client",
version = "1.2.3",
disable_version_flag = true,
author,
help_template = "{before-help}{name} {version}: {about-with-newline}
{usage-heading} {usage}
{all-args}{after-help}",
after_help = "To view the user documentation, please visit https://dbrgn.github.io/tealdeer/.",
arg_required_else_help = true,
help_expected = true,
group = ArgGroup::new("command_or_file").args(&["command", "render"]),
)]
#[clap(setting = AppSettings::DeriveDisplayOrder)]
#[clap(arg_required_else_help(true))]
#[clap(disable_colored_help(true))]
#[clap(group = ArgGroup::new("command_or_file").args(&["command", "render"]))]
pub(crate) struct Args {
pub(crate) struct Cli {
/// The command to show (e.g. `tar` or `git log`)
#[clap(min_values = 1)]
#[arg(num_args(1..))]
pub command: Vec<String>,

/// List all commands in the cache
#[clap(short = 'l', long = "list")]
#[arg(short = 'l', long = "list")]
pub list: bool,

/// Render a specific markdown file
#[clap(
#[arg(
short = 'f',
long = "render",
value_name = "FILE",
Expand All @@ -36,61 +46,62 @@ pub(crate) struct Args {
pub render: Option<PathBuf>,

/// Override the operating system
#[clap(
#[arg(
short = 'p',
long = "platform",
action = ArgAction::Append,
possible_values = ["linux", "macos", "windows", "sunos", "osx", "android"],
value_parser = EnumValueParser::<PlatformType>::new(),
)]
pub platforms: Option<Vec<PlatformType>>,

/// Override the language
#[clap(short = 'L', long = "language")]
#[arg(short = 'L', long = "language")]
pub language: Option<String>,

/// Update the local cache
#[clap(short = 'u', long = "update")]
#[arg(short = 'u', long = "update")]
pub update: bool,

/// If auto update is configured, disable it for this run
#[clap(long = "no-auto-update", requires = "command_or_file")]
#[arg(long = "no-auto-update", requires = "command_or_file")]
pub no_auto_update: bool,

/// Clear the local cache
#[clap(short = 'c', long = "clear-cache")]
#[arg(short = 'c', long = "clear-cache")]
pub clear_cache: bool,

/// Use a pager to page output
#[clap(long = "pager", requires = "command_or_file")]
#[arg(long = "pager", requires = "command_or_file")]
pub pager: bool,

/// Display the raw markdown instead of rendering it
#[clap(short = 'r', long = "--raw", requires = "command_or_file")]
#[arg(short = 'r', long = "raw", requires = "command_or_file")]
pub raw: bool,

/// Suppress informational messages
#[clap(short = 'q', long = "quiet")]
#[arg(short = 'q', long = "quiet")]
pub quiet: bool,

/// Show file and directory paths used by tealdeer
#[clap(long = "show-paths")]
#[arg(long = "show-paths")]
pub show_paths: bool,

/// Create a basic config
#[clap(long = "seed-config")]
#[arg(long = "seed-config")]
pub seed_config: bool,

/// Control whether to use color
#[clap(
#[arg(
long = "color",
value_name = "WHEN",
possible_values = ["always", "auto", "never"]
value_parser = EnumValueParser::<ColorOptions>::new(),
)]
//possible_values = ["always", "auto", "never"]
pub color: Option<ColorOptions>,

/// Print the version
// Note: We override the version flag because clap uses `-V` by default,
// while TLDR specification requires `-v` to be used.
#[clap(short = 'v', long = "version")]
pub version: bool,
#[arg(short = 'v', long = "version", action = ArgAction::Version)]
pub version: Option<bool>,
}
8 changes: 4 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ mod utils;

use crate::{
cache::{Cache, CacheFreshness, PageLookupResult, TLDR_PAGES_DIR},
cli::Args,
cli::Cli,
config::{get_config_dir, get_config_path, make_default_config, Config, PathWithSource},
extensions::Dedup,
output::print_page,
Expand All @@ -65,7 +65,7 @@ const ARCHIVE_URL: &str = "https://tldr.sh/assets/tldr.zip";

/// The cache should be updated if it was explicitly requested,
/// or if an automatic update is due and allowed.
fn should_update_cache(cache: &Cache, args: &Args, config: &Config) -> bool {
fn should_update_cache(cache: &Cache, args: &Cli, config: &Config) -> bool {
args.update
|| (!args.no_auto_update
&& config.updates.auto_update
Expand All @@ -81,7 +81,7 @@ enum CheckCacheResult {
}

/// Check the cache for freshness. If it's stale or missing, show a warning.
fn check_cache(cache: &Cache, args: &Args, enable_styles: bool) -> CheckCacheResult {
fn check_cache(cache: &Cache, args: &Cli, enable_styles: bool) -> CheckCacheResult {
match cache.freshness() {
CacheFreshness::Fresh => CheckCacheResult::CacheFound,
CacheFreshness::Stale(_) if args.quiet => CheckCacheResult::CacheFound,
Expand Down Expand Up @@ -243,7 +243,7 @@ fn main() {
init_log();

// Parse arguments
let args = Args::parse();
let args = Cli::parse();

// Determine the usage of styles
#[cfg(target_os = "windows")]
Expand Down
Loading

0 comments on commit 110dfd7

Please sign in to comment.