authorgravatar for leecannon@leecannon.xyzLee Cannon <leecannon@leecannon.xyz> 2021-08-31 02:39:02+01:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2021-08-30 21:39:02-04:00
logede47d49eba1a2b03a991b8e33bc5f4ff7e24bb9
treed02977c553b5a1803d3ea69a9e8215080479e5b4
parentcf75cad899069d67d50e6d08160b998b7cba16dc
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

Print enum values for build options in help output (#9650)


2 files changed, 20 insertions(+), 0 deletions(-)

lib/std/build.zig+13
...@@ -94,6 +94,8 @@ pub const Builder = struct {...@@ -94,6 +94,8 @@ pub const Builder = struct {
94 name: []const u8,94 name: []const u8,
95 type_id: TypeId,95 type_id: TypeId,
96 description: []const u8,96 description: []const u8,
97 /// If the `type_id` is `enum` this provides the list of enum options
98 enum_options: ?[]const []const u8,
97 };99 };
98100
99 const UserInputOption = struct {101 const UserInputOption = struct {
...@@ -482,10 +484,21 @@ pub const Builder = struct {...@@ -482,10 +484,21 @@ pub const Builder = struct {
482 const name = self.dupe(name_raw);484 const name = self.dupe(name_raw);
483 const description = self.dupe(description_raw);485 const description = self.dupe(description_raw);
484 const type_id = comptime typeToEnum(T);486 const type_id = comptime typeToEnum(T);
487 const enum_options = if (type_id == .@"enum") blk: {
488 const fields = comptime std.meta.fields(T);
489 var options = ArrayList([]const u8).initCapacity(self.allocator, fields.len) catch unreachable;
490
491 inline for (fields) |field| {
492 options.appendAssumeCapacity(field.name);
493 }
494
495 break :blk options.toOwnedSlice();
496 } else null;
485 const available_option = AvailableOption{497 const available_option = AvailableOption{
486 .name = name,498 .name = name,
487 .type_id = type_id,499 .type_id = type_id,
488 .description = description,500 .description = description,
501 .enum_options = enum_options,
489 };502 };
490 if ((self.available_options_map.fetchPut(name, available_option) catch unreachable) != null) {503 if ((self.available_options_map.fetchPut(name, available_option) catch unreachable) != null) {
491 panic("Option '{s}' declared twice", .{name});504 panic("Option '{s}' declared twice", .{name});
lib/std/special/build_runner.zig+7
...@@ -245,6 +245,13 @@ fn usage(builder: *Builder, already_ran_build: bool, out_stream: anytype) !void...@@ -245,6 +245,13 @@ fn usage(builder: *Builder, already_ran_build: bool, out_stream: anytype) !void
245 });245 });
246 defer allocator.free(name);246 defer allocator.free(name);
247 try out_stream.print("{s:<30} {s}\n", .{ name, option.description });247 try out_stream.print("{s:<30} {s}\n", .{ name, option.description });
248 if (option.enum_options) |enum_options| {
249 const padding = " " ** 33;
250 try out_stream.writeAll(padding ++ "Supported Values:\n");
251 for (enum_options) |enum_option| {
252 try out_stream.print(padding ++ " {s}\n", .{enum_option});
253 }
254 }
248 }255 }
249 }256 }
250257