diff options
| author | 2021-12-21 20:26:54 +0100 | |
|---|---|---|
| committer | 2021-12-21 20:26:54 +0100 | |
| commit | 285fb8f5cc604a3886450aba56b5e217760ab748 (patch) | |
| tree | 309601281c29395c5db521c5e5fa02c427dcb2d5 | |
| parent | Improve help and usage examples (diff) | |
| parent | zig master updates: allocator changes (#60) (diff) | |
| download | zig-clap-285fb8f5cc604a3886450aba56b5e217760ab748.tar.gz zig-clap-285fb8f5cc604a3886450aba56b5e217760ab748.tar.xz zig-clap-285fb8f5cc604a3886450aba56b5e217760ab748.zip | |
Merge branch 'zig-master'
| -rw-r--r-- | .github/workflows/main.yml | 4 | ||||
| -rw-r--r-- | build.zig | 2 | ||||
| -rw-r--r-- | clap.zig | 8 | ||||
| -rw-r--r-- | clap/args.zig | 10 | ||||
| -rw-r--r-- | clap/comptime.zig | 8 |
5 files changed, 16 insertions, 16 deletions
diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index f97e69a..08d791f 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml | |||
| @@ -12,7 +12,7 @@ jobs: | |||
| 12 | submodules: recursive | 12 | submodules: recursive |
| 13 | - uses: goto-bus-stop/setup-zig@v1.3.0 | 13 | - uses: goto-bus-stop/setup-zig@v1.3.0 |
| 14 | with: | 14 | with: |
| 15 | version: 0.8.0 | 15 | version: master |
| 16 | - run: zig build | 16 | - run: zig build |
| 17 | lint: | 17 | lint: |
| 18 | runs-on: ubuntu-latest | 18 | runs-on: ubuntu-latest |
| @@ -20,5 +20,5 @@ jobs: | |||
| 20 | - uses: actions/checkout@v2.4.0 | 20 | - uses: actions/checkout@v2.4.0 |
| 21 | - uses: goto-bus-stop/setup-zig@v1.3.0 | 21 | - uses: goto-bus-stop/setup-zig@v1.3.0 |
| 22 | with: | 22 | with: |
| 23 | version: 0.8.0 | 23 | version: master |
| 24 | - run: zig fmt --check . | 24 | - run: zig fmt --check . |
| @@ -52,7 +52,7 @@ pub fn build(b: *Builder) void { | |||
| 52 | 52 | ||
| 53 | fn readMeStep(b: *Builder) *std.build.Step { | 53 | fn readMeStep(b: *Builder) *std.build.Step { |
| 54 | const s = b.allocator.create(std.build.Step) catch unreachable; | 54 | const s = b.allocator.create(std.build.Step) catch unreachable; |
| 55 | s.* = std.build.Step.init(.Custom, "ReadMeStep", b.allocator, struct { | 55 | s.* = std.build.Step.init(.custom, "ReadMeStep", b.allocator, struct { |
| 56 | fn make(step: *std.build.Step) anyerror!void { | 56 | fn make(step: *std.build.Step) anyerror!void { |
| 57 | @setEvalBranchQuota(10000); | 57 | @setEvalBranchQuota(10000); |
| 58 | _ = step; | 58 | _ = step; |
| @@ -74,7 +74,7 @@ pub fn parseParam(line: []const u8) !Param(Help) { | |||
| 74 | @setEvalBranchQuota(std.math.maxInt(u32)); | 74 | @setEvalBranchQuota(std.math.maxInt(u32)); |
| 75 | 75 | ||
| 76 | var found_comma = false; | 76 | var found_comma = false; |
| 77 | var it = mem.tokenize(line, " \t"); | 77 | var it = mem.tokenize(u8, line, " \t"); |
| 78 | var param_str = it.next() orelse return error.NoParamFound; | 78 | var param_str = it.next() orelse return error.NoParamFound; |
| 79 | 79 | ||
| 80 | const short_name = if (!mem.startsWith(u8, param_str, "--") and | 80 | const short_name = if (!mem.startsWith(u8, param_str, "--") and |
| @@ -337,7 +337,7 @@ pub const ParseOptions = struct { | |||
| 337 | /// `parse`, `parseEx` does not wrap the allocator so the heap allocator can be | 337 | /// `parse`, `parseEx` does not wrap the allocator so the heap allocator can be |
| 338 | /// quite expensive. (TODO: Can we pick a better default? For `parse`, this allocator | 338 | /// quite expensive. (TODO: Can we pick a better default? For `parse`, this allocator |
| 339 | /// is fine, as it wraps it in an arena) | 339 | /// is fine, as it wraps it in an arena) |
| 340 | allocator: *mem.Allocator = heap.page_allocator, | 340 | allocator: mem.Allocator = heap.page_allocator, |
| 341 | diagnostic: ?*Diagnostic = null, | 341 | diagnostic: ?*Diagnostic = null, |
| 342 | }; | 342 | }; |
| 343 | 343 | ||
| @@ -350,7 +350,7 @@ pub fn parse( | |||
| 350 | var iter = try args.OsIterator.init(opt.allocator); | 350 | var iter = try args.OsIterator.init(opt.allocator); |
| 351 | const clap = try parseEx(Id, params, &iter, .{ | 351 | const clap = try parseEx(Id, params, &iter, .{ |
| 352 | // Let's reuse the arena from the `OSIterator` since we already have it. | 352 | // Let's reuse the arena from the `OSIterator` since we already have it. |
| 353 | .allocator = &iter.arena.allocator, | 353 | .allocator = iter.arena.allocator(), |
| 354 | .diagnostic = opt.diagnostic, | 354 | .diagnostic = opt.diagnostic, |
| 355 | }); | 355 | }); |
| 356 | 356 | ||
| @@ -409,7 +409,7 @@ pub fn helpFull( | |||
| 409 | try printParam(cs.writer(), Id, param, Error, context, valueText); | 409 | try printParam(cs.writer(), Id, param, Error, context, valueText); |
| 410 | try stream.writeByteNTimes(' ', max_spacing - @intCast(usize, cs.bytes_written)); | 410 | try stream.writeByteNTimes(' ', max_spacing - @intCast(usize, cs.bytes_written)); |
| 411 | const help_text = try helpText(context, param); | 411 | const help_text = try helpText(context, param); |
| 412 | var help_text_line_it = mem.split(help_text, "\n"); | 412 | var help_text_line_it = mem.split(u8, help_text, "\n"); |
| 413 | var indent_line = false; | 413 | var indent_line = false; |
| 414 | while (help_text_line_it.next()) |line| : (indent_line = true) { | 414 | while (help_text_line_it.next()) |line| : (indent_line = true) { |
| 415 | if (indent_line) { | 415 | if (indent_line) { |
diff --git a/clap/args.zig b/clap/args.zig index a6be833..16299c8 100644 --- a/clap/args.zig +++ b/clap/args.zig | |||
| @@ -57,7 +57,7 @@ pub const OsIterator = struct { | |||
| 57 | /// return an error when we have no exe. | 57 | /// return an error when we have no exe. |
| 58 | exe_arg: ?[:0]const u8, | 58 | exe_arg: ?[:0]const u8, |
| 59 | 59 | ||
| 60 | pub fn init(allocator: *mem.Allocator) Error!OsIterator { | 60 | pub fn init(allocator: mem.Allocator) Error!OsIterator { |
| 61 | var res = OsIterator{ | 61 | var res = OsIterator{ |
| 62 | .arena = heap.ArenaAllocator.init(allocator), | 62 | .arena = heap.ArenaAllocator.init(allocator), |
| 63 | .args = process.args(), | 63 | .args = process.args(), |
| @@ -73,7 +73,7 @@ pub const OsIterator = struct { | |||
| 73 | 73 | ||
| 74 | pub fn next(iter: *OsIterator) Error!?[:0]const u8 { | 74 | pub fn next(iter: *OsIterator) Error!?[:0]const u8 { |
| 75 | if (builtin.os.tag == .windows) { | 75 | if (builtin.os.tag == .windows) { |
| 76 | return try iter.args.next(&iter.arena.allocator) orelse return null; | 76 | return try iter.args.next(iter.arena.allocator()) orelse return null; |
| 77 | } else { | 77 | } else { |
| 78 | return iter.args.nextPosix(); | 78 | return iter.args.nextPosix(); |
| 79 | } | 79 | } |
| @@ -91,7 +91,7 @@ pub const ShellIterator = struct { | |||
| 91 | arena: heap.ArenaAllocator, | 91 | arena: heap.ArenaAllocator, |
| 92 | str: []const u8, | 92 | str: []const u8, |
| 93 | 93 | ||
| 94 | pub fn init(allocator: *mem.Allocator, str: []const u8) ShellIterator { | 94 | pub fn init(allocator: mem.Allocator, str: []const u8) ShellIterator { |
| 95 | return .{ | 95 | return .{ |
| 96 | .arena = heap.ArenaAllocator.init(allocator), | 96 | .arena = heap.ArenaAllocator.init(allocator), |
| 97 | .str = str, | 97 | .str = str, |
| @@ -106,7 +106,7 @@ pub const ShellIterator = struct { | |||
| 106 | // Whenever possible, this iterator will return slices into `str` instead of | 106 | // Whenever possible, this iterator will return slices into `str` instead of |
| 107 | // allocating. Sometimes this is not possible, for example, escaped characters | 107 | // allocating. Sometimes this is not possible, for example, escaped characters |
| 108 | // have be be unescape, so we need to allocate in this case. | 108 | // have be be unescape, so we need to allocate in this case. |
| 109 | var list = std.ArrayList(u8).init(&iter.arena.allocator); | 109 | var list = std.ArrayList(u8).init(iter.arena.allocator()); |
| 110 | var start: usize = 0; | 110 | var start: usize = 0; |
| 111 | var state: enum { | 111 | var state: enum { |
| 112 | skip_whitespace, | 112 | skip_whitespace, |
| @@ -274,7 +274,7 @@ pub const ShellIterator = struct { | |||
| 274 | 274 | ||
| 275 | fn testShellIteratorOk(str: []const u8, allocations: usize, expect: []const []const u8) !void { | 275 | fn testShellIteratorOk(str: []const u8, allocations: usize, expect: []const []const u8) !void { |
| 276 | var allocator = testing.FailingAllocator.init(testing.allocator, allocations); | 276 | var allocator = testing.FailingAllocator.init(testing.allocator, allocations); |
| 277 | var it = ShellIterator.init(&allocator.allocator, str); | 277 | var it = ShellIterator.init(allocator.allocator(), str); |
| 278 | defer it.deinit(); | 278 | defer it.deinit(); |
| 279 | 279 | ||
| 280 | for (expect) |e| { | 280 | for (expect) |e| { |
diff --git a/clap/comptime.zig b/clap/comptime.zig index a0f57ad..b440004 100644 --- a/clap/comptime.zig +++ b/clap/comptime.zig | |||
| @@ -41,7 +41,7 @@ pub fn ComptimeClap( | |||
| 41 | single_options_is_set: std.PackedIntArray(u1, single_options), | 41 | single_options_is_set: std.PackedIntArray(u1, single_options), |
| 42 | flags: std.PackedIntArray(u1, flags), | 42 | flags: std.PackedIntArray(u1, flags), |
| 43 | pos: []const []const u8, | 43 | pos: []const []const u8, |
| 44 | allocator: *mem.Allocator, | 44 | allocator: mem.Allocator, |
| 45 | 45 | ||
| 46 | pub fn parse(iter: anytype, opt: clap.ParseOptions) !@This() { | 46 | pub fn parse(iter: anytype, opt: clap.ParseOptions) !@This() { |
| 47 | const allocator = opt.allocator; | 47 | const allocator = opt.allocator; |
| @@ -82,8 +82,8 @@ pub fn ComptimeClap( | |||
| 82 | if (multis.len != 0) | 82 | if (multis.len != 0) |
| 83 | try multis[param.id].append(arg.value.?); | 83 | try multis[param.id].append(arg.value.?); |
| 84 | } else { | 84 | } else { |
| 85 | debug.assert(res.flags.len() != 0); | 85 | debug.assert(res.flags.len != 0); |
| 86 | if (res.flags.len() != 0) | 86 | if (res.flags.len != 0) |
| 87 | res.flags.set(param.id, 1); | 87 | res.flags.set(param.id, 1); |
| 88 | } | 88 | } |
| 89 | } | 89 | } |
| @@ -195,7 +195,7 @@ fn testErr( | |||
| 195 | ) !void { | 195 | ) !void { |
| 196 | var diag = clap.Diagnostic{}; | 196 | var diag = clap.Diagnostic{}; |
| 197 | var iter = clap.args.SliceIterator{ .args = args_strings }; | 197 | var iter = clap.args.SliceIterator{ .args = args_strings }; |
| 198 | var args = clap.parseEx(u8, params, &iter, .{ | 198 | _ = clap.parseEx(u8, params, &iter, .{ |
| 199 | .allocator = testing.allocator, | 199 | .allocator = testing.allocator, |
| 200 | .diagnostic = &diag, | 200 | .diagnostic = &diag, |
| 201 | }) catch |err| { | 201 | }) catch |err| { |