authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2026-08-20 15:46:49-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2026-08-20 17:35:12-07:00
log7e4d15b0c45858ebd24522676c20fc1cda5306ba
tree34ee72bfc8b72bf4d9dd5604b6d8458f50b5be31
parentbf4e2ab8d16e74374c4769f90e1f4042c0f968a1

Maker: more helpful CLI text on wrong enum tag provided


1 files changed, 26 insertions(+), 20 deletions(-)

lib/compiler/Maker.zig+26-20
...@@ -292,9 +292,7 @@ pub fn main(init: process.Init.Minimal) !void {...@@ -292,9 +292,7 @@ pub fn main(init: process.Init.Minimal) !void {
292 try cached_passthru_configure.append(arena, @intCast(configure_argv.items.len));292 try cached_passthru_configure.append(arena, @intCast(configure_argv.items.len));
293 configure_argv.appendAssumeCapacity(arg);293 configure_argv.appendAssumeCapacity(arg);
294 } else if (mem.eql(u8, arg, "--color")) {294 } else if (mem.eql(u8, arg, "--color")) {
295 const next_arg = nextArgOrFatal(args, &arg_i);295 color = nextEnumArg(args, &arg_i, Color);
296 color = stringToEnum(Color, next_arg) orelse
297 fatalWithHint("expected [auto|on|off] found {q}", .{next_arg});
298296
299 try cached_passthru_configure.append(arena, @intCast(configure_argv.items.len));297 try cached_passthru_configure.append(arena, @intCast(configure_argv.items.len));
300 configure_argv.appendAssumeCapacity(try arena.print("--color={t}", .{color}));298 configure_argv.appendAssumeCapacity(try arena.print("--color={t}", .{color}));
...@@ -401,25 +399,11 @@ pub fn main(init: process.Init.Minimal) !void {...@@ -401,25 +399,11 @@ pub fn main(init: process.Init.Minimal) !void {
401 } else if (mem.eql(u8, arg, "--libc")) {399 } else if (mem.eql(u8, arg, "--libc")) {
402 graph.libc_file = nextArgOrFatal(args, &arg_i);400 graph.libc_file = nextArgOrFatal(args, &arg_i);
403 } else if (mem.eql(u8, arg, "--error-style")) {401 } else if (mem.eql(u8, arg, "--error-style")) {
404 const next_arg = nextArg(args, &arg_i) orelse402 error_style = nextEnumArg(args, &arg_i, ErrorStyle);
405 fatalWithHint("expected style after {q}", .{arg});
406 error_style = stringToEnum(ErrorStyle, next_arg) orelse {
407 fatalWithHint("expected style after {q}, found {q}", .{ arg, next_arg });
408 };
409 } else if (mem.eql(u8, arg, "--multiline-errors")) {403 } else if (mem.eql(u8, arg, "--multiline-errors")) {
410 const next_arg = nextArg(args, &arg_i) orelse404 multiline_errors = nextEnumArg(args, &arg_i, MultilineErrors);
411 fatalWithHint("expected style after {q}", .{arg});
412 multiline_errors = stringToEnum(MultilineErrors, next_arg) orelse {
413 fatalWithHint("expected style after {q}, found {q}", .{ arg, next_arg });
414 };
415 } else if (mem.eql(u8, arg, "--summary")) {405 } else if (mem.eql(u8, arg, "--summary")) {
416 const next_arg = nextArg(args, &arg_i) orelse406 summary = nextEnumArg(args, &arg_i, Summary);
417 fatalWithHint("expected [all|new|failures|line|none] after {q}", .{arg});
418 summary = stringToEnum(Summary, next_arg) orelse {
419 fatalWithHint("expected [all|new|failures|line|none] after {q}, found {q}", .{
420 arg, next_arg,
421 });
422 };
423 } else if (mem.cutPrefix(u8, arg, "--seed=")) |rest| {407 } else if (mem.cutPrefix(u8, arg, "--seed=")) |rest| {
424 graph.random_seed = parseRandomSeed(rest);408 graph.random_seed = parseRandomSeed(rest);
425 } else if (mem.eql(u8, arg, "--build-id")) {409 } else if (mem.eql(u8, arg, "--build-id")) {
...@@ -4053,3 +4037,25 @@ fn confPathDepToCachePath(...@@ -4053,3 +4037,25 @@ fn confPathDepToCachePath(
4053 .install_include => @panic("TODO"),4037 .install_include => @panic("TODO"),
4054 };4038 };
4055}4039}
4040
4041fn fatalEnumHint(comptime E: type, arg: []const u8, param: ?[]const u8) noreturn {
4042 var buf: [100]u8 = undefined;
4043 var w: Io.Writer = .fixed(&buf);
4044 for (@typeInfo(E).@"enum".field_names) |field_name| {
4045 w.writeAll(field_name) catch unreachable;
4046 w.writeByte('|') catch unreachable;
4047 }
4048 const buffered = w.buffered();
4049 const enum_options_text = buffered[0 .. buffered.len - 1];
4050 if (param) |p| {
4051 fatalWithHint("expected [{s}] after {q}; found {q}", .{ enum_options_text, arg, p });
4052 } else {
4053 fatalWithHint("expected [{s}] after {q}", .{ enum_options_text, arg });
4054 }
4055}
4056
4057fn nextEnumArg(args: []const []const u8, i: *usize, comptime E: type) E {
4058 const arg = args[i.* - 1];
4059 const next_arg = nextArg(args, i) orelse fatalEnumHint(E, arg, null);
4060 return stringToEnum(E, next_arg) orelse fatalEnumHint(E, arg, next_arg);
4061}