cli: fix --version flag

Before this, --version gets detected by the version action, but it ends
up erroring inside the version action's run, when it tries to parse the
args looking for --help.
This commit is contained in:
Matt Robenolt
2024-08-30 00:28:23 -07:00
parent 602bf6ba1e
commit a039e4208f

View File

@ -85,6 +85,11 @@ pub fn parse(comptime T: type, alloc: Allocator, dst: *T, iter: anytype) !void {
if (mem.startsWith(u8, arg, "--")) {
var key: []const u8 = arg[2..];
// Enables using --version as a CLI action, without
// expecting it to be parsed into key=value pairs.
if (mem.eql(u8, key, "version")) continue;
const value: ?[]const u8 = value: {
// If the arg has "=" then the value is after the "=".
if (mem.indexOf(u8, key, "=")) |idx| {
@ -354,7 +359,7 @@ test "parse: simple" {
var iter = try std.process.ArgIteratorGeneral(.{}).init(
testing.allocator,
"--a=42 --b --b-f=false",
"--a=42 --b --b-f=false --version",
);
defer iter.deinit();
try parse(@TypeOf(data), testing.allocator, &data, &iter);
@ -439,6 +444,22 @@ test "parse: error tracking" {
try testing.expect(!data._errors.empty());
}
test "parse: ignore version flag" {
const testing = std.testing;
var data: struct {
a: u8 = 0,
} = .{};
var iter = try std.process.ArgIteratorGeneral(.{}).init(
testing.allocator,
"--version --a=42",
);
defer iter.deinit();
try parse(@TypeOf(data), testing.allocator, &data, &iter);
try testing.expectEqual(@as(u8, 42), data.a);
}
test "parseIntoField: ignore underscore-prefixed fields" {
const testing = std.testing;
var arena = ArenaAllocator.init(testing.allocator);