-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathe2_nested_commands.zig
More file actions
87 lines (76 loc) · 2.64 KB
/
e2_nested_commands.zig
File metadata and controls
87 lines (76 loc) · 2.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
const std = @import("std");
const chilli = @import("chilli");
fn rootExec(ctx: chilli.CommandContext) !void {
try ctx.command.printHelp();
}
fn dbMigrateExec(ctx: chilli.CommandContext) !void {
_ = ctx;
const io = std.Options.debug_io;
var stdout_buf: [4096]u8 = undefined;
var stdout_fw = std.Io.File.stdout().writer(io, &stdout_buf);
defer stdout_fw.flush() catch {};
const stdout = &stdout_fw.interface;
try stdout.print("Running database migrations...\n", .{});
}
fn dbSeedExec(ctx: chilli.CommandContext) !void {
const file = try ctx.getArg("file", []const u8);
const io = std.Options.debug_io;
var stdout_buf: [4096]u8 = undefined;
var stdout_fw = std.Io.File.stdout().writer(io, &stdout_buf);
defer stdout_fw.flush() catch {};
const stdout = &stdout_fw.interface;
try stdout.print("Seeding database from file: {s}\n", .{file});
}
pub fn main(init: std.process.Init.Minimal) anyerror!void {
var gpa: std.heap.DebugAllocator(.{}) = .init;
defer _ = gpa.deinit();
const allocator = gpa.allocator();
var root_cmd = try chilli.Command.init(allocator, .{
.name = "app",
.description = "An application with nested commands.",
.exec = rootExec,
});
defer root_cmd.deinit();
var db_cmd = try chilli.Command.init(allocator, .{
.name = "db",
.description = "Manage the application database.",
.exec = rootExec,
});
try root_cmd.addSubcommand(db_cmd);
const db_migrate_cmd = try chilli.Command.init(allocator, .{
.name = "migrate",
.description = "Run database migrations.",
.exec = dbMigrateExec,
});
try db_cmd.addSubcommand(db_migrate_cmd);
var db_seed_cmd = try chilli.Command.init(allocator, .{
.name = "seed",
.description = "Seed the database with initial data.",
.exec = dbSeedExec,
});
try db_cmd.addSubcommand(db_seed_cmd);
try db_seed_cmd.addPositional(.{
.name = "file",
.description = "The seed file to use.",
.is_required = true,
});
try root_cmd.run(init.args, null);
}
// Example Invocations
//
// 1. Build the example executable:
// zig build e2_nested_commands
//
// 2. Run with different arguments:
//
// // Show help for the root 'app' command
// ./zig-out/bin/e2_nested_commands --help
//
// // Show help for the 'db' subcommand
// ./zig-out/bin/e2_nested_commands db --help
//
// // Execute the 'db migrate' command
// ./zig-out/bin/e2_nested_commands db migrate
//
// // Execute the 'db seed' command with its required file argument
// ./zig-out/bin/e2_nested_commands db seed data/seeds.sql