1use clap::{crate_name, CommandFactory, Parser, ValueEnum};
2use clap_complete::{generate, Shell};
3use fluent_i18n::t;
4use std::io;
5use std::process::exit;
6
7static CONTRIBUTORS: &str = include_str!("../Contributors");
9
10static CHANGELOG: &str = include_str!("../ChangeLog");
12
13#[derive(Parser, Debug)]
14#[command(author, version, about, long_about = None)]
17#[command(propagate_version = true)]
18pub struct Cli {
19 #[arg(long, default_value = "")]
21 pub filename: String,
22
23 #[arg(long, default_value_t = 6)]
25 pub minlen: usize,
26
27 #[arg(long, default_value_t = false)]
29 pub noop: bool,
30
31 #[arg(long)]
33 pub only_list_typos: Option<TypoType>,
34
35 #[arg(long, default_value_t = false)]
37 pub details: bool,
38
39 #[arg(long, default_value = "Corrects typo '{typo}' into '{correction}'")]
41 pub message: String,
42
43 #[arg(long)]
45 pub exclude_file: Option<Vec<String>>,
46
47 #[arg(long)]
49 pub exclude_typo: Option<Vec<String>>,
50
51 #[arg(long)]
53 pub exclude_correction: Option<Vec<String>>,
54
55 #[arg(long, default_value_t = false)]
57 pub debug: bool,
58
59 #[arg(long, default_value_t = false)]
61 pub contributors: bool,
62
63 #[arg(long, default_value_t = false)]
65 pub changelog: bool,
66
67 #[arg(long, default_value = None)]
69 pub completion: Option<Shell>,
70}
71
72#[derive(ValueEnum, Debug, Clone)]
73pub enum TypoType {
74 All,
75 Corrected,
76 NotCorrected,
77}
78
79impl Cli {
80 pub fn analyze() -> Self {
81 let cli = Cli::parse();
85
86 if cli.contributors {
87 print!("{CONTRIBUTORS}");
88 exit(0);
89 }
90
91 if cli.changelog {
92 print!("{CHANGELOG}");
93 exit(0);
94 }
95
96 if let Some(shell) = cli.completion {
97 let mut cmd = Cli::command();
98 generate(shell, &mut cmd, crate_name!(), &mut io::stdout());
99 exit(0);
100 }
101
102 if cli.filename.is_empty() {
103 let mut cmd = Cli::command();
104 match cmd.print_help() {
105 Ok(_) => eprintln!("\n{}", t!("cli-error-command")),
106 Err(e) => eprintln!("{}", t!("cli-error-help", {"e" => e.to_string()})),
107 }
108 exit(1);
109 }
110
111 cli
112 }
113}