Skip to main content

crate_cli/config/
fn.rs

1use super::*;
2
3/// Parse command line arguments
4///
5/// # Returns
6///
7/// - `Args`: Parsed arguments
8pub fn parse_args() -> Args {
9    let raw_args: Vec<String> = args().collect();
10    let mut command: CommandType = CommandType::Help;
11    let mut check: bool = false;
12    let mut manifest_path: Option<String> = None;
13    let mut bump_type: Option<BumpVersionType> = None;
14    let mut max_retries: u32 = 8;
15    let mut i: usize = 1;
16    while i < raw_args.len() {
17        let arg: &str = raw_args[i].as_str();
18        match arg {
19            "-h" | "--help" => {
20                command = CommandType::Help;
21            }
22            "-v" | "--version" => {
23                command = CommandType::Version;
24            }
25            "fmt" if (command == CommandType::Help || command == CommandType::Version) => {
26                command = CommandType::Fmt;
27            }
28            "bump" if (command == CommandType::Help || command == CommandType::Version) => {
29                command = CommandType::Bump;
30            }
31            "publish" if (command == CommandType::Help || command == CommandType::Version) => {
32                command = CommandType::Publish;
33            }
34            "sync" if (command == CommandType::Help || command == CommandType::Version) => {
35                command = CommandType::Sync;
36            }
37            "--patch" => {
38                bump_type = Some(BumpVersionType::Patch);
39            }
40            "--minor" => {
41                bump_type = Some(BumpVersionType::Minor);
42            }
43            "--major" => {
44                bump_type = Some(BumpVersionType::Major);
45            }
46            "--release" => {
47                bump_type = Some(BumpVersionType::Release);
48            }
49            "--alpha" => {
50                bump_type = Some(BumpVersionType::Alpha);
51            }
52            "--beta" => {
53                bump_type = Some(BumpVersionType::Beta);
54            }
55            "--rc" => {
56                bump_type = Some(BumpVersionType::Rc);
57            }
58            "--check" => {
59                check = true;
60            }
61            "--manifest-path" => {
62                i += 1;
63                if i < raw_args.len() {
64                    manifest_path = Some(raw_args[i].clone());
65                }
66            }
67            "--max-retries" => {
68                i += 1;
69                if i < raw_args.len()
70                    && let Ok(n) = raw_args[i].parse::<u32>()
71                {
72                    max_retries = n;
73                }
74            }
75            _ => {}
76        }
77        i += 1;
78    }
79    Args {
80        command,
81        check,
82        manifest_path,
83        bump_type,
84        max_retries,
85    }
86}