Arena Allocatorや基本的なstdoutとstderrを使う
const std = @import("std");
const fs = std.fs;
const heap = std.heap;
const mem = std.mem;
const process = std.process;
var arena: heap.ArenaAllocator = undefined;
fn help(writer: anytype, appname: []const u8) !noreturn {
try writer.print("Usage: {s} [ARG]\n", .{appname});
arena.deinit();
process.exit(1);
}
pub fn main() !void {
var stdout_file = fs.File.stdout().writer(&.{});
const stdout = &stdout_file.interface;
var stderr_file = fs.File.stderr().writer(&.{});
const stderr = &stderr_file.interface;
arena = heap.ArenaAllocator.init(heap.page_allocator);
defer arena.deinit();
const allocator = arena.allocator();
const args = try process.argsAlloc(allocator);
if (args.len != 2) {
try help(stderr, args[0]);
}
if (mem.eql(u8, args[1], "--help") or mem.eql(u8, args[1], "-h")) {
try help(stdout, args[0]);
}
}