typos_git_commit/
cli.rs

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