diff options
| author | Isaac Freund <ifreund@ifreund.xyz> | 2020-10-13 20:17:30 +0200 |
|---|---|---|
| committer | Isaac Freund <ifreund@ifreund.xyz> | 2020-10-13 20:17:30 +0200 |
| commit | 6ba1fdf7e0d8dfc348a163219b3c3215c3644dd6 (patch) | |
| tree | a8e5d328bc8f1dc3f47f8d84fc16ce1582ed673c /src/main.zig | |
| parent | f01c3150c19fa3d6c605cc1940f70cce303c5997 (diff) | |
| download | zig-6ba1fdf7e0d8dfc348a163219b3c3215c3644dd6.tar.gz zig-6ba1fdf7e0d8dfc348a163219b3c3215c3644dd6.zip | |
stage2: use meta.stringToEnum for Color parsing
This requires renaming the variants to be snake_case, which is the new
recommended style anyways.
Diffstat (limited to 'src/main.zig')
| -rw-r--r-- | src/main.zig | 36 |
1 files changed, 12 insertions, 24 deletions
diff --git a/src/main.zig b/src/main.zig index 8a3fb72102..f2036c31dd 100644 --- a/src/main.zig +++ b/src/main.zig @@ -28,9 +28,9 @@ pub fn fatal(comptime format: []const u8, args: anytype) noreturn { pub const max_src_size = 2 * 1024 * 1024 * 1024; // 2 GiB pub const Color = enum { - Auto, - Off, - On, + auto, + off, + on, }; const usage = @@ -380,7 +380,7 @@ fn buildOutputType( run, }, ) !void { - var color: Color = .Auto; + var color: Color = .auto; var optimize_mode: std.builtin.Mode = .Debug; var provided_name: ?[]const u8 = null; var link_mode: ?std.builtin.LinkMode = null; @@ -585,15 +585,9 @@ fn buildOutputType( } i += 1; const next_arg = args[i]; - if (mem.eql(u8, next_arg, "auto")) { - color = .Auto; - } else if (mem.eql(u8, next_arg, "on")) { - color = .On; - } else if (mem.eql(u8, next_arg, "off")) { - color = .Off; - } else { + color = std.meta.stringToEnum(Color, next_arg) orelse { fatal("expected [auto|on|off] after --color, found '{}'", .{next_arg}); - } + }; } else if (mem.eql(u8, arg, "--subsystem")) { if (i + 1 >= args.len) fatal("expected parameter after {}", .{arg}); i += 1; @@ -2374,7 +2368,7 @@ const Fmt = struct { pub fn cmdFmt(gpa: *Allocator, args: []const []const u8) !void { const stderr_file = io.getStdErr(); - var color: Color = .Auto; + var color: Color = .auto; var stdin_flag: bool = false; var check_flag: bool = false; var input_files = ArrayList([]const u8).init(gpa); @@ -2394,15 +2388,9 @@ pub fn cmdFmt(gpa: *Allocator, args: []const []const u8) !void { } i += 1; const next_arg = args[i]; - if (mem.eql(u8, next_arg, "auto")) { - color = .Auto; - } else if (mem.eql(u8, next_arg, "on")) { - color = .On; - } else if (mem.eql(u8, next_arg, "off")) { - color = .Off; - } else { + color = std.meta.stringToEnum(Color, next_arg) orelse { fatal("expected [auto|on|off] after --color, found '{}'", .{next_arg}); - } + }; } else if (mem.eql(u8, arg, "--stdin")) { stdin_flag = true; } else if (mem.eql(u8, arg, "--check")) { @@ -2626,9 +2614,9 @@ fn printErrMsgToFile( color: Color, ) !void { const color_on = switch (color) { - .Auto => file.isTty(), - .On => true, - .Off => false, + .auto => file.isTty(), + .on => true, + .off => false, }; const lok_token = parse_error.loc(); const span_first = lok_token; |
