From 9a94c4803a52e54c26b198096d63fb5bde752da2 Mon Sep 17 00:00:00 2001 From: Alexei Samokvalov Date: Sat, 11 May 2024 09:08:55 +0200 Subject: [PATCH] Update README --- README.md | 59 +++++++++++++++++++++++++++++++------------------------ 1 file changed, 33 insertions(+), 26 deletions(-) diff --git a/README.md b/README.md index d04785a..33f60e3 100644 --- a/README.md +++ b/README.md @@ -24,41 +24,48 @@ Inspired by [urfave/cli](https://github.com/urfave/cli) Go package. const std = @import("std"); const cli = @import("zig-cli"); -var gpa = std.heap.GeneralPurposeAllocator(.{}){}; -const allocator = gpa.allocator(); - +// Define a configuration structure with default values. var config = struct { host: []const u8 = "localhost", port: u16 = undefined, }{}; -var host = cli.Option{ - .long_name = "host", - .help = "host to listen on", - .value_ref = cli.mkRef(&config.host), -}; -var port = cli.Option{ - .long_name = "port", - .help = "port to bind to", - .required = true, - .value_ref = cli.mkRef(&config.port), -}; -var app = &cli.App{ - .command = cli.Command{ - .name = "short", - .options = &.{ &host, &port }, - .target = cli.CommandTarget{ - .action = cli.CommandAction{ .exec = run_server }, - }, - }, -}; - pub fn main() !void { - return cli.run(app, allocator); + var r = try cli.AppRunner.init(std.heap.page_allocator); + + // Create an App with a command named "short" that takes host and port options. + const app = cli.App{ + .command = cli.Command{ + .name = "short", + .options = &.{ + // Define an Option for the "host" command-line argument. + .{ + .long_name = "host", + .help = "host to listen on", + .value_ref = r.mkRef(&config.host), + }, + + // Define an Option for the "port" command-line argument. + .{ + .long_name = "port", + .help = "port to bind to", + .required = true, + .value_ref = r.mkRef(&config.port), + }, + + }, + .target = cli.CommandTarget{ + .action = cli.CommandAction{ .exec = run_server }, + }, + }, + }; + return r.run(&app); } +// Action function to execute when the "short" command is invoked. fn run_server() !void { - std.log.debug("server is listening on {s}:{}", .{ config.host, config.port }); + // Log a debug message indicating the server is listening on the specified host and port. + std.log.debug("server is listening on {s}:{d}", .{ config.host, config.port }); } ```