typos_git_commit/
cli.rs

1use clap::{crate_name, CommandFactory, Parser, ValueEnum};
2use clap_complete::{generate, Shell};
3use fluent_i18n::t;
4use std::io;
5use std::process::exit;
6
7// displayed when --contributors is invoked
8static CONTRIBUTORS: &str = include_str!("../Contributors");
9
10// displayed when --changelog is invoked
11static CHANGELOG: &str = include_str!("../ChangeLog");
12
13#[derive(Parser, Debug)]
14/// This program analyzes a json file produced with `typos` and makes commits
15/// for each correction.
16#[command(author, version, about, long_about = None)]
17#[command(propagate_version = true)]
18#[allow(clippy::struct_excessive_bools)]
19pub struct Cli {
20    /// typos file filename to analyze
21    #[arg(long, default_value = "")]
22    pub filename: String,
23
24    /// minimum length of the typo correction to be considered as correctable automatically.
25    #[arg(long, default_value_t = 6)]
26    pub minlen: usize,
27
28    /// Prints what it will do without doing it
29    #[arg(long, default_value_t = false)]
30    pub noop: bool,
31
32    /// Lists typos found in a brief way (does not modify anything)
33    #[arg(long)]
34    pub only_list_typos: Option<TypoType>,
35
36    /// Details output of --only-list-typos option
37    #[arg(long, default_value_t = false)]
38    pub details: bool,
39
40    /// Message to be used in commits. It may use '{typo}' and '{correction}'.
41    #[arg(long, default_value = "Corrects typo '{typo}' into '{correction}'")]
42    pub message: String,
43
44    /// Excludes file from being included in corrections.
45    #[arg(long)]
46    pub exclude_file: Option<Vec<String>>,
47
48    /// Excludes typo from being corrected.
49    #[arg(long)]
50    pub exclude_typo: Option<Vec<String>>,
51
52    /// Excludes a correction from being correctable.
53    #[arg(long)]
54    pub exclude_correction: Option<Vec<String>>,
55
56    /// Debug options that prints information to know what is going on
57    #[arg(long, default_value_t = false)]
58    pub debug: bool,
59
60    /// prints typos-git-commit contributor's list
61    #[arg(long, default_value_t = false)]
62    pub contributors: bool,
63
64    /// prints typos-git-commit changelog
65    #[arg(long, default_value_t = false)]
66    pub changelog: bool,
67
68    /// prints typos-git-commit bash completion's shell script
69    #[arg(long, default_value = None)]
70    pub completion: Option<Shell>,
71}
72
73#[derive(ValueEnum, Debug, Clone)]
74pub enum TypoType {
75    All,
76    Corrected,
77    NotCorrected,
78}
79
80impl Cli {
81    pub fn analyze() -> Self {
82        /* Parsing Cli to either print contributors, changelog, shell's completion
83         * scripts or be sure that --filename option has been filed.
84         */
85        let cli = Cli::parse();
86
87        if cli.contributors {
88            print!("{CONTRIBUTORS}");
89            exit(0);
90        }
91
92        if cli.changelog {
93            print!("{CHANGELOG}");
94            exit(0);
95        }
96
97        if let Some(shell) = cli.completion {
98            let mut cmd = Cli::command();
99            generate(shell, &mut cmd, crate_name!(), &mut io::stdout());
100            exit(0);
101        }
102
103        if cli.filename.is_empty() {
104            let mut cmd = Cli::command();
105            match cmd.print_help() {
106                Ok(()) => eprintln!("\n{}", t!("cli-error-command")),
107                Err(e) => eprintln!("{}", t!("cli-error-help", {"e" => e.to_string()})),
108            }
109            exit(1);
110        }
111
112        cli
113    }
114}