summaryrefslogtreecommitdiff
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
parentc2f8c77c52deac1ce035991e83ad278cc954557a (diff)
Allow caller to specify what kind of message to send with arg
-rw-r--r--build.zig3
-rw-r--r--build.zig.zon4
-rw-r--r--src/main.zig61
3 files changed, 47 insertions, 21 deletions
diff --git a/build.zig b/build.zig
index 328b822..f57a948 100644
--- a/build.zig
+++ b/build.zig
@@ -35,6 +35,9 @@ pub fn build(b: *std.Build) void {
.root_module = exe_mod,
});
+ const clap = b.dependency("clap", .{});
+ exe.root_module.addImport("clap", clap.module("clap"));
+
// This declares intent for the executable to be installed into the
// standard location when the user invokes the "install" step (the default
// step when running `zig build`).
diff --git a/build.zig.zon b/build.zig.zon
index da40722..f9d27ef 100644
--- a/build.zig.zon
+++ b/build.zig.zon
@@ -40,6 +40,10 @@
.url = "https://github.com/ikskuh/zig-network/archive/c76240d2240711a3dcbf1c0fb461d5d1f18be79a.zip",
.hash = "network-0.1.0-AAAAAOwlAQAQ6zKPUrsibdpGisxld9ftUKGdMvcCSpaj",
},
+ .clap = .{
+ .url = "git+https://github.com/Hejsil/zig-clap?ref=0.10.0#e47028deaefc2fb396d3d9e9f7bd776ae0b2a43a",
+ .hash = "clap-0.10.0-oBajB434AQBDh-Ei3YtoKIRxZacVPF1iSwp3IX_ZB8f0",
+ },
},
.paths = .{
"build.zig",
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");