some setup for subcommands: do nothing right now, experimenting with pointers

This commit is contained in:
2026-05-23 23:33:42 +02:00
parent 067078b28f
commit 88504d6043
8 changed files with 71 additions and 43 deletions
+3
View File
@@ -0,0 +1,3 @@
const std = @import("std");
const db = @import("db.zig");
const notify = @import("notify.zig");
+2
View File
@@ -0,0 +1,2 @@
const std = @import("std");
const Reminder = @import("reminder.zig").Reminder;
+20
View File
@@ -0,0 +1,20 @@
const std = @import("std");
const sub = @import("subcommands.zig");
fn eql(a: []const u8, b: []const u8) bool {
return std.mem.eql(u8, a, b);
}
pub fn main(init: std.process.Init.Minimal) !void {
var arg_iter = try std.process.Args.Iterator.initAllocator(init.args, std.heap.page_allocator);
defer arg_iter.deinit();
_ = arg_iter.next();
const subcommand = (arg_iter.next()) orelse return;
for (sub.subcommands) |cmd| {
if (std.mem.eql(u8, subcommand, cmd.name)) {
return cmd.handler(&arg_iter);
}
}
}
+1
View File
@@ -0,0 +1 @@
const std = @import("std");
+8
View File
@@ -0,0 +1,8 @@
pub const Reminder = struct {
id: u64,
title: []const u8,
due_at: i64,
done: bool,
snoozed: bool,
created_at: i64,
};
-18
View File
@@ -1,18 +0,0 @@
//! By convention, root.zig is the root source file when making a package.
const std = @import("std");
const Io = std.Io;
/// This is a documentation comment to explain the `printAnotherMessage` function below.
///
/// Accepting an `Io.Writer` instance is a handy way to write reusable code.
pub fn printAnotherMessage(writer: *Io.Writer) Io.Writer.Error!void {
try writer.print("Run `zig build test` to run the tests.\n", .{});
}
pub fn add(a: i32, b: i32) i32 {
return a + b;
}
test "basic add functionality" {
try std.testing.expect(add(3, 7) == 10);
}
+32
View File
@@ -0,0 +1,32 @@
const std = @import("std");
pub const Handler = *const fn (*std.process.Args.Iterator) anyerror!void;
pub const Subcommand = struct {
name: []const u8,
handler: Handler,
};
pub const subcommands = [_]Subcommand{
.{ .name = "add", .handler = handleAdd },
.{ .name = "remove", .handler = handleRemove },
.{ .name = "done", .handler = handleDone },
.{ .name = "list", .handler = handleList },
};
pub fn handleAdd(args: *std.process.Args.Iterator) !void {
_ = args;
std.debug.print("add!", .{});
}
pub fn handleRemove(args: *std.process.Args.Iterator) !void {
_ = args;
}
pub fn handleDone(args: *std.process.Args.Iterator) !void {
_ = args;
}
pub fn handleList(args: *std.process.Args.Iterator) !void {
_ = args;
}