summaryrefslogtreecommitdiff
path: root/src/main.zig
blob: c902202cd07a33ef9c2e2eeba31d25c29028d4bd (plain)
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
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;

/// There are 4 args: help, version, copyright, and quiet
/// ArgType also contains longarg, which is how zeven checks if the arg
/// will be --
const ArgType = enum {
    help,
    version,
    copyright,
    quiet,
    longarg,
    badarg,
};

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" {
    var test_int = try parseInt(i32, "-2", 10);

    try expect(test_int == -2);
}

test "parseInt(i32, \"\", 10) != 0" {
    var 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: ?ArgType = null;
    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| {
        f_arg = null;
        if (arg[0] == '-') { // It's an arg!
            f_arg = switch (arg[1]) {
                'h' => ArgType.help,
                'v' => ArgType.version,
                'c' => ArgType.copyright,
                'q' => ArgType.quiet,
                '-' => ArgType.longarg,
                '0'...'9' => null,
                else => ArgType.badarg,
            };
        }
        if (f_arg) |usearg| {
            var realarg = usearg;
            if (usearg == ArgType.longarg) {
                if (eql(u8, arg, "--help")) {
                    realarg = ArgType.help;
                } else if (eql(u8, arg, "--copyright")) {
                    realarg = ArgType.copyright;
                } else if (eql(u8, arg, "--version")) {
                    realarg = ArgType.version;
                } else if (eql(u8, arg, "--quiet")) {
                    realarg = ArgType.quiet;
                } else {
                    realarg = ArgType.badarg;
                }
            }
            _ = switch (realarg) {
                ArgType.quiet => {
                    quiet = true;
                    continue;
                },
                ArgType.help => {
                    return print_arg(0);
                },
                ArgType.copyright => {
                    return print_arg(1);
                },
                ArgType.version => {
                    return print_arg(2);
                },
                else => {
                    return print_arg(255);
                },
            };
        } 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("zeven copyright (c) Zachary Smith 2024\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;
        },
    };
}