summaryrefslogtreecommitdiff
path: root/README.md
diff options
context:
space:
mode:
authorGravatar darcy2026-01-25 09:27:04 +1100
committerGravatar GitHub2026-01-24 23:27:04 +0100
commit1d3d273524e3c180f015ff1e93c83075e4634e2c (patch)
tree55693ab9cf02ecc4c3f171882259ef41adab4fb7 /README.md
parentfix: Regression in simple-ex example (diff)
downloadzig-clap-1d3d273524e3c180f015ff1e93c83075e4634e2c.tar.gz
zig-clap-1d3d273524e3c180f015ff1e93c83075e4634e2c.tar.xz
zig-clap-1d3d273524e3c180f015ff1e93c83075e4634e2c.zip
fix: use new `std.process.Init` args for Zig `0.16.0-dev.2261+d6b3dd25a` (#175)HEADmaster
Diffstat (limited to 'README.md')
-rw-r--r--README.md90
1 files changed, 24 insertions, 66 deletions
diff --git a/README.md b/README.md
index bbf1ea0..00ebe81 100644
--- a/README.md
+++ b/README.md
@@ -54,14 +54,7 @@ incomplete.
54The simplest way to use this library is to just call the `clap.parse` function. 54The simplest way to use this library is to just call the `clap.parse` function.
55 55
56```zig 56```zig
57pub fn main() !void { 57pub fn main(init: std.process.Init) !void {
58 var gpa_state = std.heap.DebugAllocator(.{}){};
59 const gpa = gpa_state.allocator();
60 defer _ = gpa_state.deinit();
61
62 var threaded: std.Io.Threaded = .init_single_threaded;
63 const io: std.Io = threaded.io();
64
65 // First we specify what parameters our program can take. 58 // First we specify what parameters our program can take.
66 // We can use `parseParamsComptime` to parse a string into an array of `Param(Help)`. 59 // We can use `parseParamsComptime` to parse a string into an array of `Param(Help)`.
67 const params = comptime clap.parseParamsComptime( 60 const params = comptime clap.parseParamsComptime(
@@ -76,12 +69,12 @@ pub fn main() !void {
76 // This is optional. You can also pass `.{}` to `clap.parse` if you don't 69 // This is optional. You can also pass `.{}` to `clap.parse` if you don't
77 // care about the extra information `Diagnostics` provides. 70 // care about the extra information `Diagnostics` provides.
78 var diag = clap.Diagnostic{}; 71 var diag = clap.Diagnostic{};
79 var res = clap.parse(clap.Help, &params, clap.parsers.default, .{ 72 var res = clap.parse(clap.Help, &params, clap.parsers.default, init.minimal.args, .{
80 .diagnostic = &diag, 73 .diagnostic = &diag,
81 .allocator = gpa, 74 .allocator = init.gpa,
82 }) catch |err| { 75 }) catch |err| {
83 // Report useful error and exit. 76 // Report useful error and exit.
84 try diag.reportToFile(io, .stderr(), err); 77 try diag.reportToFile(init.io, .stderr(), err);
85 return err; 78 return err;
86 }; 79 };
87 defer res.deinit(); 80 defer res.deinit();
@@ -112,14 +105,7 @@ contains a parser that returns `usize`. You can pass in something other than `cl
112if you want some other mapping. 105if you want some other mapping.
113 106
114```zig 107```zig
115pub fn main() !void { 108pub fn main(init: std.process.Init) !void {
116 var gpa_state = std.heap.DebugAllocator(.{}){};
117 const gpa = gpa_state.allocator();
118 defer _ = gpa_state.deinit();
119
120 var threaded: std.Io.Threaded = .init_single_threaded;
121 const io: std.Io = threaded.io();
122
123 // First we specify what parameters our program can take. 109 // First we specify what parameters our program can take.
124 // We can use `parseParamsComptime` to parse a string into an array of `Param(Help)`. 110 // We can use `parseParamsComptime` to parse a string into an array of `Param(Help)`.
125 const params = comptime clap.parseParamsComptime( 111 const params = comptime clap.parseParamsComptime(
@@ -142,15 +128,15 @@ pub fn main() !void {
142 }; 128 };
143 129
144 var diag = clap.Diagnostic{}; 130 var diag = clap.Diagnostic{};
145 var res = clap.parse(clap.Help, &params, parsers, .{ 131 var res = clap.parse(clap.Help, &params, parsers, init.minimal.args, .{
146 .diagnostic = &diag, 132 .diagnostic = &diag,
147 .allocator = gpa, 133 .allocator = init.gpa,
148 // The assignment separator can be configured. `--number=1` and `--number:1` is now 134 // The assignment separator can be configured. `--number=1` and `--number:1` is now
149 // allowed. 135 // allowed.
150 .assignment_separators = "=:", 136 .assignment_separators = "=:",
151 }) catch |err| { 137 }) catch |err| {
152 // Report useful error and exit. 138 // Report useful error and exit.
153 try diag.reportToFile(io, .stderr(), err); 139 try diag.reportToFile(init.io, .stderr(), err);
154 return err; 140 return err;
155 }; 141 };
156 defer res.deinit(); 142 defer res.deinit();
@@ -198,15 +184,8 @@ const main_params = clap.parseParamsComptime(
198// get the return type of `clap.parse` and `clap.parseEx`. 184// get the return type of `clap.parse` and `clap.parseEx`.
199const MainArgs = clap.ResultEx(clap.Help, &main_params, main_parsers); 185const MainArgs = clap.ResultEx(clap.Help, &main_params, main_parsers);
200 186
201pub fn main() !void { 187pub fn main(init: std.process.Init) !void {
202 var gpa_state = std.heap.DebugAllocator(.{}){}; 188 var iter = try init.minimal.args.iterateAllocator(init.gpa);
203 const gpa = gpa_state.allocator();
204 defer _ = gpa_state.deinit();
205
206 var threaded: std.Io.Threaded = .init_single_threaded;
207 const io: std.Io = threaded.io();
208
209 var iter = try std.process.ArgIterator.initWithAllocator(gpa);
210 defer iter.deinit(); 189 defer iter.deinit();
211 190
212 _ = iter.next(); 191 _ = iter.next();
@@ -214,7 +193,7 @@ pub fn main() !void {
214 var diag = clap.Diagnostic{}; 193 var diag = clap.Diagnostic{};
215 var res = clap.parseEx(clap.Help, &main_params, main_parsers, &iter, .{ 194 var res = clap.parseEx(clap.Help, &main_params, main_parsers, &iter, .{
216 .diagnostic = &diag, 195 .diagnostic = &diag,
217 .allocator = gpa, 196 .allocator = init.gpa,
218 197
219 // Terminate the parsing of arguments after parsing the first positional (0 is passed 198 // Terminate the parsing of arguments after parsing the first positional (0 is passed
220 // here because parsed positionals are, like slices and arrays, indexed starting at 0). 199 // here because parsed positionals are, like slices and arrays, indexed starting at 0).
@@ -223,7 +202,7 @@ pub fn main() !void {
223 // not fully consumed. It can then be reused to parse the arguments for subcommands. 202 // not fully consumed. It can then be reused to parse the arguments for subcommands.
224 .terminating_positional = 0, 203 .terminating_positional = 0,
225 }) catch |err| { 204 }) catch |err| {
226 try diag.reportToFile(io, .stderr(), err); 205 try diag.reportToFile(init.io, .stderr(), err);
227 return err; 206 return err;
228 }; 207 };
229 defer res.deinit(); 208 defer res.deinit();
@@ -234,11 +213,11 @@ pub fn main() !void {
234 const command = res.positionals[0] orelse return error.MissingCommand; 213 const command = res.positionals[0] orelse return error.MissingCommand;
235 switch (command) { 214 switch (command) {
236 .help => std.debug.print("--help\n", .{}), 215 .help => std.debug.print("--help\n", .{}),
237 .math => try mathMain(io, gpa, &iter, res), 216 .math => try mathMain(init.io, init.gpa, &iter, res),
238 } 217 }
239} 218}
240 219
241fn mathMain(io: std.Io, gpa: std.mem.Allocator, iter: *std.process.ArgIterator, main_args: MainArgs) !void { 220fn mathMain(io: std.Io, gpa: std.mem.Allocator, iter: *std.process.Args.Iterator, main_args: MainArgs) !void {
242 // The parent arguments are not used here, but there are cases where it might be useful, so 221 // The parent arguments are not used here, but there are cases where it might be useful, so
243 // this example shows how to pass the arguments around. 222 // this example shows how to pass the arguments around.
244 _ = main_args; 223 _ = main_args;
@@ -284,14 +263,7 @@ The `streaming.Clap` is the base of all the other parsers. It's a streaming pars
284`args.Iterator` to provide it with arguments lazily. 263`args.Iterator` to provide it with arguments lazily.
285 264
286```zig 265```zig
287pub fn main() !void { 266pub fn main(init: std.process.Init) !void {
288 var gpa_state = std.heap.DebugAllocator(.{}){};
289 const gpa = gpa_state.allocator();
290 defer _ = gpa_state.deinit();
291
292 var threaded: std.Io.Threaded = .init_single_threaded;
293 const io: std.Io = threaded.io();
294
295 // First we specify what parameters our program can take. 267 // First we specify what parameters our program can take.
296 const params = [_]clap.Param(u8){ 268 const params = [_]clap.Param(u8){
297 .{ 269 .{
@@ -306,7 +278,7 @@ pub fn main() !void {
306 .{ .id = 'f', .takes_value = .one }, 278 .{ .id = 'f', .takes_value = .one },
307 }; 279 };
308 280
309 var iter = try std.process.ArgIterator.initWithAllocator(gpa); 281 var iter = try init.minimal.args.iterateAllocator(init.gpa);
310 defer iter.deinit(); 282 defer iter.deinit();
311 283
312 // Skip exe argument. 284 // Skip exe argument.
@@ -316,7 +288,7 @@ pub fn main() !void {
316 // This is optional. You can also leave the `diagnostic` field unset if you 288 // This is optional. You can also leave the `diagnostic` field unset if you
317 // don't care about the extra information `Diagnostic` provides. 289 // don't care about the extra information `Diagnostic` provides.
318 var diag = clap.Diagnostic{}; 290 var diag = clap.Diagnostic{};
319 var parser = clap.streaming.Clap(u8, std.process.ArgIterator){ 291 var parser = clap.streaming.Clap(u8, std.process.Args.Iterator){
320 .params = &params, 292 .params = &params,
321 .iter = &iter, 293 .iter = &iter,
322 .diagnostic = &diag, 294 .diagnostic = &diag,
@@ -325,7 +297,7 @@ pub fn main() !void {
325 // Because we use a streaming parser, we have to consume each argument parsed individually. 297 // Because we use a streaming parser, we have to consume each argument parsed individually.
326 while (parser.next() catch |err| { 298 while (parser.next() catch |err| {
327 // Report useful error and exit. 299 // Report useful error and exit.
328 try diag.reportToFile(io, .stderr(), err); 300 try diag.reportToFile(init.io, .stderr(), err);
329 return err; 301 return err;
330 }) |arg| { 302 }) |arg| {
331 // arg.param will point to the parameter which matched the argument. 303 // arg.param will point to the parameter which matched the argument.
@@ -363,21 +335,14 @@ runtime.
363is passed to `help` to control how the help message is printed. 335is passed to `help` to control how the help message is printed.
364 336
365```zig 337```zig
366pub fn main() !void { 338pub fn main(init: std.process.Init) !void {
367 var gpa_state = std.heap.DebugAllocator(.{}){};
368 const gpa = gpa_state.allocator();
369 defer _ = gpa_state.deinit();
370
371 var threaded: std.Io.Threaded = .init_single_threaded;
372 const io: std.Io = threaded.io();
373
374 const params = comptime clap.parseParamsComptime( 339 const params = comptime clap.parseParamsComptime(
375 \\-h, --help Display this help and exit. 340 \\-h, --help Display this help and exit.
376 \\-v, --version Output version information and exit. 341 \\-v, --version Output version information and exit.
377 \\ 342 \\
378 ); 343 );
379 344
380 var res = try clap.parse(clap.Help, &params, clap.parsers.default, .{ .allocator = gpa }); 345 var res = try clap.parse(clap.Help, &params, clap.parsers.default, init.minimal.args, .{ .allocator = init.gpa });
381 defer res.deinit(); 346 defer res.deinit();
382 347
383 // `clap.help` is a function that can print a simple help message. It can print any `Param` 348 // `clap.help` is a function that can print a simple help message. It can print any `Param`
@@ -385,7 +350,7 @@ pub fn main() !void {
385 // The last argument contains options as to how `help` should print those parameters. Using 350 // The last argument contains options as to how `help` should print those parameters. Using
386 // `.{}` means the default options. 351 // `.{}` means the default options.
387 if (res.args.help != 0) 352 if (res.args.help != 0)
388 return clap.helpToFile(io, .stderr(), clap.Help, &params, .{}); 353 return clap.helpToFile(init.io, .stderr(), clap.Help, &params, .{});
389} 354}
390 355
391const clap = @import("clap"); 356const clap = @import("clap");
@@ -407,14 +372,7 @@ $ zig-out/bin/help --help
407`value` method so it can provide that in the output. 372`value` method so it can provide that in the output.
408 373
409```zig 374```zig
410pub fn main() !void { 375pub fn main(init: std.process.Init) !void {
411 var gpa_state = std.heap.DebugAllocator(.{}){};
412 const gpa = gpa_state.allocator();
413 defer _ = gpa_state.deinit();
414
415 var threaded: std.Io.Threaded = .init_single_threaded;
416 const io: std.Io = threaded.io();
417
418 const params = comptime clap.parseParamsComptime( 376 const params = comptime clap.parseParamsComptime(
419 \\-h, --help Display this help and exit. 377 \\-h, --help Display this help and exit.
420 \\-v, --version Output version information and exit. 378 \\-v, --version Output version information and exit.
@@ -422,13 +380,13 @@ pub fn main() !void {
422 \\ 380 \\
423 ); 381 );
424 382
425 var res = try clap.parse(clap.Help, &params, clap.parsers.default, .{ .allocator = gpa }); 383 var res = try clap.parse(clap.Help, &params, clap.parsers.default, init.minimal.args, .{ .allocator = init.gpa });
426 defer res.deinit(); 384 defer res.deinit();
427 385
428 // `clap.usageToFile` is a function that can print a simple usage string. It can print any 386 // `clap.usageToFile` is a function that can print a simple usage string. It can print any
429 // `Param` where `Id` has a `value` method (`Param(Help)` is one such parameter). 387 // `Param` where `Id` has a `value` method (`Param(Help)` is one such parameter).
430 if (res.args.help != 0) 388 if (res.args.help != 0)
431 return clap.usageToFile(io, .stdout(), clap.Help, &params); 389 return clap.usageToFile(init.io, .stdout(), clap.Help, &params);
432} 390}
433 391
434const clap = @import("clap"); 392const clap = @import("clap");