From 227d41bbf89ae6401fdbbe536fc67d3bb908ee64 Mon Sep 17 00:00:00 2001 From: makefunstuff Date: Wed, 3 Jul 2024 02:26:15 +0300 Subject: wip simple arg parse --- src/brr.zig | 0 src/main.zig | 41 ++++++++++++++++++++++++++++------------- 2 files changed, 28 insertions(+), 13 deletions(-) create mode 100644 src/brr.zig (limited to 'src') diff --git a/src/brr.zig b/src/brr.zig new file mode 100644 index 0000000..e69de29 diff --git a/src/main.zig b/src/main.zig index d712153..522eb43 100644 --- a/src/main.zig +++ b/src/main.zig @@ -3,6 +3,27 @@ const ls = @import("ls.zig"); const socket_server = @import("socket-server.zig"); const sdl_window = @import("sdl-window.zig"); +const commands = [_]u8{ "ls", "tcp", "window", "brr" }; +const Command = enum { + LS, + TCP, + WINDOW, + BRR, +}; + +const Arg = struct { + const Self = @This(); + + name: []const u8, + command: Command, + + fn parse(self: *Self) void { + inline for (commands) |command| { + if (std.mem.eql([]u8, command, self.name)) {} + } + } +}; + pub fn main() !void { var gpa = std.heap.GeneralPurposeAllocator(.{}){}; const allocator = gpa.allocator(); @@ -11,27 +32,21 @@ 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(); - return; - } - if (args.len == 2 and std.mem.eql(u8, args[1], "ls")) { - try ls.ls(); + 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], "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) {} 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", .{}); } -- cgit 1.4.1-2-gfad0 From 389197eef38182b3610a881399d9d04c6cf59f72 Mon Sep 17 00:00:00 2001 From: makefunstuff Date: Wed, 3 Jul 2024 02:46:54 +0300 Subject: kinda works --- src/main.zig | 36 +++++++++++++++++++++++++++++++----- 1 file changed, 31 insertions(+), 5 deletions(-) (limited to 'src') diff --git a/src/main.zig b/src/main.zig index 522eb43..653dd3b 100644 --- a/src/main.zig +++ b/src/main.zig @@ -3,7 +3,16 @@ const ls = @import("ls.zig"); const socket_server = @import("socket-server.zig"); const sdl_window = @import("sdl-window.zig"); -const commands = [_]u8{ "ls", "tcp", "window", "brr" }; +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, @@ -15,11 +24,15 @@ const Arg = struct { const Self = @This(); name: []const u8, - command: Command, + command: Command = undefined, - fn parse(self: *Self) void { + fn parse(self: *Self) !void { inline for (commands) |command| { - if (std.mem.eql([]u8, command, self.name)) {} + if (std.mem.eql(u8, command.name, self.name)) { + self.command = command.command; + } else { + return error.CommandNotFound; + } } } }; @@ -37,7 +50,20 @@ pub fn main() !void { return; } - if (args.len == 2) {} + 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(); -- cgit 1.4.1-2-gfad0 From 78495c88705f9d01b342a6ea8218c050fc67d877 Mon Sep 17 00:00:00 2001 From: makefunstuff Date: Wed, 3 Jul 2024 02:59:38 +0300 Subject: fixed --- src/main.zig | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) (limited to 'src') diff --git a/src/main.zig b/src/main.zig index 653dd3b..750dce0 100644 --- a/src/main.zig +++ b/src/main.zig @@ -27,11 +27,9 @@ const Arg = struct { command: Command = undefined, fn parse(self: *Self) !void { - inline for (commands) |command| { - if (std.mem.eql(u8, command.name, self.name)) { - self.command = command.command; - } else { - return error.CommandNotFound; + inline for (commands) |cmd| { + if (std.mem.eql(u8, cmd.name, self.name)) { + self.command = cmd.command; } } } -- cgit 1.4.1-2-gfad0