Skip to main content

hyperlane_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 project_name: Option<String> = None;
16    let mut template_type: Option<TemplateType> = None;
17    let mut model_sub_type: Option<ModelSubType> = None;
18    let mut component_name: Option<String> = None;
19    let mut i: usize = 1;
20    while i < raw_args.len() {
21        let arg: &str = raw_args[i].as_str();
22        match arg {
23            "-h" | "--help" => {
24                command = CommandType::Help;
25            }
26            "-v" | "--version" => {
27                command = CommandType::Version;
28            }
29            "fmt" if (command == CommandType::Help || command == CommandType::Version) => {
30                command = CommandType::Fmt;
31            }
32            "watch" if (command == CommandType::Help || command == CommandType::Version) => {
33                command = CommandType::Watch;
34            }
35            "bump" if (command == CommandType::Help || command == CommandType::Version) => {
36                command = CommandType::Bump;
37            }
38            "publish" if (command == CommandType::Help || command == CommandType::Version) => {
39                command = CommandType::Publish;
40            }
41            "new" if (command == CommandType::Help || command == CommandType::Version) => {
42                command = CommandType::New;
43                i += 1;
44                if i < raw_args.len()
45                    && !raw_args[i].starts_with("--")
46                    && !raw_args[i].starts_with("-")
47                {
48                    project_name = Some(raw_args[i].clone());
49                } else {
50                    i -= 1;
51                }
52            }
53            "template" if (command == CommandType::Help || command == CommandType::Version) => {
54                command = CommandType::Template;
55                i += 1;
56                if i < raw_args.len()
57                    && !raw_args[i].starts_with("--")
58                    && !raw_args[i].starts_with("-")
59                {
60                    let type_str: &str = &raw_args[i];
61                    template_type = TemplateType::from_str(type_str).ok();
62                    i += 1;
63                    if template_type == Some(TemplateType::Model)
64                        && i < raw_args.len()
65                        && !raw_args[i].starts_with("--")
66                        && !raw_args[i].starts_with("-")
67                    {
68                        let sub_type_str: &str = &raw_args[i];
69                        model_sub_type = ModelSubType::from_str(sub_type_str).ok();
70                        i += 1;
71                    }
72                    if i < raw_args.len()
73                        && !raw_args[i].starts_with("--")
74                        && !raw_args[i].starts_with("-")
75                    {
76                        component_name = Some(raw_args[i].clone());
77                        i += 1;
78                    }
79                    i -= 1;
80                }
81            }
82            "--patch" => {
83                bump_type = Some(BumpVersionType::Patch);
84            }
85            "--minor" => {
86                bump_type = Some(BumpVersionType::Minor);
87            }
88            "--major" => {
89                bump_type = Some(BumpVersionType::Major);
90            }
91            "--release" => {
92                bump_type = Some(BumpVersionType::Release);
93            }
94            "--alpha" => {
95                bump_type = Some(BumpVersionType::Alpha);
96            }
97            "--beta" => {
98                bump_type = Some(BumpVersionType::Beta);
99            }
100            "--rc" => {
101                bump_type = Some(BumpVersionType::Rc);
102            }
103            "--check" => {
104                check = true;
105            }
106            "--manifest-path" => {
107                i += 1;
108                if i < raw_args.len() {
109                    manifest_path = Some(raw_args[i].clone());
110                }
111            }
112            "--max-retries" => {
113                i += 1;
114                if i < raw_args.len()
115                    && let Ok(n) = raw_args[i].parse::<u32>()
116                {
117                    max_retries = n;
118                }
119            }
120            _ => {}
121        }
122        i += 1;
123    }
124    Args {
125        command,
126        check,
127        manifest_path,
128        bump_type,
129        max_retries,
130        project_name,
131        template_type,
132        model_sub_type,
133        component_name,
134    }
135}