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            "sync" if (command == CommandType::Help || command == CommandType::Version) => {
42                command = CommandType::Sync;
43            }
44            "new" if (command == CommandType::Help || command == CommandType::Version) => {
45                command = CommandType::New;
46                i += 1;
47                if i < raw_args.len()
48                    && !raw_args[i].starts_with("--")
49                    && !raw_args[i].starts_with("-")
50                {
51                    project_name = Some(raw_args[i].clone());
52                } else {
53                    i -= 1;
54                }
55            }
56            "template" if (command == CommandType::Help || command == CommandType::Version) => {
57                command = CommandType::Template;
58                i += 1;
59                if i < raw_args.len()
60                    && !raw_args[i].starts_with("--")
61                    && !raw_args[i].starts_with("-")
62                {
63                    let type_str: &str = &raw_args[i];
64                    template_type = TemplateType::from_str(type_str).ok();
65                    i += 1;
66                    if template_type == Some(TemplateType::Model)
67                        && i < raw_args.len()
68                        && !raw_args[i].starts_with("--")
69                        && !raw_args[i].starts_with("-")
70                    {
71                        let sub_type_str: &str = &raw_args[i];
72                        model_sub_type = ModelSubType::from_str(sub_type_str).ok();
73                        i += 1;
74                    }
75                    if i < raw_args.len()
76                        && !raw_args[i].starts_with("--")
77                        && !raw_args[i].starts_with("-")
78                    {
79                        component_name = Some(raw_args[i].clone());
80                        i += 1;
81                    }
82                    i -= 1;
83                }
84            }
85            "--patch" => {
86                bump_type = Some(BumpVersionType::Patch);
87            }
88            "--minor" => {
89                bump_type = Some(BumpVersionType::Minor);
90            }
91            "--major" => {
92                bump_type = Some(BumpVersionType::Major);
93            }
94            "--release" => {
95                bump_type = Some(BumpVersionType::Release);
96            }
97            "--alpha" => {
98                bump_type = Some(BumpVersionType::Alpha);
99            }
100            "--beta" => {
101                bump_type = Some(BumpVersionType::Beta);
102            }
103            "--rc" => {
104                bump_type = Some(BumpVersionType::Rc);
105            }
106            "--check" => {
107                check = true;
108            }
109            "--manifest-path" => {
110                i += 1;
111                if i < raw_args.len() {
112                    manifest_path = Some(raw_args[i].clone());
113                }
114            }
115            "--max-retries" => {
116                i += 1;
117                if i < raw_args.len()
118                    && let Ok(n) = raw_args[i].parse::<u32>()
119                {
120                    max_retries = n;
121                }
122            }
123            _ => {}
124        }
125        i += 1;
126    }
127    Args {
128        command,
129        check,
130        manifest_path,
131        bump_type,
132        max_retries,
133        project_name,
134        template_type,
135        model_sub_type,
136        component_name,
137    }
138}