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 {
9494 name: []const u8,
9595 type_id: TypeId,
9696 description: []const u8,
97 /// If the `type_id` is `enum` this provides the list of enum options
98 enum_options: ?[]const []const u8,
9799 };
98100
99101 const UserInputOption = struct {
......@@ -482,10 +484,21 @@ pub const Builder = struct {
482484 const name = self.dupe(name_raw);
483485 const description = self.dupe(description_raw);
484486 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;
485497 const available_option = AvailableOption{
486498 .name = name,
487499 .type_id = type_id,
488500 .description = description,
501 .enum_options = enum_options,
489502 };
490503 if ((self.available_options_map.fetchPut(name, available_option) catch unreachable) != null) {
491504 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
245245 });
246246 defer allocator.free(name);
247247 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 }
248255 }
249256 }
250257