cli: support --help and -h for actions

This commit is contained in:
Mitchell Hashimoto
2024-01-20 09:29:26 -08:00
parent 203b38fdac
commit b438998fb8
7 changed files with 97 additions and 2 deletions

View File

@@ -77,6 +77,17 @@ pub fn parse(comptime T: type, alloc: Allocator, dst: *T, iter: anytype) !void {
if (!try dst.parseManuallyHook(arena_alloc, arg, iter)) return;
}
// If the destination supports help then we check for it, call
// the help function and return.
if (@hasDecl(T, "help")) {
if (mem.eql(u8, arg, "--help") or
mem.eql(u8, arg, "-h"))
{
try dst.help();
return;
}
}
if (mem.startsWith(u8, arg, "--")) {
var key: []const u8 = arg[2..];
const value: ?[]const u8 = value: {