Sysexits-zig: the system exit codes as defined by <sysexits.h>

Hi,

I made a library for Zig which provides the system exit code constants as defined by <sysexits.h>.

This is based on a library for Rust that I’m developing, but it still has few features.

Example:

const std = @import("std");

const sysexits = @import("sysexits");

pub fn main() !u8 {
    const bytes = [_]u8{ 0xf0, 0x9f, 0x92, 0x96 };
    if (std.unicode.utf8ValidateSlice(&bytes)) {
        try std.io.getStdOut().writer().print("{s}\n", .{bytes});
        // exit by 0
        return @intFromEnum(sysexits.ExitCode.ok);
    } else {
        try std.io.getStdErr().writer().print("Error: invalid UTF-8 sequence\n", .{});
        // exit by 65
        return @intFromEnum(sysexits.ExitCode.data_err);
    }
}
4 Likes

Real nice! I know this is just a trivial detail, but maybe adding this:

    // Returns the integer value for the given code.
    pub fn code(self: Self) u7 {
        return @intFromEnum(self);
    }

which allows:

return sysexits.ExitCode.code(.ok);
4 Likes