flags: automatically prepend '-'

This makes the usage a bit cleaner as the results of the parsing may be
accessed with e.g. ret.flags.version instead of ret.flags.@"-version".
This commit is contained in:
Isaac Freund
2022-12-28 22:11:14 +01:00
parent 2be9ac05d6
commit e18d0d5e1c
6 changed files with 47 additions and 47 deletions

View File

@ -72,13 +72,13 @@ pub fn parser(comptime Arg: type, comptime flags: []const Flag) type {
while (i < args.len) : (i += 1) {
var parsed_flag = false;
inline for (flags) |flag| {
if (mem.eql(u8, flag.name, mem.span(args[i]))) {
if (mem.eql(u8, "-" ++ flag.name, mem.span(args[i]))) {
switch (flag.kind) {
.boolean => @field(result_flags, flag.name) = true,
.arg => {
i += 1;
if (i == args.len) {
std.log.err("option '" ++ flag.name ++
std.log.err("option '-" ++ flag.name ++
"' requires an argument but none was provided!", .{});
return error.MissingFlagArgument;
}