|
| 1 | +const std = @import("std"); |
| 2 | +const testing = std.testing; |
| 3 | +const mem = std.mem; |
| 4 | + |
| 5 | +pub const RocCmd = enum { |
| 6 | + roc_run, |
| 7 | + roc_build, |
| 8 | + roc_test, |
| 9 | + roc_repl, |
| 10 | + roc_format, |
| 11 | + roc_version, |
| 12 | + roc_check, |
| 13 | + roc_docs, |
| 14 | + roc_glue, |
| 15 | + roc_help, |
| 16 | + |
| 17 | + pub fn parse(str: []const u8) ?RocCmd { |
| 18 | + const map = std.static_string_map.StaticStringMap(RocCmd).initComptime(.{ |
| 19 | + .{ "run", .roc_run }, |
| 20 | + .{ "build", .roc_build }, |
| 21 | + .{ "test", .roc_test }, |
| 22 | + .{ "repl", .roc_repl }, |
| 23 | + .{ "format", .roc_format }, |
| 24 | + .{ "version", .roc_version }, |
| 25 | + .{ "check", .roc_check }, |
| 26 | + .{ "docs", .roc_docs }, |
| 27 | + .{ "glue", .roc_glue }, |
| 28 | + .{ "help", .roc_help }, |
| 29 | + }); |
| 30 | + |
| 31 | + if (map.get(str)) |cmd| { |
| 32 | + return cmd; |
| 33 | + } |
| 34 | + return null; |
| 35 | + } |
| 36 | +}; |
| 37 | + |
| 38 | +test "parse cli subcommands" { |
| 39 | + try testing.expectEqual(RocCmd.parse("build").?, .roc_build); |
| 40 | + try testing.expectEqual(RocCmd.parse(""), null); |
| 41 | +} |
| 42 | + |
| 43 | +pub const RocOpt = struct { |
| 44 | + opt: enum { |
| 45 | + none, |
| 46 | + size, |
| 47 | + speed, |
| 48 | + } = .none, |
| 49 | + emit_llvm_ir: bool = false, |
| 50 | + profiling: bool = false, |
| 51 | + timing: bool = false, |
| 52 | + fuzzing: bool = false, |
| 53 | + |
| 54 | + pub fn parse(args: []const []const u8) !struct { opt: RocOpt, next_index: usize } { |
| 55 | + var opt = RocOpt{}; |
| 56 | + var i: usize = 0; |
| 57 | + while (i < args.len) : (i += 1) { |
| 58 | + const arg = args[i]; |
| 59 | + |
| 60 | + // If argument doesn't start with '-', we're done parsing options |
| 61 | + if (arg.len == 0 or arg[0] != '-') { |
| 62 | + return .{ .opt = opt, .next_index = i }; |
| 63 | + } |
| 64 | + |
| 65 | + if (mem.eql(u8, arg, "--opt")) { |
| 66 | + // Check if there's a next argument |
| 67 | + if (i + 1 >= args.len) { |
| 68 | + return error.MissingOptValue; |
| 69 | + } |
| 70 | + i += 1; |
| 71 | + const value = args[i]; |
| 72 | + |
| 73 | + if (mem.eql(u8, value, "none")) { |
| 74 | + opt.opt = .none; |
| 75 | + } else if (mem.eql(u8, value, "size")) { |
| 76 | + opt.opt = .size; |
| 77 | + } else if (mem.eql(u8, value, "speed")) { |
| 78 | + opt.opt = .speed; |
| 79 | + } else { |
| 80 | + return error.InvalidOptValue; |
| 81 | + } |
| 82 | + } else if (mem.eql(u8, arg, "--emit-llvm-ir")) { |
| 83 | + opt.emit_llvm_ir = true; |
| 84 | + } else if (mem.eql(u8, arg, "--profiling")) { |
| 85 | + opt.profiling = true; |
| 86 | + } else if (mem.eql(u8, arg, "--time")) { |
| 87 | + opt.timing = true; |
| 88 | + } else if (mem.eql(u8, arg, "--fuzz")) { |
| 89 | + opt.fuzzing = true; |
| 90 | + } else { |
| 91 | + return error.InvalidArgument; |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + return .{ .opt = opt, .next_index = i }; |
| 96 | + } |
| 97 | +}; |
| 98 | + |
| 99 | +test "default options" { |
| 100 | + try testing.expectEqual(RocOpt{}, RocOpt{ |
| 101 | + .opt = .none, |
| 102 | + .emit_llvm_ir = false, |
| 103 | + .profiling = false, |
| 104 | + .timing = false, |
| 105 | + .fuzzing = false, |
| 106 | + }); |
| 107 | +} |
| 108 | + |
| 109 | +// Testing helper, split a string into arguments ignoring whitespace and empty strings |
| 110 | +fn splitArgs(allocator: std.mem.Allocator, str: []const u8) ![]const []const u8 { |
| 111 | + var args = std.ArrayList([]const u8).init(allocator); |
| 112 | + errdefer args.deinit(); |
| 113 | + |
| 114 | + var iter = std.mem.split(u8, str, " "); |
| 115 | + while (iter.next()) |arg| { |
| 116 | + if (arg.len > 0) { |
| 117 | + try args.append(arg); |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + return args.toOwnedSlice(); |
| 122 | +} |
| 123 | + |
| 124 | +test "parsing cli options" { |
| 125 | + const TestCase = struct { |
| 126 | + args: []const u8, |
| 127 | + expected: union(enum) { |
| 128 | + ok: struct { |
| 129 | + opt: RocOpt, |
| 130 | + next_index: usize, |
| 131 | + }, |
| 132 | + err: anyerror, |
| 133 | + }, |
| 134 | + }; |
| 135 | + |
| 136 | + const test_cases = &[_]TestCase{ |
| 137 | + .{ |
| 138 | + .args = "", |
| 139 | + .expected = .{ .ok = .{ .opt = RocOpt{}, .next_index = 0 } }, |
| 140 | + }, |
| 141 | + .{ |
| 142 | + .args = "--opt size", |
| 143 | + .expected = .{ .ok = .{ .opt = RocOpt{ .opt = .size }, .next_index = 2 } }, |
| 144 | + }, |
| 145 | + .{ |
| 146 | + .args = "--opt speed app.roc", |
| 147 | + .expected = .{ .ok = .{ .opt = RocOpt{ .opt = .speed }, .next_index = 2 } }, |
| 148 | + }, |
| 149 | + .{ |
| 150 | + .args = "--time build app.roc", |
| 151 | + .expected = .{ .ok = .{ .opt = RocOpt{ .timing = true }, .next_index = 1 } }, |
| 152 | + }, |
| 153 | + .{ |
| 154 | + .args = "--opt size --time app.roc", |
| 155 | + .expected = .{ .ok = .{ |
| 156 | + .opt = RocOpt{ .opt = .size, .timing = true }, |
| 157 | + .next_index = 3, |
| 158 | + } }, |
| 159 | + }, |
| 160 | + .{ |
| 161 | + .args = "build --time", |
| 162 | + .expected = .{ .ok = .{ .opt = RocOpt{}, .next_index = 0 } }, |
| 163 | + }, |
| 164 | + .{ |
| 165 | + .args = "--opt", |
| 166 | + .expected = .{ .err = error.MissingOptValue }, |
| 167 | + }, |
| 168 | + .{ |
| 169 | + .args = "--opt invalid", |
| 170 | + .expected = .{ .err = error.InvalidOptValue }, |
| 171 | + }, |
| 172 | + .{ |
| 173 | + .args = "--unknown-flag", |
| 174 | + .expected = .{ .err = error.InvalidArgument }, |
| 175 | + }, |
| 176 | + .{ |
| 177 | + .args = "app.roc --invalid", |
| 178 | + .expected = .{ .ok = .{ .opt = RocOpt{}, .next_index = 0 } }, |
| 179 | + }, |
| 180 | + }; |
| 181 | + |
| 182 | + for (test_cases) |tc| { |
| 183 | + const args = try splitArgs(testing.allocator, tc.args); |
| 184 | + defer testing.allocator.free(args); |
| 185 | + |
| 186 | + switch (tc.expected) { |
| 187 | + .ok => |expected| { |
| 188 | + const result = try RocOpt.parse(args); |
| 189 | + try testing.expectEqual(expected.opt, result.opt); |
| 190 | + try testing.expectEqual(expected.next_index, result.next_index); |
| 191 | + }, |
| 192 | + .err => |expected_err| { |
| 193 | + const result = RocOpt.parse(args); |
| 194 | + try testing.expectError(expected_err, result); |
| 195 | + }, |
| 196 | + } |
| 197 | + } |
| 198 | +} |
0 commit comments