typos_git_commit/
cli.rs

1use clap::{crate_name, CommandFactory, Parser};
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, default_value_t = false)]
33    pub only_list_typos: bool,
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
72impl Cli {
73    pub fn analyze() -> Self {
74        /* Parsing Cli to either print contributors, changelog, shell's completion
75         * scripts or be sure that --filename option has been filed.
76         */
77        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}