Skip to main content

gpg_tui/
args.rs

1//! Command-line argument parser.
2
3use crate::app::banner::BANNERS;
4use crate::app::selection::Selection;
5use crate::app::style::Style;
6use crate::gpg::key::KeyDetail;
7use crate::widget::style::Color;
8use clap::Parser;
9
10/// Argument parser powered by [`clap`].
11#[derive(Debug, Default, Parser)]
12#[clap(
13    version,
14    author = clap::crate_authors!("\n"),
15    about,
16	rename_all_env = "screaming-snake",
17	before_help = format!("\u{2800} {}", BANNERS[2]),
18	help_template = "\
19{before-help}{name} {version}
20{author-with-newline}{about-with-newline}
21{usage-heading}
22  {usage}
23
24{all-args}{after-help}
25",
26)]
27pub struct Args {
28	/// Enables ASCII armored output.
29	#[clap(short, long)]
30	pub armor: bool,
31	/// Shows the splash screen on startup.
32	#[clap(long)]
33	pub splash: bool,
34	/// Sets the configuration file.
35	#[clap(
36		long,
37		value_name = "path",
38		env = "GPG_TUI_CONFIG",
39		value_parser = Args::parse_dir
40	)]
41	pub config: Option<String>,
42	/// Sets the GnuPG home directory.
43	#[clap(
44		long,
45		value_name = "dir",
46		env = "GNUPGHOME",
47		value_parser = Args::parse_dir
48	)]
49	pub homedir: Option<String>,
50	/// Sets the output directory.
51	#[clap(
52		short,
53		long,
54		value_name = "dir",
55		env,
56		value_parser = Args::parse_dir
57	)]
58	pub outdir: Option<String>,
59	/// Sets the template for the output file name.
60	#[clap(
61		long,
62		value_name = "path",
63		default_value = "{type}_{query}.{ext}",
64		env,
65		value_parser = Args::parse_dir
66	)]
67	pub outfile: String,
68	/// Sets the default key to sign with.
69	#[clap(short, long, value_name = "key", env)]
70	pub default_key: Option<String>,
71	/// Sets the tick rate of the terminal.
72	#[clap(short, long, value_name = "ms", default_value = "250", env)]
73	pub tick_rate: u64,
74	/// Sets the accent color of the terminal.
75	#[clap(short, long, value_name = "color", default_value = "gray", env)]
76	pub color: Color,
77	/// Sets the style of the terminal.
78	#[clap(short, long, value_name = "style", default_value = "colored", env)]
79	pub style: Style,
80	/// Sets the utility for file selection.
81	#[clap(short, long, value_name = "app", default_value = "xplr", env)]
82	pub file_explorer: String,
83	/// Sets the detail level for the keys.
84	#[clap(long, value_name = "level", default_value = "minimum", env)]
85	pub detail_level: KeyDetail,
86	/// Sets the file to save the logs.
87	#[clap(long, value_name = "path", env)]
88	pub log_file: Option<String>,
89	/// Enables the selection mode.
90	#[clap(long, value_name = "option", env)]
91	pub select: Option<Selection>,
92}
93
94impl Args {
95	/// Custom string parser for directories.
96	///
97	/// Expands the tilde (`~`) character in the beginning of the
98	/// input string into contents of the path returned by [`home_dir`].
99	///
100	/// [`home_dir`]: dirs_next::home_dir
101	fn parse_dir(dir: &str) -> Result<String, String> {
102		Ok(shellexpand::tilde(dir).to_string())
103	}
104}
105
106#[cfg(test)]
107mod tests {
108	use super::*;
109	use clap::CommandFactory;
110	use std::path::PathBuf;
111	#[test]
112	fn test_args() {
113		Args::command().debug_assert();
114	}
115	#[test]
116	fn test_tilde_expansion() {
117		let home_dir =
118			dirs_next::home_dir().expect("cannot retrieve home directory");
119		let dir = Args::parse_dir("~/").expect("cannot expand tilde");
120		assert_eq!(home_dir, PathBuf::from(dir));
121	}
122}