Skip to content

Commit 2abd9bf

Browse files
First CLI args usage
1 parent bf4674d commit 2abd9bf

File tree

1 file changed

+63
-1
lines changed

1 file changed

+63
-1
lines changed

src/dim.zig

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,73 @@ const std = @import("std");
55

66
const Tokenizer = @import("Tokenizer.zig");
77
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+
;
834

935
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);
1046

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+
}
1270

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);
1375
}
1476

1577
test {

0 commit comments

Comments
 (0)