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 = "fix(typo): corrects '{typo}' into '{correction}'")]
42    pub message: String,
43
44    /// Corrects only this typo (use multiple times add a typo to be corrected)
45    #[arg(long)]
46    pub typo: Option<Vec<String>>,
47
48    /// Excludes file from being included in corrections (use multiple times to exclude more than one file)
49    #[arg(long)]
50    pub exclude_file: Option<Vec<String>>,
51
52    /// Excludes typo from being corrected (use multiple times to exclude more than one typo)
53    #[arg(long)]
54    pub exclude_typo: Option<Vec<String>>,
55
56    /// Excludes a correction from being correctable (use multiple times to exclude more than one correction)
57    #[arg(long)]
58    pub exclude_correction: Option<Vec<String>>,
59
60    /// Debug options that prints information to know what is going on
61    #[arg(long, default_value_t = false)]
62    pub debug: bool,
63
64    /// prints typos-git-commit contributor's list
65    #[arg(long, default_value_t = false)]
66    pub contributors: bool,
67
68    /// prints typos-git-commit changelog
69    #[arg(long, default_value_t = false)]
70    pub changelog: bool,
71
72    /// prints typos-git-commit bash completion's shell script
73    #[arg(long, default_value = None)]
74    pub completion: Option<Shell>,
75}
76
77#[derive(ValueEnum, Debug, Clone)]
78pub enum TypoType {
79    All,
80    Corrected,
81    NotCorrected,
82}
83
84impl Cli {
85    pub fn analyze() -> Self {
86        /* Parsing Cli to either print contributors, changelog, shell's completion
87         * scripts or be sure that --filename option has been filed.
88         */
89        let cli = Cli::parse();
90
91        if cli.contributors {
92            print!("{CONTRIBUTORS}");
93            exit(0);
94        }
95
96        if cli.changelog {
97            print!("{CHANGELOG}");
98            exit(0);
99        }
100
101        if let Some(shell) = cli.completion {
102            let mut cmd = Cli::command();
103            generate(shell, &mut cmd, crate_name!(), &mut io::stdout());
104            exit(0);
105        }
106
107        if cli.filename.is_empty() {
108            let mut cmd = Cli::command();
109            match cmd.print_help() {
110                Ok(()) => eprintln!("\n{}", t!("cli-error-command")),
111                Err(e) => eprintln!("{}", t!("cli-error-help", {"e" => e.to_string()})),
112            }
113            exit(1);
114        }
115
116        cli
117    }
118}