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

This commit is contained in:
Lee Cannon 2021-08-31 02:39:02 +01:00 committed by GitHub
parent cf75cad899
commit ede47d49eb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 20 additions and 0 deletions

View File

@ -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});

View File

@ -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});
}
}
}
}