-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodule.jai
More file actions
425 lines (373 loc) · 18.5 KB
/
Copy pathmodule.jai
File metadata and controls
425 lines (373 loc) · 18.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
#import "Basic";
#import "String";
// Define a struct to hold the command-line arguments
Command :: struct {
name: string;
short_name: string;
description: string;
arguments: [..]Argument;
subcommands: [..]Command;
options: [..]Option;
global_options: [..]Option; // Options that apply to all subcommands
action: (*void) -> ();
ctx_data: *void; // Pointer to context data for the action
}
// Define a struct to hold information about an argument
Argument :: struct {
name: string;
description: string;
arg_type: Type; // Jai's type reflection can be used here
type_name: string; // Store the type name as a string for runtime comparison
required: bool;
value: *void; // Pointer to store the parsed value
}
// Define a struct to hold information about an option
Option :: struct {
name: string;
short_name: string;
description: string;
option_type: Type; // Jai's type reflection can be used here
type_name: string; // Store the type name as a string for runtime comparison
default_value: *void; // Use a void pointer for generic default values
required: bool;
value: *void; // Pointer to store the parsed value
}
// Result of parsing arguments
ParseResult :: struct {
success: bool;
error_message: string;
command: *Command;
help_shown: bool;
}
// Function to parse the command-line arguments
parse_args :: (command: *Command, args: []string = .[]) -> ParseResult {
local_args := args;
if local_args.count == 0 local_args = get_command_line_arguments();
result: ParseResult;
result.success = true;
result.command = command;
if local_args.count <= 1 {
// No arguments provided, execute the root command's action
if command.action {
command.action(command.ctx_data);
}
return result;
}
// Skip the program name (local_args[0])
current_index := 1;
current_command := command;
while current_index < local_args.count {
arg := local_args[current_index];
current_index += 1;
// Check if it's a subcommand
subcommand_found := false;
for * subcommand: current_command.subcommands {
if arg == subcommand.name || (subcommand.short_name.count > 0 && arg == subcommand.short_name) {
current_command = subcommand;
result.command = current_command;
subcommand_found = true;
// Check if the next arg is --help
if current_index < local_args.count && local_args[current_index] == "--help" {
print_help(current_command);
result.help_shown = true;
return result;
}
break;
}
}
if subcommand_found continue;
// Check if it's an option (either long or short form)
if starts_with(arg, "--") || (starts_with(arg, "-") && arg.count > 1) {
option_found := false;
// Handle long options (--option)
if starts_with(arg, "--") {
option_name := slice(arg, 2, arg.count - 2);
// Handle the help option
if option_name == "help" {
print_help(current_command);
result.help_shown = true;
return result;
}
// Check current command options
for * option: current_command.options {
if option_name == option.name {
option_found = true;
if option.type_name == "bool" {
// Boolean flag
(cast(*bool)option.value).* = true;
} else {
// Option with value
if current_index >= local_args.count {
result.success = false;
result.error_message = tprint("Option % requires a value", option.name);
return result;
}
value_str := local_args[current_index];
current_index += 1;
if option.type_name == "string" {
(cast(*string)option.value).* = value_str;
} else if option.type_name == "int" || option.type_name == "s64" {
value, success := string_to_int(value_str);
if !success {
result.success = false;
result.error_message = tprint("Failed to parse % as int", value_str);
return result;
}
if option.type_name == "int" {
(cast(*int)option.value).* = value;
} else { // s64
(cast(*s64)option.value).* = value;
}
} else if option.type_name == "float" {
value, success := string_to_float(value_str);
if !success {
result.success = false;
result.error_message = tprint("Failed to parse % as float", value_str);
return result;
}
(cast(*float)option.value).* = value;
}
}
break;
}
}
// If not found in current command options, check global options
if !option_found {
for * option: current_command.global_options {
if option_name == option.name {
option_found = true;
if option.type_name == "bool" {
// Boolean flag
(cast(*bool)option.value).* = true;
} else {
// Option with value
if current_index >= local_args.count {
result.success = false;
result.error_message = tprint("Option % requires a value", option.name);
return result;
}
value_str := local_args[current_index];
current_index += 1;
if option.type_name == "string" {
(cast(*string)option.value).* = value_str;
} else if option.type_name == "int" || option.type_name == "s64" {
value, success := string_to_int(value_str);
if !success {
result.success = false;
result.error_message = tprint("Failed to parse % as int", value_str);
return result;
}
if option.type_name == "int" {
(cast(*int)option.value).* = value;
} else { // s64
(cast(*s64)option.value).* = value;
}
} else if option.type_name == "float" {
value, success := string_to_float(value_str);
if !success {
result.success = false;
result.error_message = tprint("Failed to parse % as float", value_str);
return result;
}
(cast(*float)option.value).* = value;
}
}
break;
}
}
}
} else if starts_with(arg, "-") { // Handle short options (-o)
short_option_name := slice(arg, 1, arg.count - 1);
// Check current command options
for * option: current_command.options {
if short_option_name == option.short_name {
option_found = true;
if option.type_name == "bool" {
// Boolean flag
(cast(*bool)option.value).* = true;
} else {
// Option with value
if current_index >= local_args.count {
result.success = false;
result.error_message = tprint("Option % requires a value", option.short_name);
return result;
}
value_str := local_args[current_index];
current_index += 1;
if option.type_name == "string" {
(cast(*string)option.value).* = value_str;
} else if option.type_name == "int" || option.type_name == "s64" {
value, success := string_to_int(value_str);
if !success {
result.success = false;
result.error_message = tprint("Failed to parse % as int", value_str);
return result;
}
if option.type_name == "int" {
(cast(*int)option.value).* = value;
} else { // s64
(cast(*s64)option.value).* = value;
}
} else if option.type_name == "float" {
value, success := string_to_float(value_str);
if !success {
result.success = false;
result.error_message = tprint("Failed to parse % as float", value_str);
return result;
}
(cast(*float)option.value).* = value;
}
}
break;
}
}
// Check global options
for * option: current_command.global_options {
if short_option_name == option.short_name {
option_found = true;
if option.type_name == "bool" {
// Boolean flag
(cast(*bool)option.value).* = true;
} else {
// Option with value
if current_index >= local_args.count {
result.success = false;
result.error_message = tprint("Option % requires a value", option.short_name);
return result;
}
value_str := local_args[current_index];
current_index += 1;
if option.type_name == "string" {
(cast(*string)option.value).* = value_str;
} else if option.type_name == "int" || option.type_name == "s64" {
value, success := string_to_int(value_str);
if !success {
result.success = false;
result.error_message = tprint("Failed to parse % as int", value_str);
return result;
}
if option.type_name == "int" {
(cast(*int)option.value).* = value;
} else { // s64
(cast(*s64)option.value).* = value;
}
} else if option.type_name == "float" {
value, success := string_to_float(value_str);
if !success {
result.success = false;
result.error_message = tprint("Failed to parse % as float", value_str);
return result;
}
(cast(*float)option.value).* = value;
}
}
break;
}
}
}
// Only report an error if the option wasn't found in either current command options or global options
if !option_found {
result.success = false;
result.error_message = tprint("Unknown option: %", arg);
return result;
}
} else {
// It's a positional argument
if current_command.arguments.count > 0 {
arg_index := 0;
if arg_index < current_command.arguments.count {
argument := *current_command.arguments[arg_index];
// Parse the argument based on its type
if argument.type_name == "string" {
(cast(*string)argument.value).* = arg;
} else if argument.type_name == "s64" || argument.type_name == "int" {
value, success := string_to_int(arg);
if !success {
result.success = false;
result.error_message = tprint("Failed to parse % as int", arg);
return result;
}
// Handle both int and s64 types based on the type name
if argument.type_name == "int" {
(cast(*int)argument.value).* = value;
} else if argument.type_name == "s64" {
(cast(*s64)argument.value).* = value;
}
} else if argument.type_name == "float" {
value, success := string_to_float(arg);
if !success {
result.success = false;
result.error_message = tprint("Failed to parse % as float", arg);
return result;
}
(cast(*float)argument.value).* = value;
}
// TODO: Handle more argument types and multiple arguments
// For now, we only handle the first argument
} else {
result.success = false;
result.error_message = tprint("Too many arguments for command: %", current_command.name);
return result;
}
} else {
result.success = false;
result.error_message = tprint("Command % does not accept arguments: %", current_command.name, arg);
return result;
}
}
}
// Update the result with the final command
result.command = current_command;
// Execute the action of the final command, but only if help wasn't shown
if !result.help_shown && current_command.action {
current_command.action(current_command.ctx_data);
}
return result;
}
// Function to print help information for a command
print_help :: (command: *Command) {
print("Usage: %", command.name);
if command.options.count > 0 print(" [options]");
if command.arguments.count > 0 {
for arg: command.arguments print(" <%>", arg.name);
}
if command.subcommands.count > 0 print(" [subcommand]");
print("\n");
print("%\n", command.description);
if command.arguments.count > 0 {
print("\nArguments:\n");
for arg: command.arguments {
print(" %\t%", arg.name, arg.description);
if arg.required print(" (required)");
print("\n");
}
}
// Print local options
if command.options.count > 0 {
print("\nOptions:\n");
for opt: command.options {
print(" ");
if opt.short_name.count > 0 print("-%, ", opt.short_name);
print("--%\t%", opt.name, opt.description);
// if opt.default_value != null print(" (default: %)", opt.default_value); // How to print generic default?
print("\n");
}
}
// Print global options
if command.global_options.count > 0 {
print("\nGlobal Options:\n");
for opt: command.global_options {
print(" ");
if opt.short_name.count > 0 print("-%, ", opt.short_name);
print("--%\t%", opt.name, opt.description);
print("\n");
}
}
if command.subcommands.count > 0 {
print("\nSubcommands:\n");
for sub: command.subcommands {
print(" %\t%\n", sub.name, sub.description);
}
print("\nRun '% [subcommand] --help' for more information on a subcommand.\n", command.name);
}
}