This repository has been archived by the owner on Mar 31, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 64
/
args.rs
107 lines (95 loc) · 3.43 KB
/
args.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
// Copyright 2018-2020 Sebastian Wiesner <sebastian@swsnr.de>
// This Source Code Form 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/.
use clap::ValueHint;
fn after_help() -> &'static str {
"See 'man 1 mdcat' for more information.
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,
/// Paginate the output of mdcat with a pager like less.
#[arg(short, long, overrides_with = "no_pager")]
paginate: bool,
/// Do not paginate output (default). Overrides an earlier --paginate.
#[arg(short = 'P', long)]
no_pager: bool,
},
#[command(version, about, after_help = after_help(), long_version = long_version())]
Mdless {
#[command(flatten)]
args: CommonArgs,
/// Do not paginate output.
#[arg(short = 'P', long, overrides_with = "paginate")]
no_pager: bool,
/// Paginate the output of mdcat with a pager like less (default). Overrides an earlier --no-pager.
#[arg(short, long)]
paginate: bool,
},
}
impl Command {
#[allow(dead_code)]
pub fn paginate(&self) -> bool {
match *self {
// In both cases look at the option indicating the non-default
// behaviour; the overrides above are configured accordingly.
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)]
// #[command(author, version, about, after_help = after_help(), long_version = long_version())]
pub struct CommonArgs {
/// Files to read. If - read from standard input instead.
#[arg(default_value="-", value_hint = ValueHint::FilePath)]
pub filenames: Vec<String>,
/// Disable all colours and other styles.
#[arg(short = 'c', long, aliases=["nocolour", "no-color", "nocolor"])]
pub no_colour: bool,
/// Maximum number of columns to use for output.
#[arg(long)]
pub columns: Option<usize>,
/// Do not load remote resources like images.
#[arg(short, long = "local")]
pub local_only: bool,
/// Exit immediately if any error occurs processing an input file.
#[arg(long = "fail")]
pub fail_fast: bool,
/// Print detected terminal name and exit.
#[arg(long = "detect-terminal")]
pub detect_and_exit: bool,
/// Skip terminal detection and only use ANSI formatting.
#[arg(long = "ansi", conflicts_with = "no_colour")]
pub ansi_only: bool,
}