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,
|
.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
|
// This declares intent for the executable to be installed into the
|
||||||
// standard location when the user invokes the "install" step (the default
|
// standard location when the user invokes the "install" step (the default
|
||||||
// step when running `zig build`).
|
// step when running `zig build`).
|
||||||
|
|||||||
@@ -40,6 +40,10 @@
|
|||||||
.url = "https://github.com/ikskuh/zig-network/archive/c76240d2240711a3dcbf1c0fb461d5d1f18be79a.zip",
|
.url = "https://github.com/ikskuh/zig-network/archive/c76240d2240711a3dcbf1c0fb461d5d1f18be79a.zip",
|
||||||
.hash = "network-0.1.0-AAAAAOwlAQAQ6zKPUrsibdpGisxld9ftUKGdMvcCSpaj",
|
.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 = .{
|
.paths = .{
|
||||||
"build.zig",
|
"build.zig",
|
||||||
|
|||||||
63
src/main.zig
63
src/main.zig
@@ -15,35 +15,52 @@ pub fn main() !void {
|
|||||||
|
|
||||||
const gpa = if (is_debug) dba.allocator() else std.heap.smp_allocator;
|
const gpa = if (is_debug) dba.allocator() else std.heap.smp_allocator;
|
||||||
|
|
||||||
const args = try std.process.argsAlloc(gpa);
|
// First we specify what parameters our program can take.
|
||||||
defer std.process.argsFree(gpa, args);
|
// 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);
|
// Initialize our diagnostics, which can be used for reporting useful errors.
|
||||||
defer message.deinit();
|
// This is optional. You can also pass `.{}` to `clap.parse` if you don't
|
||||||
|
// care about the extra information `Diagnostics` provides.
|
||||||
for (args[1..], 0..) |arg, i| {
|
var diag = clap.Diagnostic{};
|
||||||
try message.appendSlice(arg);
|
var res = clap.parse(clap.Help, ¶ms, clap.parsers.default, .{
|
||||||
if (i < args.len - 1)
|
.diagnostic = &diag,
|
||||||
try message.append(' ');
|
.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();
|
try Saprus.init();
|
||||||
defer Saprus.deinit();
|
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) {
|
|
||||||
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", .{});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// try Saprus.sendRelay(if (message.items.len > 0) message.items else "Hello darkness my old friend", gpa);
|
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", .{});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return clap.help(std.io.getStdErr().writer(), clap.Help, ¶ms, .{});
|
||||||
}
|
}
|
||||||
|
|
||||||
const builtin = @import("builtin");
|
const builtin = @import("builtin");
|
||||||
@@ -53,3 +70,5 @@ const ArrayList = std.ArrayList;
|
|||||||
|
|
||||||
const Saprus = @import("./saprus.zig");
|
const Saprus = @import("./saprus.zig");
|
||||||
const SaprusMessage = Saprus.SaprusMessage;
|
const SaprusMessage = Saprus.SaprusMessage;
|
||||||
|
|
||||||
|
const clap = @import("clap");
|
||||||
|
|||||||
Reference in New Issue
Block a user