const std = @import("std"); const argsWithAllocator = std.process.argsWithAllocator; const eql = std.mem.eql; const expect = std.testing.expect; const parseInt = std.fmt.parseInt; const print = std.debug.print; const GeneralPurposeAllocator = std.heap.GeneralPurposeAllocator; const VERSION = "1.0.0"; const LICENSE = "MIT"; test "@rem(-2, 2) == 0" { try expect(@rem(-2, 2) == 0); } test "parseInt(i32, \"-2\", 10) == -2" { const test_int = try parseInt(i32, "-2", 10); try expect(test_int == -2); } test "parseInt(i32, \"\", 10) != 0" { const test_int = try parseInt(i32, "-2", 10); try expect(test_int != 0); } pub fn main() anyerror!u8 { // This is an iterator for the provided args var gpa = GeneralPurposeAllocator(.{}){}; const allocator = gpa.allocator(); defer _ = gpa.deinit(); var arg_it = try argsWithAllocator(allocator); defer arg_it.deinit(); var f_arg: u5 = 0b0; var f_int: i32 = undefined; // This is whether or not to print the results var quiet: bool = false; // This is to check if a value has been provided var value: bool = false; // This skips the name of the binary _ = arg_it.next(); while (arg_it.next()) |arg| { if (arg[0] == '-') { // It's an arg! if (arg[1] == '-') { if (eql(u8, arg, "--help")) { f_arg = 0b1; } else if (eql(u8, arg, "--copyright")) { f_arg = 0b10; } else if (eql(u8, arg, "--version")) { f_arg = 0b100; } else if (eql(u8, arg, "--quiet")) { f_arg = 0b1000; } else { f_arg = 0b11111; } } else { f_arg = switch (arg[1]) { 'h' => f_arg | 0b1, 'c' => f_arg | 0b10, 'v' => f_arg | 0b100, 'q' => f_arg | 0b1000, '0'...'9' => 0, else => f_arg | 0b11111, }; } if (f_arg > 0) { if (f_arg ^ 0b11111 == 0) { return print_arg(255); } if (f_arg ^ 0b1 == 0) { return print_arg(0); } if (f_arg ^ 0b10 == 0) { return print_arg(1); } if (f_arg ^ 0b100 == 0) { return print_arg(2); } if (f_arg ^ 0b1000 == 0) { quiet = true; } } else { value = true; f_int = parseInt(i32, arg, 10) catch |err| return err; } } else { // Not an arg! value = true; f_int = parseInt(i32, arg, 10) catch |err| return err; } } if (value) { if (@rem(f_int, 2) == 0) { if (!quiet) { print("even\n", .{}); } return 0; } else { if (!quiet) { print("odd\n", .{}); } return 1; } } else { print("No value was provided!\n", .{}); } return 1; } pub fn print_arg(err: u8) !u8 { return switch (err) { 0 => { print("zeven: a binary by ZachIR to check if ", .{}); print("a number is even or odd\n", .{}); print("\tzeven [-hcv]\n", .{}); print("\tzeven [--help]\n", .{}); print("\tzeven [--copyright]\n", .{}); print("\tzeven [--version]\n", .{}); print("\tzeven [-q] [--quiet] NUMBER\n", .{}); print("\t-h: prints this help message\n", .{}); print("\t-c: prints the copyright information\n", .{}); print("\t-v: prints the version\n", .{}); print("\t-q: just uses error codes, no text\n", .{}); return 0; }, 1 => { print("copyright (c) 2025 ZachIR\n", .{}); print("zeven is Free software under the {s} ", .{LICENSE}); print("license, and is provided without ", .{}); print("warranty of any kind.\n", .{}); return 0; }, 2 => { print("zeven version {s}, 2024\n", .{VERSION}); return 0; }, else => { print("A bad argument was provided!\n", .{}); return 1; }, }; }