diff --git a/lib/compiler/Maker.zig b/lib/compiler/Maker.zig index b5132c6ad05787c9318d649beb2549b7282de07f..d32e3e15ad4f9bb37bf6fb9ccb358c9b1ea9f7c4 100644 --- a/lib/compiler/Maker.zig +++ b/lib/compiler/Maker.zig @@ -292,9 +292,7 @@ pub fn main(init: process.Init.Minimal) !void { try cached_passthru_configure.append(arena, @intCast(configure_argv.items.len)); configure_argv.appendAssumeCapacity(arg); } else if (mem.eql(u8, arg, "--color")) { - const next_arg = nextArgOrFatal(args, &arg_i); - color = stringToEnum(Color, next_arg) orelse - fatalWithHint("expected [auto|on|off] found {q}", .{next_arg}); + color = nextEnumArg(args, &arg_i, Color); try cached_passthru_configure.append(arena, @intCast(configure_argv.items.len)); configure_argv.appendAssumeCapacity(try arena.print("--color={t}", .{color})); @@ -401,25 +399,11 @@ pub fn main(init: process.Init.Minimal) !void { } else if (mem.eql(u8, arg, "--libc")) { graph.libc_file = nextArgOrFatal(args, &arg_i); } else if (mem.eql(u8, arg, "--error-style")) { - const next_arg = nextArg(args, &arg_i) orelse - fatalWithHint("expected style after {q}", .{arg}); - error_style = stringToEnum(ErrorStyle, next_arg) orelse { - fatalWithHint("expected style after {q}, found {q}", .{ arg, next_arg }); - }; + error_style = nextEnumArg(args, &arg_i, ErrorStyle); } else if (mem.eql(u8, arg, "--multiline-errors")) { - const next_arg = nextArg(args, &arg_i) orelse - fatalWithHint("expected style after {q}", .{arg}); - multiline_errors = stringToEnum(MultilineErrors, next_arg) orelse { - fatalWithHint("expected style after {q}, found {q}", .{ arg, next_arg }); - }; + multiline_errors = nextEnumArg(args, &arg_i, MultilineErrors); } else if (mem.eql(u8, arg, "--summary")) { - const next_arg = nextArg(args, &arg_i) orelse - fatalWithHint("expected [all|new|failures|line|none] after {q}", .{arg}); - summary = stringToEnum(Summary, next_arg) orelse { - fatalWithHint("expected [all|new|failures|line|none] after {q}, found {q}", .{ - arg, next_arg, - }); - }; + summary = nextEnumArg(args, &arg_i, Summary); } else if (mem.cutPrefix(u8, arg, "--seed=")) |rest| { graph.random_seed = parseRandomSeed(rest); } else if (mem.eql(u8, arg, "--build-id")) { @@ -4053,3 +4037,25 @@ fn confPathDepToCachePath( .install_include => @panic("TODO"), }; } + +fn fatalEnumHint(comptime E: type, arg: []const u8, param: ?[]const u8) noreturn { + var buf: [100]u8 = undefined; + var w: Io.Writer = .fixed(&buf); + for (@typeInfo(E).@"enum".field_names) |field_name| { + w.writeAll(field_name) catch unreachable; + w.writeByte('|') catch unreachable; + } + const buffered = w.buffered(); + const enum_options_text = buffered[0 .. buffered.len - 1]; + if (param) |p| { + fatalWithHint("expected [{s}] after {q}; found {q}", .{ enum_options_text, arg, p }); + } else { + fatalWithHint("expected [{s}] after {q}", .{ enum_options_text, arg }); + } +} + +fn nextEnumArg(args: []const []const u8, i: *usize, comptime E: type) E { + const arg = args[i.* - 1]; + const next_arg = nextArg(args, i) orelse fatalEnumHint(E, arg, null); + return stringToEnum(E, next_arg) orelse fatalEnumHint(E, arg, next_arg); +}