1use clap::{crate_name, CommandFactory, Parser};
2use clap_complete::{generate, Shell};
3use std::io;
4use std::process::exit;
5
6static CONTRIBUTORS: &str = include_str!("../Contributors");
8
9static CHANGELOG: &str = include_str!("../ChangeLog");
11
12#[derive(Parser, Debug)]
13#[command(author, version, about, long_about = None)]
16#[command(propagate_version = true)]
17pub struct Cli {
18 #[arg(long, default_value = "")]
20 pub filename: String,
21
22 #[arg(long, default_value_t = 6)]
24 pub minlen: usize,
25
26 #[arg(long, default_value_t = false)]
28 pub noop: bool,
29
30 #[arg(long, default_value_t = false)]
32 pub only_list_typos: bool,
33
34 #[arg(long, default_value_t = false)]
36 pub details: bool,
37
38 #[arg(long, default_value = "Corrects typo '{typo}' into '{correction}'")]
40 pub message: String,
41
42 #[arg(long)]
44 pub exclude_file: Option<Vec<String>>,
45
46 #[arg(long)]
48 pub exclude_typo: Option<Vec<String>>,
49
50 #[arg(long)]
52 pub exclude_correction: Option<Vec<String>>,
53
54 #[arg(long, default_value_t = false)]
56 pub debug: bool,
57
58 #[arg(long, default_value_t = false)]
60 pub contributors: bool,
61
62 #[arg(long, default_value_t = false)]
64 pub changelog: bool,
65
66 #[arg(long, default_value = None)]
68 pub completion: Option<Shell>,
69}
70
71impl Cli {
72 pub fn analyze() -> Self {
73 let cli = Cli::parse();
77
78 if cli.contributors {
79 print!("{CONTRIBUTORS}");
80 exit(0);
81 }
82
83 if cli.changelog {
84 print!("{CHANGELOG}");
85 exit(0);
86 }
87
88 if let Some(shell) = cli.completion {
89 let mut cmd = Cli::command();
90 generate(shell, &mut cmd, crate_name!(), &mut io::stdout());
91 exit(0);
92 }
93
94 if cli.filename.is_empty() {
95 let mut cmd = Cli::command();
96 match cmd.print_help() {
97 Ok(_) => eprintln!("\nerror: one of these --filename, --contributors, --changelog, --completion must be used."),
98 Err(e) => eprintln!("error while printing help: {e}"),
99 }
100 exit(1);
101 }
102
103 cli
104 }
105}