From ede47d49eba1a2b03a991b8e33bc5f4ff7e24bb9 Mon Sep 17 00:00:00 2001 From: Lee Cannon Date: Tue, 31 Aug 2021 02:39:02 +0100 Subject: [PATCH] Print enum values for build options in help output (#9650) --- lib/std/build.zig | 13 +++++++++++++ lib/std/special/build_runner.zig | 7 +++++++ 2 files changed, 20 insertions(+) diff --git a/lib/std/build.zig b/lib/std/build.zig index afbe580f6d..1124cde2cb 100644 --- a/lib/std/build.zig +++ b/lib/std/build.zig @@ -94,6 +94,8 @@ pub const Builder = struct { name: []const u8, type_id: TypeId, description: []const u8, + /// If the `type_id` is `enum` this provides the list of enum options + enum_options: ?[]const []const u8, }; const UserInputOption = struct { @@ -482,10 +484,21 @@ pub const Builder = struct { const name = self.dupe(name_raw); const description = self.dupe(description_raw); const type_id = comptime typeToEnum(T); + const enum_options = if (type_id == .@"enum") blk: { + const fields = comptime std.meta.fields(T); + var options = ArrayList([]const u8).initCapacity(self.allocator, fields.len) catch unreachable; + + inline for (fields) |field| { + options.appendAssumeCapacity(field.name); + } + + break :blk options.toOwnedSlice(); + } else null; const available_option = AvailableOption{ .name = name, .type_id = type_id, .description = description, + .enum_options = enum_options, }; if ((self.available_options_map.fetchPut(name, available_option) catch unreachable) != null) { panic("Option '{s}' declared twice", .{name}); diff --git a/lib/std/special/build_runner.zig b/lib/std/special/build_runner.zig index 9be6ffa671..666abf247a 100644 --- a/lib/std/special/build_runner.zig +++ b/lib/std/special/build_runner.zig @@ -245,6 +245,13 @@ fn usage(builder: *Builder, already_ran_build: bool, out_stream: anytype) !void }); defer allocator.free(name); try out_stream.print("{s:<30} {s}\n", .{ name, option.description }); + if (option.enum_options) |enum_options| { + const padding = " " ** 33; + try out_stream.writeAll(padding ++ "Supported Values:\n"); + for (enum_options) |enum_option| { + try out_stream.print(padding ++ " {s}\n", .{enum_option}); + } + } } }