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 project_name: Option<String> = None;
12    let mut template_type: Option<TemplateType> = None;
13    let mut model_sub_type: Option<ModelSubType> = None;
14    let mut component_name: Option<String> = None;
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            "watch" if (command == CommandType::Help || command == CommandType::Version) => {
26                command = CommandType::Watch;
27            }
28            "new" if (command == CommandType::Help || command == CommandType::Version) => {
29                command = CommandType::New;
30                i += 1;
31                if i < raw_args.len()
32                    && !raw_args[i].starts_with("--")
33                    && !raw_args[i].starts_with("-")
34                {
35                    project_name = Some(raw_args[i].clone());
36                } else {
37                    i -= 1;
38                }
39            }
40            "template" if (command == CommandType::Help || command == CommandType::Version) => {
41                command = CommandType::Template;
42                i += 1;
43                if i < raw_args.len()
44                    && !raw_args[i].starts_with("--")
45                    && !raw_args[i].starts_with("-")
46                {
47                    let type_str: &str = &raw_args[i];
48                    template_type = TemplateType::from_str(type_str).ok();
49                    i += 1;
50                    if template_type == Some(TemplateType::Model)
51                        && i < raw_args.len()
52                        && !raw_args[i].starts_with("--")
53                        && !raw_args[i].starts_with("-")
54                    {
55                        let sub_type_str: &str = &raw_args[i];
56                        model_sub_type = ModelSubType::from_str(sub_type_str).ok();
57                        i += 1;
58                    }
59                    if i < raw_args.len()
60                        && !raw_args[i].starts_with("--")
61                        && !raw_args[i].starts_with("-")
62                    {
63                        component_name = Some(raw_args[i].clone());
64                        i += 1;
65                    }
66                    i -= 1;
67                }
68            }
69            _ => {}
70        }
71        i += 1;
72    }
73    Args {
74        command,
75        project_name,
76        template_type,
77        model_sub_type,
78        component_name,
79    }
80}