mirror of
https://git.robbyzambito.me/zaprus
synced 2025-12-20 08:14:50 +00:00
Allow caller to specify what kind of message to send with arg
This commit is contained in:
@@ -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`).
|
||||
|
||||
@@ -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",
|
||||
|
||||
49
src/main.zig
49
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, ¶ms, 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, ¶ms, .{});
|
||||
}
|
||||
|
||||
const res: ?SaprusMessage = Saprus.connect(if (message.items.len > 0) message.items else "Hello darkness my old friend", gpa) catch |err| switch (err) {
|
||||
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 (res) |r| r.deinit(gpa);
|
||||
if (res) |r| {
|
||||
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, ¶ms, .{});
|
||||
}
|
||||
|
||||
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");
|
||||
|
||||
Reference in New Issue
Block a user