common/flags: make argFlag() return a slice

We always pass the result of this to mem.span() currently, no need for
the code duplication.
This commit is contained in:
Isaac Freund 2021-12-15 17:09:45 +01:00
parent d93ee2c27e
commit c3370afa3d
No known key found for this signature in database
GPG Key ID: 86DED400DDFD7A11
3 changed files with 11 additions and 9 deletions

View File

@ -63,9 +63,11 @@ pub fn ParseResult(comptime flags: []const Flag) type {
unreachable; // Invalid flag_name unreachable; // Invalid flag_name
} }
pub fn argFlag(self: Self, flag_name: [*:0]const u8) ?[*:0]const u8 { pub fn argFlag(self: Self, flag_name: [*:0]const u8) ?[:0]const u8 {
for (self.flag_data) |flag_data| { for (self.flag_data) |flag_data| {
if (cstr.cmp(flag_data.name, flag_name) == 0) return flag_data.value.arg; if (cstr.cmp(flag_data.name, flag_name) == 0) {
return std.mem.span(flag_data.value.arg);
}
} }
unreachable; // Invalid flag_name unreachable; // Invalid flag_name
} }

View File

@ -67,7 +67,7 @@ pub fn main() anyerror!void {
os.exit(0); os.exit(0);
} }
if (result.argFlag("-log-level")) |level_str| { if (result.argFlag("-log-level")) |level_str| {
const level = std.meta.stringToEnum(LogLevel, std.mem.span(level_str)) orelse { const level = std.meta.stringToEnum(LogLevel, level_str) orelse {
std.log.err("invalid log level '{s}'", .{level_str}); std.log.err("invalid log level '{s}'", .{level_str});
try io.getStdErr().writeAll(usage); try io.getStdErr().writeAll(usage);
os.exit(1); os.exit(1);
@ -81,7 +81,7 @@ pub fn main() anyerror!void {
} }
const startup_command = blk: { const startup_command = blk: {
if (result.argFlag("-c")) |command| { if (result.argFlag("-c")) |command| {
break :blk try util.gpa.dupeZ(u8, std.mem.span(command)); break :blk try util.gpa.dupeZ(u8, command);
} else { } else {
break :blk try defaultInitPath(); break :blk try defaultInitPath();
} }

View File

@ -336,23 +336,23 @@ pub fn main() !void {
os.exit(0); os.exit(0);
} }
if (result.argFlag("-view-padding")) |raw| { if (result.argFlag("-view-padding")) |raw| {
view_padding = std.fmt.parseUnsigned(u32, mem.span(raw), 10) catch view_padding = std.fmt.parseUnsigned(u32, raw, 10) catch
fatalPrintUsage("invalid value '{s}' provided to -view-padding", .{raw}); fatalPrintUsage("invalid value '{s}' provided to -view-padding", .{raw});
} }
if (result.argFlag("-outer-padding")) |raw| { if (result.argFlag("-outer-padding")) |raw| {
outer_padding = std.fmt.parseUnsigned(u32, mem.span(raw), 10) catch outer_padding = std.fmt.parseUnsigned(u32, raw, 10) catch
fatalPrintUsage("invalid value '{s}' provided to -outer-padding", .{raw}); fatalPrintUsage("invalid value '{s}' provided to -outer-padding", .{raw});
} }
if (result.argFlag("-main-location")) |raw| { if (result.argFlag("-main-location")) |raw| {
default_main_location = std.meta.stringToEnum(Location, mem.span(raw)) orelse default_main_location = std.meta.stringToEnum(Location, raw) orelse
fatalPrintUsage("invalid value '{s}' provided to -main-location", .{raw}); fatalPrintUsage("invalid value '{s}' provided to -main-location", .{raw});
} }
if (result.argFlag("-main-count")) |raw| { if (result.argFlag("-main-count")) |raw| {
default_main_count = std.fmt.parseUnsigned(u32, mem.span(raw), 10) catch default_main_count = std.fmt.parseUnsigned(u32, raw, 10) catch
fatalPrintUsage("invalid value '{s}' provided to -main-count", .{raw}); fatalPrintUsage("invalid value '{s}' provided to -main-count", .{raw});
} }
if (result.argFlag("-main-ratio")) |raw| { if (result.argFlag("-main-ratio")) |raw| {
default_main_ratio = std.fmt.parseFloat(f64, mem.span(raw)) catch default_main_ratio = std.fmt.parseFloat(f64, raw) catch
fatalPrintUsage("invalid value '{s}' provided to -main-ratio", .{raw}); fatalPrintUsage("invalid value '{s}' provided to -main-ratio", .{raw});
} }