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
//! Command-line argument parser.

use crate::app::banner::BANNERS;
use crate::app::selection::Selection;
use crate::app::style::Style;
use crate::widget::style::Color;
use structopt::clap::AppSettings;
use structopt::StructOpt;

/// Argument parser powered by [`structopt`].
#[derive(Debug, Default, StructOpt)]
#[structopt(
    name = env!("CARGO_PKG_NAME"),
    version = env!("CARGO_PKG_VERSION"),
    author = env!("CARGO_PKG_AUTHORS"),
    about = env!("CARGO_PKG_DESCRIPTION"),
	before_help = BANNERS[2],
    global_settings(&[
        AppSettings::ColorAuto,
        AppSettings::ColoredHelp,
        AppSettings::DeriveDisplayOrder,
    ]),
	rename_all_env = "screaming-snake",
)]
pub struct Args {
	/// Enables ASCII armored output.
	#[structopt(short, long)]
	pub armor: bool,
	/// Shows the splash screen on startup.
	#[structopt(long)]
	pub splash: bool,
	/// Sets the configuration file.
	#[structopt(long, value_name = "path", env = "GPG_TUI_CONFIG", parse(from_str = Args::parse_dir))]
	pub config: Option<String>,
	/// Sets the GnuPG home directory.
	#[structopt(long, value_name = "dir", env = "GNUPGHOME", parse(from_str = Args::parse_dir))]
	pub homedir: Option<String>,
	/// Sets the output directory.
	#[structopt(short, long, value_name = "dir", env, parse(from_str = Args::parse_dir))]
	pub outdir: Option<String>,
	/// Sets the default key to sign with.
	#[structopt(short, long, value_name = "key", env)]
	pub default_key: Option<String>,
	/// Sets the tick rate of the terminal.
	#[structopt(short, long, value_name = "ms", default_value = "250", env)]
	pub tick_rate: u64,
	/// Sets the accent color of the terminal.
	#[structopt(short, long, default_value = "gray", parse(from_str), env)]
	pub color: Color,
	/// Sets the style of the terminal.
	#[structopt(
		short, long, possible_values = &["plain", "colored"], default_value = "plain", env
	)]
	pub style: Style,
	/// Sets the utility for file selection.
	#[structopt(short, long, value_name = "app", default_value = "xplr", env)]
	pub file_explorer: String,
	/// Enables the selection mode.
	#[structopt(
		long,
		value_name = "option",
		possible_values = &["key_id", "key_fpr", "user_id", "row1", "row2"],
		env
	)]
	pub select: Option<Selection>,
}

impl Args {
	/// Custom string parser for directories.
	///
	/// Expands the tilde (`~`) character in the beginning of the
	/// input string into contents of the path returned by [`home_dir`].
	///
	/// [`home_dir`]: dirs_next::home_dir
	fn parse_dir(dir: &str) -> String {
		shellexpand::tilde(dir).to_string()
	}

	/// Parses the command-line arguments.
	///
	/// See [`StructOpt::from_args`].
	pub fn parse() -> Self {
		Self::from_args()
	}
}