@@ -5,11 +5,73 @@ const std = @import("std");
5
5
6
6
const Tokenizer = @import ("Tokenizer.zig" );
7
7
const Parser = @import ("Parser.zig" );
8
+ const args = @import ("args" );
9
+
10
+ const Options = struct {
11
+ output : ? []const u8 = null ,
12
+ size : ? u32 = null ,
13
+ script : ? []const u8 = null ,
14
+ @"import-env" : bool = false ,
15
+ };
16
+
17
+ const usage =
18
+ \\dim OPTIONS [VARS]
19
+ \\
20
+ \\OPTIONS:
21
+ \\ --output <path>
22
+ \\ mandatory: where to store the output file
23
+ \\[--size <size>]
24
+ \\ optional: how big is the resulting disk image? allowed suffixes: k,K,M,G
25
+ \\ --script <path>
26
+ \\ mandatory: which script file to execute?
27
+ \\[--import-env]
28
+ \\ optional: if set, imports the current process environment into the variables
29
+ \\VARS:
30
+ \\{ KEY=VALUE }*
31
+ \\ multiple ≥ 0: Sets variable KEY to VALUE
32
+ \\
33
+ ;
8
34
9
35
pub fn main () ! void {
36
+ var gpa_impl : std .heap .DebugAllocator (.{}) = .init ;
37
+ defer _ = gpa_impl .deinit ();
38
+
39
+ const gpa = gpa_impl .allocator ();
40
+
41
+ const opts = try args .parseForCurrentProcess (Options , gpa , .print );
42
+ defer opts .deinit ();
43
+
44
+ var var_map : std .StringArrayHashMapUnmanaged ([]const u8 ) = .empty ;
45
+ defer var_map .deinit (gpa );
10
46
11
- //
47
+ for (opts .positionals ) | pos | {
48
+ if (std .mem .indexOfScalar (u8 , pos , '=' )) | idx | {
49
+ const key = pos [0.. idx ];
50
+ const val = pos [idx + 1 .. ];
51
+ try var_map .put (gpa , key , val );
52
+ }
53
+ }
54
+
55
+ const options = opts .options ;
56
+
57
+ if (options .output == null ) {
58
+ fatal ("No output path specified" );
59
+ }
60
+
61
+ if (options .script == null ) {
62
+ fatal ("No script specified" );
63
+ }
64
+
65
+ std .debug .print (
66
+ "Output={?s} Script={?s} Size={?} import-env={}\n " ,
67
+ .{ options .output , options .script , options .size , options .@"import-env" },
68
+ );
69
+ }
12
70
71
+ fn fatal (msg : []const u8 ) noreturn {
72
+ std .debug .print ("Error: {s}\n " , .{msg });
73
+ std .debug .print ("Usage: {s}" , .{usage });
74
+ std .process .exit (1 );
13
75
}
14
76
15
77
test {
0 commit comments