diff options
Diffstat (limited to 'src/main.zig')
| -rw-r--r-- | src/main.zig | 105 |
1 files changed, 42 insertions, 63 deletions
diff --git a/src/main.zig b/src/main.zig index 411be6a..e65516f 100644 --- a/src/main.zig +++ b/src/main.zig @@ -1,82 +1,61 @@ const std = @import("std"); const builtin = @import("builtin"); const zits = @import("zits"); -const yazap = @import("yazap"); const Message = zits.MessageParser.Message; const Server = zits.Server; const serve = @import("./subcommand/serve.zig").main; -pub fn main() !void { - var dba: std.heap.DebugAllocator(.{}) = .init; - defer _ = dba.deinit(); - const gpa = if (builtin.mode == .Debug or builtin.mode == .ReleaseSafe) dba.allocator() else std.heap.smp_allocator; +const help = + \\High Performance NATS compatible client and server. + \\ + \\Commands: + \\ serve Serve a high performance NATS compatible server + \\ publish Publish a message over the NATS protocol + \\ help Show this output + \\ + \\Global Flags: + \\ -h, --help Show context-sensitive help + \\ +; - 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.message.Message.ServerInfo, .host).defaultValue().?}, - ), - ), - yazap.Arg.singleValueOption( - "port", - 'p', - std.fmt.comptimePrint( - "Port to listen on (default: {d})", - .{std.meta.fieldInfo(zits.Server.message.Message.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); - - var io_impl: std.Io.Threaded = .init_single_threaded; - defer io_impl.deinit(); - const io = io_impl.io(); - - const matches = try app.parseProcess(io); +const Subcommand = enum { + serve, + publish, + help, +}; - if (matches.subcommandMatches("serve")) |serve_matches| { - var info: zits.Server.message.Message.ServerInfo = .{ - .server_id = zits.Server.default_id, - .server_name = zits.Server.default_name, - .version = "zits-master", - .max_payload = 1048576, - .headers = true, - }; - 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 }); - } +const to_subcommand: std.StaticStringMap(Subcommand) = .initComptime(.{ + .{ @tagName(.serve), .serve }, + .{ "srv", .serve }, + .{ @tagName(.publish), .publish }, + .{ "pub", .publish }, + .{ @tagName(.help), .help }, +}); - if (serve_matches.getSingleValue("name")) |name| { - info.server_name = name; - } +pub fn main(init: std.process.Init) !void { + const io = init.io; + const args = try init.minimal.args.toSlice(init.arena.allocator()); - try serve(gpa, info); - return; - } else if (matches.subcommandMatches("pub")) |_| { - std.debug.print("Unimplemented\n", .{}); + if (args.len == 1) { + try std.Io.File.stdout().writeStreamingAll(io, help); return; } - try app.displayHelp(io); + switch (to_subcommand.get(args[1]) orelse .help) { + .serve => { + try serve(init.gpa, io, args[2..]); + return; + }, + .publish => { + std.debug.print("Unimplemented\n", .{}); + }, + else => { + try std.Io.File.stdout().writeStreamingAll(io, help); + return; + }, + } } pub const std_options: std.Options = .{ |
