diff options
| author | 2023-12-13 08:53:06 +0100 | |
|---|---|---|
| committer | 2023-12-13 08:53:06 +0100 | |
| commit | feffdff4094ea3927eb3880b46b65e700f1e86fb (patch) | |
| tree | 071a86610548ebed5b8bec0617cf79cbeeee65fd | |
| parent | Fix short only params not getting fields in `Arguments` (diff) | |
| download | zig-clap-feffdff4094ea3927eb3880b46b65e700f1e86fb.tar.gz zig-clap-feffdff4094ea3927eb3880b46b65e700f1e86fb.tar.xz zig-clap-feffdff4094ea3927eb3880b46b65e700f1e86fb.zip | |
Remove the default allocator from `ParseOptions`no-default-allocator
fixes #111
| -rw-r--r-- | README.md | 22 | ||||
| -rw-r--r-- | clap.zig | 7 | ||||
| -rw-r--r-- | example/help.zig | 7 | ||||
| -rw-r--r-- | example/simple-ex.zig | 4 | ||||
| -rw-r--r-- | example/simple.zig | 4 | ||||
| -rw-r--r-- | example/usage.zig | 7 |
6 files changed, 41 insertions, 10 deletions
| @@ -37,6 +37,9 @@ const debug = std.debug; | |||
| 37 | const io = std.io; | 37 | const io = std.io; |
| 38 | 38 | ||
| 39 | pub fn main() !void { | 39 | pub fn main() !void { |
| 40 | var gpa = std.heap.GeneralPurposeAllocator(.{}){}; | ||
| 41 | defer _ = gpa.deinit(); | ||
| 42 | |||
| 40 | // First we specify what parameters our program can take. | 43 | // First we specify what parameters our program can take. |
| 41 | // We can use `parseParamsComptime` to parse a string into an array of `Param(Help)` | 44 | // We can use `parseParamsComptime` to parse a string into an array of `Param(Help)` |
| 42 | const params = comptime clap.parseParamsComptime( | 45 | const params = comptime clap.parseParamsComptime( |
| @@ -53,6 +56,7 @@ pub fn main() !void { | |||
| 53 | var diag = clap.Diagnostic{}; | 56 | var diag = clap.Diagnostic{}; |
| 54 | var res = clap.parse(clap.Help, ¶ms, clap.parsers.default, .{ | 57 | var res = clap.parse(clap.Help, ¶ms, clap.parsers.default, .{ |
| 55 | .diagnostic = &diag, | 58 | .diagnostic = &diag, |
| 59 | .allocator = gpa.allocator(), | ||
| 56 | }) catch |err| { | 60 | }) catch |err| { |
| 57 | // Report useful error and exit | 61 | // Report useful error and exit |
| 58 | diag.report(io.getStdErr().writer(), err) catch {}; | 62 | diag.report(io.getStdErr().writer(), err) catch {}; |
| @@ -92,6 +96,9 @@ const io = std.io; | |||
| 92 | const process = std.process; | 96 | const process = std.process; |
| 93 | 97 | ||
| 94 | pub fn main() !void { | 98 | pub fn main() !void { |
| 99 | var gpa = std.heap.GeneralPurposeAllocator(.{}){}; | ||
| 100 | defer _ = gpa.deinit(); | ||
| 101 | |||
| 95 | // First we specify what parameters our program can take. | 102 | // First we specify what parameters our program can take. |
| 96 | // We can use `parseParamsComptime` to parse a string into an array of `Param(Help)` | 103 | // We can use `parseParamsComptime` to parse a string into an array of `Param(Help)` |
| 97 | const params = comptime clap.parseParamsComptime( | 104 | const params = comptime clap.parseParamsComptime( |
| @@ -116,6 +123,7 @@ pub fn main() !void { | |||
| 116 | var diag = clap.Diagnostic{}; | 123 | var diag = clap.Diagnostic{}; |
| 117 | var res = clap.parse(clap.Help, ¶ms, parsers, .{ | 124 | var res = clap.parse(clap.Help, ¶ms, parsers, .{ |
| 118 | .diagnostic = &diag, | 125 | .diagnostic = &diag, |
| 126 | .allocator = gpa.allocator(), | ||
| 119 | }) catch |err| { | 127 | }) catch |err| { |
| 120 | diag.report(io.getStdErr().writer(), err) catch {}; | 128 | diag.report(io.getStdErr().writer(), err) catch {}; |
| 121 | return err; | 129 | return err; |
| @@ -219,13 +227,18 @@ const clap = @import("clap"); | |||
| 219 | const std = @import("std"); | 227 | const std = @import("std"); |
| 220 | 228 | ||
| 221 | pub fn main() !void { | 229 | pub fn main() !void { |
| 230 | var gpa = std.heap.GeneralPurposeAllocator(.{}){}; | ||
| 231 | defer _ = gpa.deinit(); | ||
| 232 | |||
| 222 | const params = comptime clap.parseParamsComptime( | 233 | const params = comptime clap.parseParamsComptime( |
| 223 | \\-h, --help Display this help and exit. | 234 | \\-h, --help Display this help and exit. |
| 224 | \\-v, --version Output version information and exit. | 235 | \\-v, --version Output version information and exit. |
| 225 | \\ | 236 | \\ |
| 226 | ); | 237 | ); |
| 227 | 238 | ||
| 228 | var res = try clap.parse(clap.Help, ¶ms, clap.parsers.default, .{}); | 239 | var res = try clap.parse(clap.Help, ¶ms, clap.parsers.default, .{ |
| 240 | .allocator = gpa.allocator(), | ||
| 241 | }); | ||
| 229 | defer res.deinit(); | 242 | defer res.deinit(); |
| 230 | 243 | ||
| 231 | // `clap.help` is a function that can print a simple help message. It can print any `Param` | 244 | // `clap.help` is a function that can print a simple help message. It can print any `Param` |
| @@ -257,6 +270,9 @@ const clap = @import("clap"); | |||
| 257 | const std = @import("std"); | 270 | const std = @import("std"); |
| 258 | 271 | ||
| 259 | pub fn main() !void { | 272 | pub fn main() !void { |
| 273 | var gpa = std.heap.GeneralPurposeAllocator(.{}){}; | ||
| 274 | defer _ = gpa.deinit(); | ||
| 275 | |||
| 260 | const params = comptime clap.parseParamsComptime( | 276 | const params = comptime clap.parseParamsComptime( |
| 261 | \\-h, --help Display this help and exit. | 277 | \\-h, --help Display this help and exit. |
| 262 | \\-v, --version Output version information and exit. | 278 | \\-v, --version Output version information and exit. |
| @@ -264,7 +280,9 @@ pub fn main() !void { | |||
| 264 | \\ | 280 | \\ |
| 265 | ); | 281 | ); |
| 266 | 282 | ||
| 267 | var res = try clap.parse(clap.Help, ¶ms, clap.parsers.default, .{}); | 283 | var res = try clap.parse(clap.Help, ¶ms, clap.parsers.default, .{ |
| 284 | .allocator = gpa.allocator(), | ||
| 285 | }); | ||
| 268 | defer res.deinit(); | 286 | defer res.deinit(); |
| 269 | 287 | ||
| 270 | // `clap.usage` is a function that can print a simple help message. It can print any `Param` | 288 | // `clap.usage` is a function that can print a simple help message. It can print any `Param` |
| @@ -641,12 +641,7 @@ test "Diagnostic.report" { | |||
| 641 | 641 | ||
| 642 | /// Options that can be set to customize the behavior of parsing. | 642 | /// Options that can be set to customize the behavior of parsing. |
| 643 | pub const ParseOptions = struct { | 643 | pub const ParseOptions = struct { |
| 644 | /// The allocator used for all memory allocations. Defaults to the `heap.page_allocator`. | 644 | allocator: mem.Allocator, |
| 645 | /// Note: You should probably override this allocator if you are calling `parseEx`. Unlike | ||
| 646 | /// `parse`, `parseEx` does not wrap the allocator so the heap allocator can be | ||
| 647 | /// quite expensive. (TODO: Can we pick a better default? For `parse`, this allocator | ||
| 648 | /// is fine, as it wraps it in an arena) | ||
| 649 | allocator: mem.Allocator = heap.page_allocator, | ||
| 650 | diagnostic: ?*Diagnostic = null, | 645 | diagnostic: ?*Diagnostic = null, |
| 651 | }; | 646 | }; |
| 652 | 647 | ||
diff --git a/example/help.zig b/example/help.zig index e83ae44..2f063c5 100644 --- a/example/help.zig +++ b/example/help.zig | |||
| @@ -2,13 +2,18 @@ const clap = @import("clap"); | |||
| 2 | const std = @import("std"); | 2 | const std = @import("std"); |
| 3 | 3 | ||
| 4 | pub fn main() !void { | 4 | pub fn main() !void { |
| 5 | var gpa = std.heap.GeneralPurposeAllocator(.{}){}; | ||
| 6 | defer _ = gpa.deinit(); | ||
| 7 | |||
| 5 | const params = comptime clap.parseParamsComptime( | 8 | const params = comptime clap.parseParamsComptime( |
| 6 | \\-h, --help Display this help and exit. | 9 | \\-h, --help Display this help and exit. |
| 7 | \\-v, --version Output version information and exit. | 10 | \\-v, --version Output version information and exit. |
| 8 | \\ | 11 | \\ |
| 9 | ); | 12 | ); |
| 10 | 13 | ||
| 11 | var res = try clap.parse(clap.Help, ¶ms, clap.parsers.default, .{}); | 14 | var res = try clap.parse(clap.Help, ¶ms, clap.parsers.default, .{ |
| 15 | .allocator = gpa.allocator(), | ||
| 16 | }); | ||
| 12 | defer res.deinit(); | 17 | defer res.deinit(); |
| 13 | 18 | ||
| 14 | // `clap.help` is a function that can print a simple help message. It can print any `Param` | 19 | // `clap.help` is a function that can print a simple help message. It can print any `Param` |
diff --git a/example/simple-ex.zig b/example/simple-ex.zig index dd5d929..436d058 100644 --- a/example/simple-ex.zig +++ b/example/simple-ex.zig | |||
| @@ -6,6 +6,9 @@ const io = std.io; | |||
| 6 | const process = std.process; | 6 | const process = std.process; |
| 7 | 7 | ||
| 8 | pub fn main() !void { | 8 | pub fn main() !void { |
| 9 | var gpa = std.heap.GeneralPurposeAllocator(.{}){}; | ||
| 10 | defer _ = gpa.deinit(); | ||
| 11 | |||
| 9 | // First we specify what parameters our program can take. | 12 | // First we specify what parameters our program can take. |
| 10 | // We can use `parseParamsComptime` to parse a string into an array of `Param(Help)` | 13 | // We can use `parseParamsComptime` to parse a string into an array of `Param(Help)` |
| 11 | const params = comptime clap.parseParamsComptime( | 14 | const params = comptime clap.parseParamsComptime( |
| @@ -30,6 +33,7 @@ pub fn main() !void { | |||
| 30 | var diag = clap.Diagnostic{}; | 33 | var diag = clap.Diagnostic{}; |
| 31 | var res = clap.parse(clap.Help, ¶ms, parsers, .{ | 34 | var res = clap.parse(clap.Help, ¶ms, parsers, .{ |
| 32 | .diagnostic = &diag, | 35 | .diagnostic = &diag, |
| 36 | .allocator = gpa.allocator(), | ||
| 33 | }) catch |err| { | 37 | }) catch |err| { |
| 34 | diag.report(io.getStdErr().writer(), err) catch {}; | 38 | diag.report(io.getStdErr().writer(), err) catch {}; |
| 35 | return err; | 39 | return err; |
diff --git a/example/simple.zig b/example/simple.zig index 429f095..a7207c7 100644 --- a/example/simple.zig +++ b/example/simple.zig | |||
| @@ -5,6 +5,9 @@ const debug = std.debug; | |||
| 5 | const io = std.io; | 5 | const io = std.io; |
| 6 | 6 | ||
| 7 | pub fn main() !void { | 7 | pub fn main() !void { |
| 8 | var gpa = std.heap.GeneralPurposeAllocator(.{}){}; | ||
| 9 | defer _ = gpa.deinit(); | ||
| 10 | |||
| 8 | // First we specify what parameters our program can take. | 11 | // First we specify what parameters our program can take. |
| 9 | // We can use `parseParamsComptime` to parse a string into an array of `Param(Help)` | 12 | // We can use `parseParamsComptime` to parse a string into an array of `Param(Help)` |
| 10 | const params = comptime clap.parseParamsComptime( | 13 | const params = comptime clap.parseParamsComptime( |
| @@ -21,6 +24,7 @@ pub fn main() !void { | |||
| 21 | var diag = clap.Diagnostic{}; | 24 | var diag = clap.Diagnostic{}; |
| 22 | var res = clap.parse(clap.Help, ¶ms, clap.parsers.default, .{ | 25 | var res = clap.parse(clap.Help, ¶ms, clap.parsers.default, .{ |
| 23 | .diagnostic = &diag, | 26 | .diagnostic = &diag, |
| 27 | .allocator = gpa.allocator(), | ||
| 24 | }) catch |err| { | 28 | }) catch |err| { |
| 25 | // Report useful error and exit | 29 | // Report useful error and exit |
| 26 | diag.report(io.getStdErr().writer(), err) catch {}; | 30 | diag.report(io.getStdErr().writer(), err) catch {}; |
diff --git a/example/usage.zig b/example/usage.zig index 333536b..a773dd2 100644 --- a/example/usage.zig +++ b/example/usage.zig | |||
| @@ -2,6 +2,9 @@ const clap = @import("clap"); | |||
| 2 | const std = @import("std"); | 2 | const std = @import("std"); |
| 3 | 3 | ||
| 4 | pub fn main() !void { | 4 | pub fn main() !void { |
| 5 | var gpa = std.heap.GeneralPurposeAllocator(.{}){}; | ||
| 6 | defer _ = gpa.deinit(); | ||
| 7 | |||
| 5 | const params = comptime clap.parseParamsComptime( | 8 | const params = comptime clap.parseParamsComptime( |
| 6 | \\-h, --help Display this help and exit. | 9 | \\-h, --help Display this help and exit. |
| 7 | \\-v, --version Output version information and exit. | 10 | \\-v, --version Output version information and exit. |
| @@ -9,7 +12,9 @@ pub fn main() !void { | |||
| 9 | \\ | 12 | \\ |
| 10 | ); | 13 | ); |
| 11 | 14 | ||
| 12 | var res = try clap.parse(clap.Help, ¶ms, clap.parsers.default, .{}); | 15 | var res = try clap.parse(clap.Help, ¶ms, clap.parsers.default, .{ |
| 16 | .allocator = gpa.allocator(), | ||
| 17 | }); | ||
| 13 | defer res.deinit(); | 18 | defer res.deinit(); |
| 14 | 19 | ||
| 15 | // `clap.usage` is a function that can print a simple help message. It can print any `Param` | 20 | // `clap.usage` is a function that can print a simple help message. It can print any `Param` |