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)]
18pub struct Cli {
19    /// typos file filename to analyze
20    #[arg(long, default_value = "")]
21    pub filename: String,
22
23    /// minimum length of the typo correction to be considered as correctable automatically.
24    #[arg(long, default_value_t = 6)]
25    pub minlen: usize,
26
27    /// Prints what it will do without doing it
28    #[arg(long, default_value_t = false)]
29    pub noop: bool,
30
31    /// Lists typos found in a brief way (does not modify anything)
32    #[arg(long)]
33    pub only_list_typos: Option<TypoType>,
34
35    /// Details output of --only-list-typos option
36    #[arg(long, default_value_t = false)]
37    pub details: bool,
38
39    /// Message to be used in commits. It may use '{typo}' and '{correction}'.
40    #[arg(long, default_value = "Corrects typo '{typo}' into '{correction}'")]
41    pub message: String,
42
43    /// Excludes file from being included in corrections.
44    #[arg(long)]
45    pub exclude_file: Option<Vec<String>>,
46
47    /// Excludes typo from being corrected.
48    #[arg(long)]
49    pub exclude_typo: Option<Vec<String>>,
50
51    /// Excludes a correction from being correctable.
52    #[arg(long)]
53    pub exclude_correction: Option<Vec<String>>,
54
55    /// Debug options that prints information to know what is going on
56    #[arg(long, default_value_t = false)]
57    pub debug: bool,
58
59    /// prints typos-git-commit contributor's list
60    #[arg(long, default_value_t = false)]
61    pub contributors: bool,
62
63    /// prints typos-git-commit changelog
64    #[arg(long, default_value_t = false)]
65    pub changelog: bool,
66
67    /// prints typos-git-commit bash completion's shell script
68    #[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        /* Parsing Cli to either print contributors, changelog, shell's completion
82         * scripts or be sure that --filename option has been filed.
83         */
84        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}