diff options
| author | 2024-07-18 16:56:11 +0200 | |
|---|---|---|
| committer | 2024-07-18 17:15:37 +0200 | |
| commit | f0a281ffe91fb64f116dcb04c34cecef70e8c79c (patch) | |
| tree | 3768dd0d4f18871161646493bc18260e1f2786a4 /clap.zig | |
| parent | Update to newest version of zig (diff) | |
| download | zig-clap-f0a281ffe91fb64f116dcb04c34cecef70e8c79c.tar.gz zig-clap-f0a281ffe91fb64f116dcb04c34cecef70e8c79c.tar.xz zig-clap-f0a281ffe91fb64f116dcb04c34cecef70e8c79c.zip | |
feat: Allow for the assignment separator to be configured
Diffstat (limited to '')
| -rw-r--r-- | clap.zig | 23 |
1 files changed, 23 insertions, 0 deletions
| @@ -18,6 +18,8 @@ test "clap" { | |||
| 18 | testing.refAllDecls(@This()); | 18 | testing.refAllDecls(@This()); |
| 19 | } | 19 | } |
| 20 | 20 | ||
| 21 | pub const default_assignment_separators = "="; | ||
| 22 | |||
| 21 | /// The names a `Param` can have. | 23 | /// The names a `Param` can have. |
| 22 | pub const Names = struct { | 24 | pub const Names = struct { |
| 23 | /// '-' prefix | 25 | /// '-' prefix |
| @@ -643,6 +645,7 @@ test "Diagnostic.report" { | |||
| 643 | pub const ParseOptions = struct { | 645 | pub const ParseOptions = struct { |
| 644 | allocator: mem.Allocator, | 646 | allocator: mem.Allocator, |
| 645 | diagnostic: ?*Diagnostic = null, | 647 | diagnostic: ?*Diagnostic = null, |
| 648 | assignment_separators: []const u8 = default_assignment_separators, | ||
| 646 | }; | 649 | }; |
| 647 | 650 | ||
| 648 | /// Same as `parseEx` but uses the `args.OsIterator` by default. | 651 | /// Same as `parseEx` but uses the `args.OsIterator` by default. |
| @@ -662,6 +665,7 @@ pub fn parse( | |||
| 662 | // Let's reuse the arena from the `OSIterator` since we already have it. | 665 | // Let's reuse the arena from the `OSIterator` since we already have it. |
| 663 | .allocator = arena.allocator(), | 666 | .allocator = arena.allocator(), |
| 664 | .diagnostic = opt.diagnostic, | 667 | .diagnostic = opt.diagnostic, |
| 668 | .assignment_separators = opt.assignment_separators, | ||
| 665 | }); | 669 | }); |
| 666 | 670 | ||
| 667 | return Result(Id, params, value_parsers){ | 671 | return Result(Id, params, value_parsers){ |
| @@ -733,6 +737,7 @@ pub fn parseEx( | |||
| 733 | .params = params, | 737 | .params = params, |
| 734 | .iter = iter, | 738 | .iter = iter, |
| 735 | .diagnostic = opt.diagnostic, | 739 | .diagnostic = opt.diagnostic, |
| 740 | .assignment_separators = opt.assignment_separators, | ||
| 736 | }; | 741 | }; |
| 737 | while (try stream.next()) |arg| { | 742 | while (try stream.next()) |arg| { |
| 738 | // TODO: We cannot use `try` inside the inline for because of a compiler bug that | 743 | // TODO: We cannot use `try` inside the inline for because of a compiler bug that |
| @@ -955,6 +960,24 @@ test "str and u64" { | |||
| 955 | defer res.deinit(); | 960 | defer res.deinit(); |
| 956 | } | 961 | } |
| 957 | 962 | ||
| 963 | test "different assignment separators" { | ||
| 964 | const params = comptime parseParamsComptime( | ||
| 965 | \\-a, --aa <usize>... | ||
| 966 | \\ | ||
| 967 | ); | ||
| 968 | |||
| 969 | var iter = args.SliceIterator{ | ||
| 970 | .args = &.{ "-a=0", "--aa=1", "-a:2", "--aa:3" }, | ||
| 971 | }; | ||
| 972 | var res = try parseEx(Help, ¶ms, parsers.default, &iter, .{ | ||
| 973 | .allocator = testing.allocator, | ||
| 974 | .assignment_separators = "=:", | ||
| 975 | }); | ||
| 976 | defer res.deinit(); | ||
| 977 | |||
| 978 | try testing.expectEqualSlices(usize, &.{ 0, 1, 2, 3 }, res.args.aa); | ||
| 979 | } | ||
| 980 | |||
| 958 | test "everything" { | 981 | test "everything" { |
| 959 | const params = comptime parseParamsComptime( | 982 | const params = comptime parseParamsComptime( |
| 960 | \\-a, --aa | 983 | \\-a, --aa |