diff options
| author | Robby Zambito <contact@robbyzambito.me> | 2025-11-29 21:20:56 -0500 |
|---|---|---|
| committer | Robby Zambito <contact@robbyzambito.me> | 2025-11-29 22:46:29 -0500 |
| commit | 41f4ee721b138304294b185185dc6fc51549c5b9 (patch) | |
| tree | 8347a6c243108e22df56cef2e9cfe11643724eab /src/main.zig | |
| parent | bd9ed88e5c7e112f2f4be8234fd11dd9db82d111 (diff) | |
Switch from clap to yazap
This is compatible with the latest 0.16.0 nightly build.
It is also a bit less magic than clap.
Diffstat (limited to 'src/main.zig')
| -rw-r--r-- | src/main.zig | 107 |
1 files changed, 54 insertions, 53 deletions
diff --git a/src/main.zig b/src/main.zig index ea188fb..367cb62 100644 --- a/src/main.zig +++ b/src/main.zig @@ -1,67 +1,68 @@ const std = @import("std"); const zits = @import("zits"); -const clap = @import("clap"); +const yazap = @import("yazap"); const Message = zits.MessageParser.Message; const Server = zits.Server; -const SubCommands = enum { - help, - serve, - @"pub", -}; - -const main_parsers = .{ - .command = clap.parsers.enumeration(SubCommands), -}; - -// The parameters for `main`. Parameters for the subcommands are specified further down. -const main_params = clap.parseParamsComptime( - \\-h, --help Display this help and exit. - \\<command> - \\ -); - -// To pass around arguments returned by clap, `clap.Result` and `clap.ResultEx` can be used to -// get the return type of `clap.parse` and `clap.parseEx`. -pub const MainArgs = clap.ResultEx(clap.Help, &main_params, main_parsers); - pub fn main() !void { var dba: std.heap.DebugAllocator(.{}) = .init; defer _ = dba.deinit(); const gpa = dba.allocator(); - var iter = try std.process.ArgIterator.initWithAllocator(gpa); - defer iter.deinit(); - - _ = iter.next(); - - var diag = clap.Diagnostic{}; - var res = clap.parseEx(clap.Help, &main_params, main_parsers, &iter, .{ - .diagnostic = &diag, - .allocator = gpa, - - // Terminate the parsing of arguments after parsing the first positional (0 is passed - // here because parsed positionals are, like slices and arrays, indexed starting at 0). - // - // This will terminate the parsing after parsing the subcommand enum and leave `iter` - // not fully consumed. It can then be reused to parse the arguments for subcommands. - .terminating_positional = 0, - }) catch |err| { - try diag.reportToFile(.stderr(), err); - return err; - }; - defer res.deinit(); - std.debug.print("res: {any}\n", .{res}); - - if (res.args.help != 0) - return clap.helpToFile(.stderr(), clap.Help, &main_params, .{}); - - const command = res.positionals[0] orelse return error.MissingCommand; - switch (command) { - .help => return clap.helpToFile(.stderr(), clap.Help, &main_params, .{}), - .serve => try Server.main(gpa, &iter, res), - .@"pub" => unreachable, + var app = yazap.App.init(gpa, "zits", "High performance NATS compatible client and server."); + defer app.deinit(); + + var zits_app = app.rootCommand(); + + var server_cmd = app.createCommand("serve", "Run a high performance NATS compatible server."); + try server_cmd.addArgs(&[_]yazap.Arg{ + yazap.Arg.singleValueOption( + "addr", + 'a', + std.fmt.comptimePrint( + "Address to bind to (default: {s})", + .{std.meta.fieldInfo(zits.Server.ServerInfo, .host).defaultValue().?}, + ), + ), + yazap.Arg.singleValueOption( + "port", + 'p', + std.fmt.comptimePrint( + "Port to listen on (default: {d})", + .{std.meta.fieldInfo(zits.Server.ServerInfo, .port).defaultValue().?}, + ), + ), + yazap.Arg.singleValueOption( + "name", + 'n', + "Server name (default: auto)", + ), + }); + try zits_app.addSubcommand(server_cmd); + + const pub_cmd = app.createCommand("pub", "Publish a message."); + try zits_app.addSubcommand(pub_cmd); + + const matches = try app.parseProcess(); + + if (matches.subcommandMatches("serve")) |serve_matches| { + var info: zits.Server.ServerInfo = .{ + .server_id = zits.Server.createId(), + .server_name = zits.Server.createName(), + .version = "zits-master", + .max_payload = 99999, + }; + if (serve_matches.getSingleValue("port")) |port| { + info.port = std.fmt.parseUnsigned(@TypeOf(info.port), port, 10) catch |err| std.process.fatal("Could not parse port ({s}): {}\n", .{ port, err }); + } + + if (serve_matches.getSingleValue("name")) |name| { + info.server_name = name; + } + + try Server.main(gpa, info); + return; } } |
