diff options
| author | Jon Parise <jon@indelible.org> | 2024-07-09 09:03:23 -0400 |
|---|---|---|
| committer | Jon Parise <jon@indelible.org> | 2024-07-09 09:08:27 -0400 |
| commit | 9de940cbbfd5ada9e17298cd5feb103db6a8a979 (patch) | |
| tree | 22fa2cd9200a7eb9404789137b5ca3a92a094edf /src/cli/args.zig | |
| parent | 31d53849204c7294eba5e973047d9fc6f6e915ff (diff) | |
cli: boolean value support for packed structs
Allow standalone boolean values like "true" and "false" to turn on or
off all of the struct's fields.
Diffstat (limited to 'src/cli/args.zig')
| -rw-r--r-- | src/cli/args.zig | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/src/cli/args.zig b/src/cli/args.zig index 707416e38..0f21ea79b 100644 --- a/src/cli/args.zig +++ b/src/cli/args.zig @@ -285,6 +285,17 @@ fn parsePackedStruct(comptime T: type, v: []const u8) !T { var result: T = .{}; + // Allow standalone boolean values like "true" and "false" to + // turn on or off all of the struct's fields. + bools: { + const b = parseBool(v) catch break :bools; + inline for (info.fields) |field| { + assert(field.type == bool); + @field(result, field.name) = b; + } + return result; + } + // We split each value by "," var iter = std.mem.splitSequence(u8, v, ","); loop: while (iter.next()) |part_raw| { @@ -586,6 +597,34 @@ test "parseIntoField: packed struct negation" { try testing.expect(!data.v.b); } +test "parseIntoField: packed struct true/false" { + const testing = std.testing; + var arena = ArenaAllocator.init(testing.allocator); + defer arena.deinit(); + const alloc = arena.allocator(); + + const Field = packed struct { + a: bool = false, + b: bool = true, + }; + var data: struct { + v: Field, + } = undefined; + + try parseIntoField(@TypeOf(data), alloc, &data, "v", "true"); + try testing.expect(data.v.a); + try testing.expect(data.v.b); + + try parseIntoField(@TypeOf(data), alloc, &data, "v", "false"); + try testing.expect(!data.v.a); + try testing.expect(!data.v.b); + + try testing.expectError( + error.InvalidValue, + parseIntoField(@TypeOf(data), alloc, &data, "v", "true,a"), + ); +} + test "parseIntoField: packed struct whitespace" { const testing = std.testing; var arena = ArenaAllocator.init(testing.allocator); |
