Skip to content

Commit

Permalink
Update README
Browse files Browse the repository at this point in the history
  • Loading branch information
sam701 committed Oct 14, 2023
1 parent 357397d commit 7c5b81a
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 7 deletions.
19 changes: 12 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ A simple package for building command line apps in Zig.
Inspired by [urfave/cli](https://github.com/urfave/cli) Go package.

## Features
* command line arguments are parsed into zig values
* long and short options: `--option1`, `-o`
* optional `=` sign: `--address=127.0.0.1` equals `--address 127.0.0.1`
* concatenated short options: `-a -b -c` equals `-abc`
Expand All @@ -24,19 +25,25 @@ const cli = @import("zig-cli");
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
const allocator = gpa.allocator();
var config = struct {
host: []const u8 = "localhost",
port: u16 = undefined,
}{};
var host = cli.Option{
.long_name = "host",
.help = "host to listen on",
.value = cli.OptionValue{ .string = null },
.value_ref = cli.mkRef(&config.host),
};
var port = cli.Option{
.long_name = "port",
.help = "port to bind to",
.value = cli.OptionValue{ .int = null },
.required = true,
.value_ref = cli.mkRef(&config.port),
};
var app = &cli.App{
.name = "awesome-app",
.options = &.{&host, &port},
.name = "short",
.options = &.{ &host, &port },
.action = run_server,
};
Expand All @@ -45,9 +52,7 @@ pub fn main() !void {
}
fn run_server(_: []const []const u8) !void {
var h = host.value.string.?;
var p = port.value.int.?;
std.log.debug("server is listening on {s}:{any}", .{ h, p });
std.log.debug("server is listening on {s}:{}", .{ config.host, config.port });
}
```

Expand Down
32 changes: 32 additions & 0 deletions src/tests.zig
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,38 @@ test "int and float" {
try expect(15.25 == bb);
}

test "optional values" {
var aa: ?i32 = null;
var bb: ?f32 = 500;
var cc: ?f32 = null;

var aa_opt = command.Option{
.long_name = "aa",
.help = "option aa",
.value_ref = mkRef(&aa),
};
var bb_opt = command.Option{
.long_name = "bb",
.help = "option bb",
.value_ref = mkRef(&bb),
};
var cc_opt = command.Option{
.long_name = "cc",
.help = "option cc",
.value_ref = mkRef(&cc),
};
var app = command.App{
.name = "abc",
.options = &.{ &aa_opt, &bb_opt, &cc_opt },
.action = dummy_action,
};

_ = try run(&app, &.{ "abc", "--aa=34", "--bb", "15.25" });
try expect(34 == aa.?);
try expect(15.25 == bb.?);
try std.testing.expect(cc == null);
}

test "int list" {
var aa: []u64 = undefined;
var aa_opt = command.Option{
Expand Down

0 comments on commit 7c5b81a

Please sign in to comment.