about summary refs log tree commit diff
diff options
context:
space:
mode:
authorIurii Plugatariov <[email protected]>2024-07-03 02:00:41 +0200
committerGitHub <[email protected]>2024-07-03 02:00:41 +0200
commit0ead1a692716d6b7f610d752ccbfaf808bb95e6c (patch)
treefcd1dc2ef5610244fe40dbb273b417c46f343d3b
parent2c2e3be090ff5217c9a776e5c9a816ebdff83539 (diff)
parent78495c88705f9d01b342a6ea8218c050fc67d877 (diff)
downloadtinkerbunk-0ead1a692716d6b7f610d752ccbfaf808bb95e6c.tar.gz
Merge pull request #1 from makefunstuff/arg-parse
Better arg parse
Diffstat (limited to '')
-rw-r--r--src/brr.zig0
-rw-r--r--src/main.zig63
2 files changed, 51 insertions, 12 deletions
diff --git a/src/brr.zig b/src/brr.zig
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/src/brr.zig
diff --git a/src/main.zig b/src/main.zig
index d712153..750dce0 100644
--- a/src/main.zig
+++ b/src/main.zig
@@ -3,6 +3,38 @@ const ls = @import("ls.zig");
 const socket_server = @import("socket-server.zig");
 const sdl_window = @import("sdl-window.zig");
 
+const commands = [_]struct {
+    name: []const u8,
+    command: Command,
+}{
+    .{ .name = "ls", .command = Command.LS },
+    .{ .name = "tcp", .command = Command.TCP },
+    .{ .name = "window", .command = Command.WINDOW },
+    .{ .name = "brr", .command = Command.BRR },
+};
+
+const Command = enum {
+    LS,
+    TCP,
+    WINDOW,
+    BRR,
+};
+
+const Arg = struct {
+    const Self = @This();
+
+    name: []const u8,
+    command: Command = undefined,
+
+    fn parse(self: *Self) !void {
+        inline for (commands) |cmd| {
+            if (std.mem.eql(u8, cmd.name, self.name)) {
+                self.command = cmd.command;
+            }
+        }
+    }
+};
+
 pub fn main() !void {
     var gpa = std.heap.GeneralPurposeAllocator(.{}){};
     const allocator = gpa.allocator();
@@ -11,27 +43,34 @@ pub fn main() !void {
     const args = try std.process.argsAlloc(allocator);
     defer std.process.argsFree(allocator, args);
 
-    if (args.len == 2 and std.mem.eql(u8, args[1], "help")) {
-        print_help();
+    if (args.len == 1) {
+        std.debug.print("No command provided. Use 'help' for usage information.\n", .{});
         return;
     }
-    if (args.len == 2 and std.mem.eql(u8, args[1], "ls")) {
-        try ls.ls();
-        return;
-    }
-
-    if (args.len == 2 and std.mem.eql(u8, args[1], "tcp-foo")) {
-        try socket_server.start_server();
-    }
 
-    if (args.len == 2 and std.mem.eql(u8, args[1], "window")) {
-        try sdl_window.present_sdl_window();
+    if (args.len == 2) {
+        var argument = Arg{
+            .name = args[1],
+        };
+        try argument.parse();
+        switch (argument.command) {
+            .LS => try ls.ls(),
+            .TCP => try socket_server.start_server(),
+            .WINDOW => try sdl_window.present_sdl_window(),
+            else => {
+                return;
+            },
+        }
     }
 
     std.debug.print("Unknown command. Use 'help' for usage information.\n", .{});
     print_help();
 }
 
+fn arg_is(arg: []const u8, target: []const u8) bool {
+    return std.mem.eql(u8, arg, target);
+}
+
 fn print_help() void {
     std.debug.print("[usage] tinkerbunk ls\n", .{});
 }