1use clap::{crate_name, CommandFactory, Parser};
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, default_value_t = false)]
33 pub only_list_typos: bool,
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
72impl Cli {
73 pub fn analyze() -> Self {
74 let cli = Cli::parse();
78
79 if cli.contributors {
80 print!("{CONTRIBUTORS}");
81 exit(0);
82 }
83
84 if cli.changelog {
85 print!("{CHANGELOG}");
86 exit(0);
87 }
88
89 if let Some(shell) = cli.completion {
90 let mut cmd = Cli::command();
91 generate(shell, &mut cmd, crate_name!(), &mut io::stdout());
92 exit(0);
93 }
94
95 if cli.filename.is_empty() {
96 let mut cmd = Cli::command();
97 match cmd.print_help() {
98 Ok(_) => eprintln!("\n{}", t!("cli-error-command")),
99 Err(e) => eprintln!("{}", t!("cli-error-help", {"e" => e.to_string()})),
100 }
101 exit(1);
102 }
103
104 cli
105 }
106}