summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Jimmi Holst Christensen2023-12-13 08:53:06 +0100
committerGravatar Jimmi Holst Christensen2023-12-13 08:53:06 +0100
commitfeffdff4094ea3927eb3880b46b65e700f1e86fb (patch)
tree071a86610548ebed5b8bec0617cf79cbeeee65fd
parentFix short only params not getting fields in `Arguments` (diff)
downloadzig-clap-no-default-allocator.tar.gz
zig-clap-no-default-allocator.tar.xz
zig-clap-no-default-allocator.zip
Remove the default allocator from `ParseOptions`no-default-allocator
fixes #111
-rw-r--r--README.md22
-rw-r--r--clap.zig7
-rw-r--r--example/help.zig7
-rw-r--r--example/simple-ex.zig4
-rw-r--r--example/simple.zig4
-rw-r--r--example/usage.zig7
6 files changed, 41 insertions, 10 deletions
diff --git a/README.md b/README.md
index f5018fc..f74c11f 100644
--- a/README.md
+++ b/README.md
@@ -37,6 +37,9 @@ const debug = std.debug;
37const io = std.io; 37const io = std.io;
38 38
39pub fn main() !void { 39pub 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, &params, clap.parsers.default, .{ 57 var res = clap.parse(clap.Help, &params, 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;
92const process = std.process; 96const process = std.process;
93 97
94pub fn main() !void { 98pub 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, &params, parsers, .{ 124 var res = clap.parse(clap.Help, &params, 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");
219const std = @import("std"); 227const std = @import("std");
220 228
221pub fn main() !void { 229pub 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, &params, clap.parsers.default, .{}); 239 var res = try clap.parse(clap.Help, &params, 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");
257const std = @import("std"); 270const std = @import("std");
258 271
259pub fn main() !void { 272pub 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, &params, clap.parsers.default, .{}); 283 var res = try clap.parse(clap.Help, &params, 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`
diff --git a/clap.zig b/clap.zig
index 302c8bb..e8a43d0 100644
--- a/clap.zig
+++ b/clap.zig
@@ -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.
643pub const ParseOptions = struct { 643pub 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");
2const std = @import("std"); 2const std = @import("std");
3 3
4pub fn main() !void { 4pub 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, &params, clap.parsers.default, .{}); 14 var res = try clap.parse(clap.Help, &params, 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;
6const process = std.process; 6const process = std.process;
7 7
8pub fn main() !void { 8pub 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, &params, parsers, .{ 34 var res = clap.parse(clap.Help, &params, 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;
5const io = std.io; 5const io = std.io;
6 6
7pub fn main() !void { 7pub 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, &params, clap.parsers.default, .{ 25 var res = clap.parse(clap.Help, &params, 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");
2const std = @import("std"); 2const std = @import("std");
3 3
4pub fn main() !void { 4pub 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, &params, clap.parsers.default, .{}); 15 var res = try clap.parse(clap.Help, &params, 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`