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
+5 -25
View File
@@ -3,10 +3,6 @@ const std = @import("std");
pub fn build(b: *std.Build) void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
const mod = b.addModule("nfi", .{
.root_source_file = b.path("src/root.zig"),
.target = target,
});
const exe = b.addExecutable(.{
.name = "nfi",
@@ -14,38 +10,22 @@ pub fn build(b: *std.Build) void {
.root_source_file = b.path("src/main.zig"),
.target = target,
.optimize = optimize,
.imports = &.{
.{ .name = "nfi", .module = mod },
},
}),
});
b.installArtifact(exe);
const run_step = b.step("run", "Run the app");
const run_cmd = b.addRunArtifact(exe);
run_step.dependOn(&run_cmd.step);
run_cmd.step.dependOn(b.getInstallStep());
if (b.args) |args| run_cmd.addArgs(args);
if (b.args) |args| {
run_cmd.addArgs(args);
}
const mod_tests = b.addTest(.{
.root_module = mod,
});
const run_mod_tests = b.addRunArtifact(mod_tests);
const run_step = b.step("run", "Run nfi");
run_step.dependOn(&run_cmd.step);
const exe_tests = b.addTest(.{
.root_module = exe.root_module,
});
const run_exe_tests = b.addRunArtifact(exe_tests);
const run_tests = b.addRunArtifact(exe_tests);
const test_step = b.step("test", "Run tests");
test_step.dependOn(&run_mod_tests.step);
test_step.dependOn(&run_exe_tests.step);
test_step.dependOn(&run_tests.step);
}
+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;
}