hyperlane_cli/config/
fn.rs1use super::*;
2
3pub fn parse_args() -> Args {
9 let raw_args: Vec<String> = args().collect();
10 parse_args_from(&raw_args)
11}
12
13pub fn parse_args_from(raw_args: &[String]) -> Args {
23 let mut command: CommandType = CommandType::Help;
24 let mut project_name: Option<String> = None;
25 let mut template_type: Option<TemplateType> = None;
26 let mut model_sub_type: Option<ModelSubType> = None;
27 let mut component_name: Option<String> = None;
28 let mut fmt_check: bool = false;
29 let mut fmt_path: Option<PathBuf> = None;
30 let mut i: usize = 1;
31 while i < raw_args.len() {
32 let arg: &str = raw_args[i].as_str();
33 let default_cmd: bool = matches!(command, CommandType::Help | CommandType::Version);
34 match arg {
35 "-h" | FLAG_LONG_HELP => {
36 command = CommandType::Help;
37 }
38 "-v" | FLAG_LONG_VERSION => {
39 command = CommandType::Version;
40 }
41 COMMAND_WATCH if default_cmd => {
42 command = CommandType::Watch;
43 }
44 COMMAND_FMT if default_cmd => {
45 command = CommandType::Fmt;
46 }
47 FLAG_FMT_CHECK if command == CommandType::Fmt => {
48 fmt_check = true;
49 }
50 FLAG_FMT_PATH if command == CommandType::Fmt => {
51 i += 1;
52 if i < raw_args.len() {
53 fmt_path = Some(PathBuf::from(&raw_args[i]));
54 } else {
55 i -= 1;
56 }
57 }
58 "new" if default_cmd => {
59 command = CommandType::New;
60 i += 1;
61 if i < raw_args.len()
62 && !raw_args[i].starts_with("--")
63 && !raw_args[i].starts_with("-")
64 {
65 project_name = Some(raw_args[i].clone());
66 } else {
67 i -= 1;
68 }
69 }
70 COMMAND_TEMPLATE if default_cmd => {
71 command = CommandType::Template;
72 i += 1;
73 if i < raw_args.len()
74 && !raw_args[i].starts_with("--")
75 && !raw_args[i].starts_with("-")
76 {
77 let type_str: &str = &raw_args[i];
78 template_type = TemplateType::from_str(type_str).ok();
79 i += 1;
80 if template_type == Some(TemplateType::Model)
81 && i < raw_args.len()
82 && !raw_args[i].starts_with("--")
83 && !raw_args[i].starts_with("-")
84 {
85 let sub_type_str: &str = &raw_args[i];
86 model_sub_type = ModelSubType::from_str(sub_type_str).ok();
87 i += 1;
88 }
89 if i < raw_args.len()
90 && !raw_args[i].starts_with("--")
91 && !raw_args[i].starts_with("-")
92 {
93 component_name = Some(raw_args[i].clone());
94 i += 1;
95 }
96 i -= 1;
97 }
98 }
99 other
100 if command == CommandType::Fmt && fmt_path.is_none() && !other.starts_with('-') =>
101 {
102 fmt_path = Some(PathBuf::from(other));
103 }
104 _ => {}
105 }
106 i += 1;
107 }
108 Args {
109 command,
110 project_name,
111 template_type,
112 model_sub_type,
113 component_name,
114 fmt_check,
115 fmt_path,
116 }
117}