|
| 1 | +//! Schema-free argv classifier: splits tokens into positionals and flags. |
| 2 | +//! Per-command validation lives at the call site, not here. |
| 3 | + |
| 4 | +const std = @import("std"); |
| 5 | + |
| 6 | +/// A parsed command-line flag. |
| 7 | +/// |
| 8 | +/// `value` is `""` both for flags written without `=` (`--verbose`) and for |
| 9 | +/// flags written with a trailing `=` and nothing after it (`--output=`). This |
| 10 | +/// classifier does not distinguish the two: read `value` only for flags you |
| 11 | +/// know take an argument, and test boolean presence via `Parsed.has`. |
| 12 | +pub const Flag = struct { name: []const u8, value: []const u8 }; |
| 13 | + |
| 14 | +/// Result of classifying an argv slice. |
| 15 | +/// |
| 16 | +/// Ownership: the outer slices (`positionals`, `flags`) are owned by the arena |
| 17 | +/// passed to `classify`. The string *contents* — each name, value, and |
| 18 | +/// positional — alias the original `argv` and are NOT copied. A `Parsed` thus |
| 19 | +/// borrows both the arena and `argv`, and must not outlive either. |
| 20 | +pub const Parsed = struct { |
| 21 | + positionals: []const []const u8, |
| 22 | + flags: []const Flag, |
| 23 | + |
| 24 | + /// True if a flag named `name` is present, with or without a value. |
| 25 | + /// Use this for boolean flags. |
| 26 | + pub fn has(self: Parsed, name: []const u8) bool { |
| 27 | + for (self.flags) |f| if (std.mem.eql(u8, f.name, name)) return true; |
| 28 | + return false; |
| 29 | + } |
| 30 | + |
| 31 | + /// Value of the first flag matching `name`, or `null` if absent. |
| 32 | + /// A present flag with no `=` yields `""`, not `null` — only absence is `null`. |
| 33 | + pub fn get(self: Parsed, name: []const u8) ?[]const u8 { |
| 34 | + for (self.flags) |f| if (std.mem.eql(u8, f.name, name)) return f.value; |
| 35 | + return null; |
| 36 | + } |
| 37 | +}; |
| 38 | + |
| 39 | +/// Splits `argv` into positionals and flags. |
| 40 | +/// |
| 41 | +/// A token is a flag when it begins with `-`; all leading dashes are stripped |
| 42 | +/// from the name. The first `=` separates name from value, so `--foo=a=b` |
| 43 | +/// yields value `a=b`; a token with no `=` gets an empty value. |
| 44 | +/// |
| 45 | +/// The returned `Parsed` borrows `arena` (outer slices) and `argv` (contents); |
| 46 | +/// see `Parsed` for details. |
| 47 | +pub fn classify(arena: std.mem.Allocator, argv: []const []const u8) !Parsed { |
| 48 | + var pos: std.ArrayList([]const u8) = .empty; |
| 49 | + var flags: std.ArrayList(Flag) = .empty; |
| 50 | + |
| 51 | + for (argv) |tok| { |
| 52 | + if (tok.len > 0 and tok[0] == '-') { |
| 53 | + const body = std.mem.trimStart(u8, tok, "-"); |
| 54 | + if (std.mem.indexOfScalar(u8, body, '=')) |i| |
| 55 | + try flags.append(arena, .{ .name = body[0..i], .value = body[i + 1 ..] }) |
| 56 | + else |
| 57 | + try flags.append(arena, .{ .name = body, .value = "" }); |
| 58 | + } else try pos.append(arena, tok); |
| 59 | + } |
| 60 | + |
| 61 | + return .{ .positionals = pos.items, .flags = flags.items }; |
| 62 | +} |
| 63 | + |
| 64 | +// ───────────────────────── tests ───────────────────────── |
| 65 | +test "positionals only" { |
| 66 | + var arena = std.heap.ArenaAllocator.init(std.testing.allocator); |
| 67 | + defer arena.deinit(); |
| 68 | + |
| 69 | + const p = try classify(arena.allocator(), &.{ "add", "foo", "bar" }); |
| 70 | + try std.testing.expectEqual(@as(usize, 3), p.positionals.len); |
| 71 | + try std.testing.expectEqual(@as(usize, 0), p.flags.len); |
| 72 | + try std.testing.expectEqualStrings("bar", p.positionals[2]); |
| 73 | +} |
| 74 | + |
| 75 | +test "long and short flags, with and without value" { |
| 76 | + var arena = std.heap.ArenaAllocator.init(std.testing.allocator); |
| 77 | + defer arena.deinit(); |
| 78 | + |
| 79 | + const p = try classify(arena.allocator(), &.{ "--save", "-u", "--out=dist" }); |
| 80 | + try std.testing.expect(p.has("save")); |
| 81 | + try std.testing.expect(p.has("u")); |
| 82 | + try std.testing.expectEqualStrings("", p.get("save").?); |
| 83 | + try std.testing.expectEqualStrings("dist", p.get("out").?); |
| 84 | +} |
| 85 | + |
| 86 | +test "trailing '=' collapses to empty value" { |
| 87 | + var arena = std.heap.ArenaAllocator.init(std.testing.allocator); |
| 88 | + defer arena.deinit(); |
| 89 | + |
| 90 | + const p = try classify(arena.allocator(), &.{"--out="}); |
| 91 | + try std.testing.expect(p.has("out")); |
| 92 | + try std.testing.expectEqualStrings("", p.get("out").?); |
| 93 | +} |
| 94 | + |
| 95 | +test "first '=' wins" { |
| 96 | + var arena = std.heap.ArenaAllocator.init(std.testing.allocator); |
| 97 | + defer arena.deinit(); |
| 98 | + |
| 99 | + const p = try classify(arena.allocator(), &.{"--filter=a=b"}); |
| 100 | + try std.testing.expectEqualStrings("a=b", p.get("filter").?); |
| 101 | +} |
| 102 | + |
| 103 | +test "mixed order is preserved within each bucket" { |
| 104 | + var arena = std.heap.ArenaAllocator.init(std.testing.allocator); |
| 105 | + defer arena.deinit(); |
| 106 | + |
| 107 | + const p = try classify(arena.allocator(), &.{ "add", "--save", "pkg", "-u" }); |
| 108 | + try std.testing.expectEqualStrings("add", p.positionals[0]); |
| 109 | + try std.testing.expectEqualStrings("pkg", p.positionals[1]); |
| 110 | + try std.testing.expect(p.has("save") and p.has("u")); |
| 111 | +} |
| 112 | + |
| 113 | +test "absent flag is null, not empty" { |
| 114 | + var arena = std.heap.ArenaAllocator.init(std.testing.allocator); |
| 115 | + defer arena.deinit(); |
| 116 | + |
| 117 | + const p = try classify(arena.allocator(), &.{"add"}); |
| 118 | + try std.testing.expect(!p.has("nope")); |
| 119 | + try std.testing.expectEqual(@as(?[]const u8, null), p.get("nope")); |
| 120 | +} |
0 commit comments