summaryrefslogtreecommitdiff
path: root/src/main.zig
diff options
context:
space:
mode:
authorRobby Zambito <contact@robbyzambito.me>2025-04-06 11:41:07 -0400
committerRobby Zambito <contact@robbyzambito.me>2025-04-06 13:08:09 -0400
commitdcb962593def256704038c7292c2df55953747b2 (patch)
treea9bb386950ed47a2bb98092836457691cf03da52 /src/main.zig
parentc2f8c77c52deac1ce035991e83ad278cc954557a (diff)
Allow caller to specify what kind of message to send with arg
Diffstat (limited to 'src/main.zig')
-rw-r--r--src/main.zig61
1 files changed, 40 insertions, 21 deletions
diff --git a/src/main.zig b/src/main.zig
index 9a4aea7..a371bb5 100644
--- a/src/main.zig
+++ b/src/main.zig
@@ -15,35 +15,52 @@ pub fn main() !void {
const gpa = if (is_debug) dba.allocator() else std.heap.smp_allocator;
- const args = try std.process.argsAlloc(gpa);
- defer std.process.argsFree(gpa, args);
+ // First we specify what parameters our program can take.
+ // We can use `parseParamsComptime` to parse a string into an array of `Param(Help)`.
+ const params = comptime clap.parseParamsComptime(
+ \\-h, --help Display this help and exit.
+ \\-r, --relay <str> A relay message to send.
+ \\-c, --connect <str> A connection message to send.
+ \\
+ );
- var message = ArrayList(u8).init(gpa);
- defer message.deinit();
-
- for (args[1..], 0..) |arg, i| {
- try message.appendSlice(arg);
- if (i < args.len - 1)
- try message.append(' ');
- }
+ // Initialize our diagnostics, which can be used for reporting useful errors.
+ // This is optional. You can also pass `.{}` to `clap.parse` if you don't
+ // care about the extra information `Diagnostics` provides.
+ var diag = clap.Diagnostic{};
+ var res = clap.parse(clap.Help, &params, clap.parsers.default, .{
+ .diagnostic = &diag,
+ .allocator = gpa,
+ }) catch |err| {
+ // Report useful error and exit.
+ diag.report(std.io.getStdErr().writer(), err) catch {};
+ return err;
+ };
+ defer res.deinit();
try Saprus.init();
defer Saprus.deinit();
- // _ = try Saprus.sendInitialConnection(if (message.items.len > 0) message.items else "Hello darkness my old friend", 6868, gpa);
+ if (res.args.help != 0) {
+ return clap.help(std.io.getStdErr().writer(), clap.Help, &params, .{});
+ }
- const res: ?SaprusMessage = Saprus.connect(if (message.items.len > 0) message.items else "Hello darkness my old friend", gpa) catch |err| switch (err) {
- error.WouldBlock => null,
- else => return err,
- };
- defer if (res) |r| r.deinit(gpa);
- if (res) |r| {
- std.debug.print("{s}\n", .{r.connection.payload});
- } else {
- std.debug.print("No response from connection request\n", .{});
+ if (res.args.relay) |r| {
+ try Saprus.sendRelay(if (r.len > 0) r else "Hello darkness my old friend", gpa);
+ } else if (res.args.connect) |c| {
+ const conn_res: ?SaprusMessage = Saprus.connect(if (c.len > 0) c else "Hello darkness my old friend", gpa) catch |err| switch (err) {
+ error.WouldBlock => null,
+ else => return err,
+ };
+ defer if (conn_res) |r| r.deinit(gpa);
+ if (conn_res) |r| {
+ std.debug.print("{s}\n", .{r.connection.payload});
+ } else {
+ std.debug.print("No response from connection request\n", .{});
+ }
}
- // try Saprus.sendRelay(if (message.items.len > 0) message.items else "Hello darkness my old friend", gpa);
+ return clap.help(std.io.getStdErr().writer(), clap.Help, &params, .{});
}
const builtin = @import("builtin");
@@ -53,3 +70,5 @@ const ArrayList = std.ArrayList;
const Saprus = @import("./saprus.zig");
const SaprusMessage = Saprus.SaprusMessage;
+
+const clap = @import("clap");