flags: further cleanup after Zig 0.10

Long live the self hosted compiler!
This commit is contained in:
Isaac Freund 2023-01-08 17:01:14 +01:00
parent 09f3f141ae
commit 2c0c606596
No known key found for this signature in database
GPG Key ID: 86DED400DDFD7A11
4 changed files with 6 additions and 13 deletions

View File

@ -69,8 +69,7 @@ pub fn parser(comptime Arg: type, comptime flags: []const Flag) type {
var result_flags: Result.Flags = .{}; var result_flags: Result.Flags = .{};
var i: usize = 0; var i: usize = 0;
while (i < args.len) : (i += 1) { outer: while (i < args.len) : (i += 1) {
var parsed_flag = false;
inline for (flags) |flag| { 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) { switch (flag.kind) {
@ -85,10 +84,10 @@ pub fn parser(comptime Arg: type, comptime flags: []const Flag) type {
@field(result_flags, flag.name) = mem.span(args[i]); @field(result_flags, flag.name) = mem.span(args[i]);
}, },
} }
parsed_flag = true; continue :outer;
} }
} }
if (!parsed_flag) break; break;
} }
return Result{ return Result{

View File

@ -42,14 +42,12 @@ const usage: []const u8 =
pub var server: Server = undefined; pub var server: Server = undefined;
pub fn main() anyerror!void { pub fn main() anyerror!void {
// This line is here because of https://github.com/ziglang/zig/issues/7807
const argv: [][*:0]const u8 = os.argv;
const result = flags.parser([*:0]const u8, &.{ const result = flags.parser([*:0]const u8, &.{
.{ .name = "h", .kind = .boolean }, .{ .name = "h", .kind = .boolean },
.{ .name = "version", .kind = .boolean }, .{ .name = "version", .kind = .boolean },
.{ .name = "c", .kind = .arg }, .{ .name = "c", .kind = .arg },
.{ .name = "log-level", .kind = .arg }, .{ .name = "log-level", .kind = .arg },
}).parse(argv[1..]) catch { }).parse(os.argv[1..]) catch {
try io.getStdErr().writeAll(usage); try io.getStdErr().writeAll(usage);
os.exit(1); os.exit(1);
}; };

View File

@ -63,12 +63,10 @@ pub fn main() !void {
} }
fn _main() !void { fn _main() !void {
// This line is here because of https://github.com/ziglang/zig/issues/7807
const argv: [][*:0]const u8 = os.argv;
const result = flags.parser([*:0]const u8, &.{ const result = flags.parser([*:0]const u8, &.{
.{ .name = "h", .kind = .boolean }, .{ .name = "h", .kind = .boolean },
.{ .name = "version", .kind = .boolean }, .{ .name = "version", .kind = .boolean },
}).parse(argv[1..]) catch { }).parse(os.argv[1..]) catch {
try io.getStdErr().writeAll(usage); try io.getStdErr().writeAll(usage);
os.exit(1); os.exit(1);
}; };

View File

@ -303,8 +303,6 @@ const Output = struct {
}; };
pub fn main() !void { pub fn main() !void {
// https://github.com/ziglang/zig/issues/7807
const argv: [][*:0]const u8 = os.argv;
const result = flags.parser([*:0]const u8, &[_]flags.Flag{ const result = flags.parser([*:0]const u8, &[_]flags.Flag{
.{ .name = "h", .kind = .boolean }, .{ .name = "h", .kind = .boolean },
.{ .name = "version", .kind = .boolean }, .{ .name = "version", .kind = .boolean },
@ -313,7 +311,7 @@ pub fn main() !void {
.{ .name = "main-location", .kind = .arg }, .{ .name = "main-location", .kind = .arg },
.{ .name = "main-count", .kind = .arg }, .{ .name = "main-count", .kind = .arg },
.{ .name = "main-ratio", .kind = .arg }, .{ .name = "main-ratio", .kind = .arg },
}).parse(argv[1..]) catch { }).parse(os.argv[1..]) catch {
try std.io.getStdErr().writeAll(usage); try std.io.getStdErr().writeAll(usage);
os.exit(1); os.exit(1);
}; };