Skip to main content

hyperlane_cli/config/
fn.rs

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