~gpanders/wk

299b9eb3fc18ff0dc7b430210f7a245b58f1ef36 — Greg Anders 8 months ago 387c5be
Remove Commands array

The Commands array was solely being used as a reference of all possible
values of the Command enum. This was solely being used in the printUsage
function which iterated through each possible value and called the
`.usage()` method. Instead, use some Zig builtin functions to access the
enum fields at compile time.
2 files changed, 2 insertions(+), 10 deletions(-)

M src/cmd.zig
M src/main.zig
M src/cmd.zig => src/cmd.zig +0 -8
@@ 24,14 24,6 @@ pub const Command = enum {
    }
};

pub const Commands = &[_]Command{
    Command.Show,
    Command.List,
    Command.Open,
    Command.New,
    Command.Search,
};

pub fn handleCommand(command: []const u8, arg: ?[]const u8, ext: []const u8, allocator: *std.mem.Allocator) !void {
    switch (try parseCommand(command)) {
        .Show => if (arg) |a| {

M src/main.zig => src/main.zig +2 -2
@@ 10,8 10,8 @@ fn printUsage(exe: []const u8) void {
    stdout.print("Usage: {} COMMAND\n", .{std.fs.path.basename(exe)}) catch unreachable;
    stdout.print("\n", .{}) catch unreachable;
    stdout.print("Available commands:\n", .{}) catch unreachable;
    inline for (cmd.Commands) |command| {
        stdout.print("\t{}\n", .{command.usage()}) catch unreachable;
    inline for (@typeInfo(cmd.Command).Enum.fields) |command| {
        stdout.print("\t{}\n", .{@field(cmd.Command, command.name).usage()}) catch unreachable;
    }
}