Skip to content
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

Move definition of CLI arguments and options into own file/module #258

Merged
merged 1 commit into from
Jan 19, 2022
Merged
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
113 changes: 113 additions & 0 deletions src/cli.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
//! Definition of the CLI arguments and options.

use std::path::PathBuf;

use clap::{AppSettings, 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(setting = AppSettings::ArgRequiredElseHelp)]
#[clap(setting = AppSettings::DeriveDisplayOrder)]
#[clap(setting = AppSettings::DisableColoredHelp)]
#[clap(
after_help = "To view the user documentation, please visit https://dbrgn.github.io/tealdeer/."
)]
#[clap(group = ArgGroup::new("command_or_file").args(&["command", "render"]))]
pub(crate) struct Args {
/// The command to show (e.g. `tar` or `git log`)
#[clap(min_values = 1)]
pub command: Vec<String>,

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

/// Render a specific markdown file
#[clap(
short = 'f',
long = "render",
value_name = "FILE",
conflicts_with = "command"
)]
pub render: Option<PathBuf>,

/// Override the operating system
#[clap(
short = 'p',
long = "platform",
possible_values = ["linux", "macos", "windows", "sunos", "osx"],
)]
pub platform: Option<PlatformType>,

/// Deprecated alias of `platform`
#[clap(
short = 'o',
long = "os",
possible_values = ["linux", "macos", "windows", "sunos", "osx"],
hide = true
)]
pub os: Option<PlatformType>,

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

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

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

/// Use a pager to page output
#[clap(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")]
pub raw: bool,

/// Deprecated alias of `raw`
#[clap(
long = "markdown",
short = 'm',
requires = "command_or_file",
hide = true
)]
pub markdown: bool,

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

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

/// Show config file path
#[clap(long = "config-path")]
pub config_path: bool,

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

/// Control whether to use color
#[clap(
long = "color",
value_name = "WHEN",
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,
}
112 changes: 4 additions & 108 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,14 @@
#![allow(clippy::struct_excessive_bools)]
#![allow(clippy::too_many_lines)]

use std::{env, path::PathBuf, process};
use std::{env, process};

use app_dirs::AppInfo;
use atty::Stream;
use clap::{AppSettings, ArgGroup, Parser};
use clap::Parser;

mod cache;
mod cli;
mod config;
pub mod extensions;
mod formatter;
Expand All @@ -33,6 +34,7 @@ mod utils;

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

// 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)]
#[clap(setting = AppSettings::DeriveDisplayOrder)]
#[clap(setting = AppSettings::DisableColoredHelp)]
#[clap(
after_help = "To view the user documentation, please visit https://dbrgn.github.io/tealdeer/."
)]
#[clap(group = ArgGroup::new("command_or_file").args(&["command", "render"]))]
struct Args {
/// The command to show (e.g. `tar` or `git log`)
#[clap(min_values = 1)]
command: Vec<String>,

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

/// Render a specific markdown file
#[clap(
short = 'f',
long = "render",
value_name = "FILE",
conflicts_with = "command"
)]
render: Option<PathBuf>,

/// Override the operating system
#[clap(
short = 'p',
long = "platform",
possible_values = ["linux", "macos", "windows", "sunos", "osx"],
)]
platform: Option<PlatformType>,

/// Deprecated alias of `platform`
#[clap(
short = 'o',
long = "os",
possible_values = ["linux", "macos", "windows", "sunos", "osx"],
hide = true
)]
os: Option<PlatformType>,

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

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

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

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

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

/// Deprecated alias of `raw`
#[clap(
long = "markdown",
short = 'm',
requires = "command_or_file",
hide = true
)]
markdown: bool,

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

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

/// Show config file path
#[clap(long = "config-path")]
config_path: bool,

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

/// Control whether to use color
#[clap(
long = "color",
value_name = "WHEN",
possible_values = ["always", "auto", "never"]
)]
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")]
version: bool,
}

/// The cache should get updated if this was requested by the user, or if auto
/// updates are enabled and the cache age is longer than the auto update interval.
fn should_update_cache(args: &Args, config: &Config) -> bool {
Expand Down