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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
|
const std = @import("std");
const zits = @import("zits");
const clap = @import("clap");
const Message = zits.MessageParser.Message;
const Server = zits.Server;
const SubCommands = enum {
help,
serve,
@"pub",
};
const main_parsers = .{
.command = clap.parsers.enumeration(SubCommands),
};
// The parameters for `main`. Parameters for the subcommands are specified further down.
const main_params = clap.parseParamsComptime(
\\-h, --help Display this help and exit.
\\<command>
\\
);
// To pass around arguments returned by clap, `clap.Result` and `clap.ResultEx` can be used to
// get the return type of `clap.parse` and `clap.parseEx`.
pub const MainArgs = clap.ResultEx(clap.Help, &main_params, main_parsers);
pub fn main() !void {
var dba: std.heap.DebugAllocator(.{}) = .init;
defer _ = dba.deinit();
const gpa = dba.allocator();
var iter = try std.process.ArgIterator.initWithAllocator(gpa);
defer iter.deinit();
_ = iter.next();
var diag = clap.Diagnostic{};
var res = clap.parseEx(clap.Help, &main_params, main_parsers, &iter, .{
.diagnostic = &diag,
.allocator = gpa,
// Terminate the parsing of arguments after parsing the first positional (0 is passed
// here because parsed positionals are, like slices and arrays, indexed starting at 0).
//
// This will terminate the parsing after parsing the subcommand enum and leave `iter`
// not fully consumed. It can then be reused to parse the arguments for subcommands.
.terminating_positional = 0,
}) catch |err| {
try diag.reportToFile(.stderr(), err);
return err;
};
defer res.deinit();
std.debug.print("res: {any}\n", .{res});
if (res.args.help != 0)
return clap.helpToFile(.stderr(), clap.Help, &main_params, .{});
const command = res.positionals[0] orelse return error.MissingCommand;
switch (command) {
.help => return clap.helpToFile(.stderr(), clap.Help, &main_params, .{}),
.serve => try Server.main(gpa, &iter, res),
.@"pub" => unreachable,
}
}
// fn serverMain(gpa: std.mem.Allocator, iter: *std.process.ArgIterator, main_args: MainArgs) !void {
// _ = iter;
// _ = main_args;
// var threaded: std.Io.Threaded = .init(gpa);
// defer threaded.deinit();
// const io = threaded.io();
// const info: ServerInfo = .{
// .server_id = "NBEK5DBBB4ZO5LTBGPXACZSB2QUTODC6GGN5NLOSPIGSRFWJID4XU52C",
// .server_name = "bar",
// .version = "2.11.8",
// .go = "go1.24.6",
// .headers = true,
// .max_payload = 1048576,
// };
// var server = try std.Io.net.IpAddress.listen(
// .{
// .ip4 = .{
// .bytes = .{ 0, 0, 0, 0 },
// .port = info.port,
// },
// },
// io,
// .{},
// );
// defer server.deinit(io);
// var group: std.Io.Group = .init;
// defer group.wait(io);
// for (0..5) |_| {
// const stream = try server.accept(io);
// group.async(io, handleConnection, .{ gpa, io, stream, info });
// }
// }
|