Skip to content

Commit

Permalink
Update README
Browse files Browse the repository at this point in the history
  • Loading branch information
sam701 committed May 11, 2024
1 parent a630b1d commit 9a94c48
Showing 1 changed file with 33 additions and 26 deletions.
59 changes: 33 additions & 26 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
}
```

Expand Down

0 comments on commit 9a94c48

Please sign in to comment.