0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Zigで小さなアプリを書くときによく使うテンプレ

Posted at

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]);
    }
}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?