use clap::ValueHint;
use clap_complete::Shell;
fn after_help() -> &'static str {
"See 'man 1 mdcat' for more information.
mdcat can be installed as or linked to mdless,
for automatic pagination.
Report issues to <https://github.com/swsnr/mdcat>."
}
fn long_version() -> &'static str {
concat!(
env!("CARGO_PKG_VERSION"),
"
Copyright (C) Sebastian Wiesner and contributors
This program is subject to the terms of the Mozilla Public License,
v. 2.0. If a copy of the MPL was not distributed with this file,
You can obtain one at http://mozilla.org/MPL/2.0/."
)
}
#[derive(Debug, clap::Parser)]
#[command(multicall = true)]
pub struct Args {
#[command(subcommand)]
pub command: Command,
}
#[derive(Debug, clap::Subcommand)]
pub enum Command {
#[command(version, about, after_help = after_help(), long_version = long_version())]
Mdcat {
#[command(flatten)]
args: CommonArgs,
#[arg(short, long, overrides_with = "no_pager")]
paginate: bool,
#[arg(short = 'P', long)]
no_pager: bool,
},
#[command(version, about, after_help = after_help(), long_version = long_version())]
Mdless {
#[command(flatten)]
args: CommonArgs,
#[arg(short = 'P', long, overrides_with = "paginate")]
no_pager: bool,
#[arg(short, long)]
paginate: bool,
},
}
impl Command {
pub fn paginate(&self) -> bool {
match *self {
Command::Mdcat { paginate, .. } => paginate,
Command::Mdless { no_pager, .. } => !no_pager,
}
}
}
impl std::ops::Deref for Command {
type Target = CommonArgs;
fn deref(&self) -> &Self::Target {
match self {
Command::Mdcat { args, .. } => args,
Command::Mdless { args, .. } => args,
}
}
}
#[derive(Debug, clap::Args)]
pub struct CommonArgs {
#[arg(default_value="-", value_hint = ValueHint::FilePath)]
pub filenames: Vec<String>,
#[arg(short = 'c', long, aliases=["nocolour", "no-color", "nocolor"])]
pub no_colour: bool,
#[arg(long)]
pub columns: Option<u16>,
#[arg(short, long = "local")]
pub local_only: bool,
#[arg(long = "fail")]
pub fail_fast: bool,
#[arg(long = "detect-terminal")]
pub detect_and_exit: bool,
#[arg(long = "ansi", conflicts_with = "no_colour")]
pub ansi_only: bool,
#[arg(long)]
pub completions: Option<Shell>,
}
#[derive(Debug, Copy, Clone)]
pub enum ResourceAccess {
LocalOnly,
Remote,
}
impl CommonArgs {
pub fn resource_access(&self) -> ResourceAccess {
if self.local_only {
ResourceAccess::LocalOnly
} else {
ResourceAccess::Remote
}
}
}
#[cfg(test)]
mod tests {
use super::Args;
use clap::CommandFactory;
#[test]
fn verify_app() {
Args::command().debug_assert();
}
}