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" | FLAG_LONG_HELP => {
20                command = CommandType::Help;
21            }
22            "-v" | FLAG_LONG_VERSION => {
23                command = CommandType::Version;
24            }
25            COMMAND_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            COMMAND_TEMPLATE
41                if (command == CommandType::Help || command == CommandType::Version) =>
42            {
43                command = CommandType::Template;
44                i += 1;
45                if i < raw_args.len()
46                    && !raw_args[i].starts_with("--")
47                    && !raw_args[i].starts_with("-")
48                {
49                    let type_str: &str = &raw_args[i];
50                    template_type = TemplateType::from_str(type_str).ok();
51                    i += 1;
52                    if template_type == Some(TemplateType::Model)
53                        && i < raw_args.len()
54                        && !raw_args[i].starts_with("--")
55                        && !raw_args[i].starts_with("-")
56                    {
57                        let sub_type_str: &str = &raw_args[i];
58                        model_sub_type = ModelSubType::from_str(sub_type_str).ok();
59                        i += 1;
60                    }
61                    if i < raw_args.len()
62                        && !raw_args[i].starts_with("--")
63                        && !raw_args[i].starts_with("-")
64                    {
65                        component_name = Some(raw_args[i].clone());
66                        i += 1;
67                    }
68                    i -= 1;
69                }
70            }
71            _ => {}
72        }
73        i += 1;
74    }
75    Args {
76        command,
77        project_name,
78        template_type,
79        model_sub_type,
80        component_name,
81    }
82}