diff options
| author | 2026-01-25 09:27:04 +1100 | |
|---|---|---|
| committer | 2026-01-24 23:27:04 +0100 | |
| commit | 1d3d273524e3c180f015ff1e93c83075e4634e2c (patch) | |
| tree | 55693ab9cf02ecc4c3f171882259ef41adab4fb7 | |
| parent | fix: Regression in simple-ex example (diff) | |
| download | zig-clap-1d3d273524e3c180f015ff1e93c83075e4634e2c.tar.gz zig-clap-1d3d273524e3c180f015ff1e93c83075e4634e2c.tar.xz zig-clap-1d3d273524e3c180f015ff1e93c83075e4634e2c.zip | |
| -rw-r--r-- | README.md | 90 | ||||
| -rw-r--r-- | build.zig.zon | 2 | ||||
| -rw-r--r-- | clap.zig | 5 | ||||
| -rw-r--r-- | example/help.zig | 13 | ||||
| -rw-r--r-- | example/simple-ex.zig | 15 | ||||
| -rw-r--r-- | example/simple.zig | 15 | ||||
| -rw-r--r-- | example/streaming-clap.zig | 15 | ||||
| -rw-r--r-- | example/subcommands.zig | 19 | ||||
| -rw-r--r-- | example/usage.zig | 13 |
9 files changed, 52 insertions, 135 deletions
| @@ -54,14 +54,7 @@ incomplete. | |||
| 54 | The simplest way to use this library is to just call the `clap.parse` function. | 54 | The simplest way to use this library is to just call the `clap.parse` function. |
| 55 | 55 | ||
| 56 | ```zig | 56 | ```zig |
| 57 | pub fn main() !void { | 57 | pub 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, ¶ms, clap.parsers.default, .{ | 72 | var res = clap.parse(clap.Help, ¶ms, 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 | |||
| 112 | if you want some other mapping. | 105 | if you want some other mapping. |
| 113 | 106 | ||
| 114 | ```zig | 107 | ```zig |
| 115 | pub fn main() !void { | 108 | pub 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, ¶ms, parsers, .{ | 131 | var res = clap.parse(clap.Help, ¶ms, 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`. |
| 199 | const MainArgs = clap.ResultEx(clap.Help, &main_params, main_parsers); | 185 | const MainArgs = clap.ResultEx(clap.Help, &main_params, main_parsers); |
| 200 | 186 | ||
| 201 | pub fn main() !void { | 187 | pub 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 | ||
| 241 | fn mathMain(io: std.Io, gpa: std.mem.Allocator, iter: *std.process.ArgIterator, main_args: MainArgs) !void { | 220 | fn 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 |
| 287 | pub fn main() !void { | 266 | pub 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 = ¶ms, | 292 | .params = ¶ms, |
| 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. | |||
| 363 | is passed to `help` to control how the help message is printed. | 335 | is passed to `help` to control how the help message is printed. |
| 364 | 336 | ||
| 365 | ```zig | 337 | ```zig |
| 366 | pub fn main() !void { | 338 | pub 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, ¶ms, clap.parsers.default, .{ .allocator = gpa }); | 345 | var res = try clap.parse(clap.Help, ¶ms, 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, ¶ms, .{}); | 353 | return clap.helpToFile(init.io, .stderr(), clap.Help, ¶ms, .{}); |
| 389 | } | 354 | } |
| 390 | 355 | ||
| 391 | const clap = @import("clap"); | 356 | const 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 |
| 410 | pub fn main() !void { | 375 | pub 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, ¶ms, clap.parsers.default, .{ .allocator = gpa }); | 383 | var res = try clap.parse(clap.Help, ¶ms, 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, ¶ms); | 389 | return clap.usageToFile(init.io, .stdout(), clap.Help, ¶ms); |
| 432 | } | 390 | } |
| 433 | 391 | ||
| 434 | const clap = @import("clap"); | 392 | const clap = @import("clap"); |
diff --git a/build.zig.zon b/build.zig.zon index ab05677..eb8ca9a 100644 --- a/build.zig.zon +++ b/build.zig.zon | |||
| @@ -1,7 +1,7 @@ | |||
| 1 | .{ | 1 | .{ |
| 2 | .name = .clap, | 2 | .name = .clap, |
| 3 | .version = "0.11.0", | 3 | .version = "0.11.0", |
| 4 | .minimum_zig_version = "0.16.0-dev.1859+212968c57", | 4 | .minimum_zig_version = "0.16.0-dev.2261+d6b3dd25a", |
| 5 | .fingerprint = 0x65f99e6f07a316a0, | 5 | .fingerprint = 0x65f99e6f07a316a0, |
| 6 | .paths = .{ | 6 | .paths = .{ |
| 7 | "clap", | 7 | "clap", |
| @@ -648,16 +648,17 @@ pub fn parse( | |||
| 648 | comptime Id: type, | 648 | comptime Id: type, |
| 649 | comptime params: []const Param(Id), | 649 | comptime params: []const Param(Id), |
| 650 | comptime value_parsers: anytype, | 650 | comptime value_parsers: anytype, |
| 651 | arguments: std.process.Args, | ||
| 651 | opt: ParseOptions, | 652 | opt: ParseOptions, |
| 652 | ) !Result(Id, params, value_parsers) { | 653 | ) !Result(Id, params, value_parsers) { |
| 653 | var arena = std.heap.ArenaAllocator.init(opt.allocator); | 654 | var arena = std.heap.ArenaAllocator.init(opt.allocator); |
| 654 | errdefer arena.deinit(); | 655 | errdefer arena.deinit(); |
| 655 | 656 | ||
| 656 | var iter = try std.process.ArgIterator.initWithAllocator(arena.allocator()); | 657 | var iter = try arguments.iterateAllocator(arena.allocator()); |
| 657 | const exe_arg = iter.next(); | 658 | const exe_arg = iter.next(); |
| 658 | 659 | ||
| 659 | const result = try parseEx(Id, params, value_parsers, &iter, .{ | 660 | const result = try parseEx(Id, params, value_parsers, &iter, .{ |
| 660 | // Let's reuse the arena from the `ArgIterator` since we already have it. | 661 | // Let's reuse the arena from the `Args.Iterator` since we already have it. |
| 661 | .allocator = arena.allocator(), | 662 | .allocator = arena.allocator(), |
| 662 | .diagnostic = opt.diagnostic, | 663 | .diagnostic = opt.diagnostic, |
| 663 | .assignment_separators = opt.assignment_separators, | 664 | .assignment_separators = opt.assignment_separators, |
diff --git a/example/help.zig b/example/help.zig index b07bc52..a41b261 100644 --- a/example/help.zig +++ b/example/help.zig | |||
| @@ -1,18 +1,11 @@ | |||
| 1 | pub fn main() !void { | 1 | pub fn main(init: std.process.Init) !void { |
| 2 | var gpa_state = std.heap.DebugAllocator(.{}){}; | ||
| 3 | const gpa = gpa_state.allocator(); | ||
| 4 | defer _ = gpa_state.deinit(); | ||
| 5 | |||
| 6 | var threaded: std.Io.Threaded = .init_single_threaded; | ||
| 7 | const io: std.Io = threaded.io(); | ||
| 8 | |||
| 9 | const params = comptime clap.parseParamsComptime( | 2 | const params = comptime clap.parseParamsComptime( |
| 10 | \\-h, --help Display this help and exit. | 3 | \\-h, --help Display this help and exit. |
| 11 | \\-v, --version Output version information and exit. | 4 | \\-v, --version Output version information and exit. |
| 12 | \\ | 5 | \\ |
| 13 | ); | 6 | ); |
| 14 | 7 | ||
| 15 | var res = try clap.parse(clap.Help, ¶ms, clap.parsers.default, .{ .allocator = gpa }); | 8 | var res = try clap.parse(clap.Help, ¶ms, clap.parsers.default, init.minimal.args, .{ .allocator = init.gpa }); |
| 16 | defer res.deinit(); | 9 | defer res.deinit(); |
| 17 | 10 | ||
| 18 | // `clap.help` is a function that can print a simple help message. It can print any `Param` | 11 | // `clap.help` is a function that can print a simple help message. It can print any `Param` |
| @@ -20,7 +13,7 @@ pub fn main() !void { | |||
| 20 | // The last argument contains options as to how `help` should print those parameters. Using | 13 | // The last argument contains options as to how `help` should print those parameters. Using |
| 21 | // `.{}` means the default options. | 14 | // `.{}` means the default options. |
| 22 | if (res.args.help != 0) | 15 | if (res.args.help != 0) |
| 23 | return clap.helpToFile(io, .stderr(), clap.Help, ¶ms, .{}); | 16 | return clap.helpToFile(init.io, .stderr(), clap.Help, ¶ms, .{}); |
| 24 | } | 17 | } |
| 25 | 18 | ||
| 26 | const clap = @import("clap"); | 19 | const clap = @import("clap"); |
diff --git a/example/simple-ex.zig b/example/simple-ex.zig index 0d90b69..c35b867 100644 --- a/example/simple-ex.zig +++ b/example/simple-ex.zig | |||
| @@ -1,11 +1,4 @@ | |||
| 1 | pub fn main() !void { | 1 | pub fn main(init: std.process.Init) !void { |
| 2 | var gpa_state = std.heap.DebugAllocator(.{}){}; | ||
| 3 | const gpa = gpa_state.allocator(); | ||
| 4 | defer _ = gpa_state.deinit(); | ||
| 5 | |||
| 6 | var threaded: std.Io.Threaded = .init_single_threaded; | ||
| 7 | const io: std.Io = threaded.io(); | ||
| 8 | |||
| 9 | // First we specify what parameters our program can take. | 2 | // First we specify what parameters our program can take. |
| 10 | // We can use `parseParamsComptime` to parse a string into an array of `Param(Help)`. | 3 | // We can use `parseParamsComptime` to parse a string into an array of `Param(Help)`. |
| 11 | const params = comptime clap.parseParamsComptime( | 4 | const params = comptime clap.parseParamsComptime( |
| @@ -28,15 +21,15 @@ pub fn main() !void { | |||
| 28 | }; | 21 | }; |
| 29 | 22 | ||
| 30 | var diag = clap.Diagnostic{}; | 23 | var diag = clap.Diagnostic{}; |
| 31 | var res = clap.parse(clap.Help, ¶ms, parsers, .{ | 24 | var res = clap.parse(clap.Help, ¶ms, parsers, init.minimal.args, .{ |
| 32 | .diagnostic = &diag, | 25 | .diagnostic = &diag, |
| 33 | .allocator = gpa, | 26 | .allocator = init.gpa, |
| 34 | // The assignment separator can be configured. `--number=1` and `--number:1` is now | 27 | // The assignment separator can be configured. `--number=1` and `--number:1` is now |
| 35 | // allowed. | 28 | // allowed. |
| 36 | .assignment_separators = "=:", | 29 | .assignment_separators = "=:", |
| 37 | }) catch |err| { | 30 | }) catch |err| { |
| 38 | // Report useful error and exit. | 31 | // Report useful error and exit. |
| 39 | try diag.reportToFile(io, .stderr(), err); | 32 | try diag.reportToFile(init.io, .stderr(), err); |
| 40 | return err; | 33 | return err; |
| 41 | }; | 34 | }; |
| 42 | defer res.deinit(); | 35 | defer res.deinit(); |
diff --git a/example/simple.zig b/example/simple.zig index a7772c6..7ed1d84 100644 --- a/example/simple.zig +++ b/example/simple.zig | |||
| @@ -1,11 +1,4 @@ | |||
| 1 | pub fn main() !void { | 1 | pub fn main(init: std.process.Init) !void { |
| 2 | var gpa_state = std.heap.DebugAllocator(.{}){}; | ||
| 3 | const gpa = gpa_state.allocator(); | ||
| 4 | defer _ = gpa_state.deinit(); | ||
| 5 | |||
| 6 | var threaded: std.Io.Threaded = .init_single_threaded; | ||
| 7 | const io: std.Io = threaded.io(); | ||
| 8 | |||
| 9 | // First we specify what parameters our program can take. | 2 | // First we specify what parameters our program can take. |
| 10 | // We can use `parseParamsComptime` to parse a string into an array of `Param(Help)`. | 3 | // We can use `parseParamsComptime` to parse a string into an array of `Param(Help)`. |
| 11 | const params = comptime clap.parseParamsComptime( | 4 | const params = comptime clap.parseParamsComptime( |
| @@ -20,12 +13,12 @@ pub fn main() !void { | |||
| 20 | // This is optional. You can also pass `.{}` to `clap.parse` if you don't | 13 | // This is optional. You can also pass `.{}` to `clap.parse` if you don't |
| 21 | // care about the extra information `Diagnostics` provides. | 14 | // care about the extra information `Diagnostics` provides. |
| 22 | var diag = clap.Diagnostic{}; | 15 | var diag = clap.Diagnostic{}; |
| 23 | var res = clap.parse(clap.Help, ¶ms, clap.parsers.default, .{ | 16 | var res = clap.parse(clap.Help, ¶ms, clap.parsers.default, init.minimal.args, .{ |
| 24 | .diagnostic = &diag, | 17 | .diagnostic = &diag, |
| 25 | .allocator = gpa, | 18 | .allocator = init.gpa, |
| 26 | }) catch |err| { | 19 | }) catch |err| { |
| 27 | // Report useful error and exit. | 20 | // Report useful error and exit. |
| 28 | try diag.reportToFile(io, .stderr(), err); | 21 | try diag.reportToFile(init.io, .stderr(), err); |
| 29 | return err; | 22 | return err; |
| 30 | }; | 23 | }; |
| 31 | defer res.deinit(); | 24 | defer res.deinit(); |
diff --git a/example/streaming-clap.zig b/example/streaming-clap.zig index 9d0a3ca..8198913 100644 --- a/example/streaming-clap.zig +++ b/example/streaming-clap.zig | |||
| @@ -1,11 +1,4 @@ | |||
| 1 | pub fn main() !void { | 1 | pub fn main(init: std.process.Init) !void { |
| 2 | var gpa_state = std.heap.DebugAllocator(.{}){}; | ||
| 3 | const gpa = gpa_state.allocator(); | ||
| 4 | defer _ = gpa_state.deinit(); | ||
| 5 | |||
| 6 | var threaded: std.Io.Threaded = .init_single_threaded; | ||
| 7 | const io: std.Io = threaded.io(); | ||
| 8 | |||
| 9 | // First we specify what parameters our program can take. | 2 | // First we specify what parameters our program can take. |
| 10 | const params = [_]clap.Param(u8){ | 3 | const params = [_]clap.Param(u8){ |
| 11 | .{ | 4 | .{ |
| @@ -20,7 +13,7 @@ pub fn main() !void { | |||
| 20 | .{ .id = 'f', .takes_value = .one }, | 13 | .{ .id = 'f', .takes_value = .one }, |
| 21 | }; | 14 | }; |
| 22 | 15 | ||
| 23 | var iter = try std.process.ArgIterator.initWithAllocator(gpa); | 16 | var iter = try init.minimal.args.iterateAllocator(init.gpa); |
| 24 | defer iter.deinit(); | 17 | defer iter.deinit(); |
| 25 | 18 | ||
| 26 | // Skip exe argument. | 19 | // Skip exe argument. |
| @@ -30,7 +23,7 @@ pub fn main() !void { | |||
| 30 | // This is optional. You can also leave the `diagnostic` field unset if you | 23 | // This is optional. You can also leave the `diagnostic` field unset if you |
| 31 | // don't care about the extra information `Diagnostic` provides. | 24 | // don't care about the extra information `Diagnostic` provides. |
| 32 | var diag = clap.Diagnostic{}; | 25 | var diag = clap.Diagnostic{}; |
| 33 | var parser = clap.streaming.Clap(u8, std.process.ArgIterator){ | 26 | var parser = clap.streaming.Clap(u8, std.process.Args.Iterator){ |
| 34 | .params = ¶ms, | 27 | .params = ¶ms, |
| 35 | .iter = &iter, | 28 | .iter = &iter, |
| 36 | .diagnostic = &diag, | 29 | .diagnostic = &diag, |
| @@ -39,7 +32,7 @@ pub fn main() !void { | |||
| 39 | // Because we use a streaming parser, we have to consume each argument parsed individually. | 32 | // Because we use a streaming parser, we have to consume each argument parsed individually. |
| 40 | while (parser.next() catch |err| { | 33 | while (parser.next() catch |err| { |
| 41 | // Report useful error and exit. | 34 | // Report useful error and exit. |
| 42 | try diag.reportToFile(io, .stderr(), err); | 35 | try diag.reportToFile(init.io, .stderr(), err); |
| 43 | return err; | 36 | return err; |
| 44 | }) |arg| { | 37 | }) |arg| { |
| 45 | // arg.param will point to the parameter which matched the argument. | 38 | // arg.param will point to the parameter which matched the argument. |
diff --git a/example/subcommands.zig b/example/subcommands.zig index bd8625b..0b14efe 100644 --- a/example/subcommands.zig +++ b/example/subcommands.zig | |||
| @@ -19,15 +19,8 @@ const main_params = clap.parseParamsComptime( | |||
| 19 | // get the return type of `clap.parse` and `clap.parseEx`. | 19 | // get the return type of `clap.parse` and `clap.parseEx`. |
| 20 | const MainArgs = clap.ResultEx(clap.Help, &main_params, main_parsers); | 20 | const MainArgs = clap.ResultEx(clap.Help, &main_params, main_parsers); |
| 21 | 21 | ||
| 22 | pub fn main() !void { | 22 | pub fn main(init: std.process.Init) !void { |
| 23 | var gpa_state = std.heap.DebugAllocator(.{}){}; | 23 | var iter = try init.minimal.args.iterateAllocator(init.gpa); |
| 24 | const gpa = gpa_state.allocator(); | ||
| 25 | defer _ = gpa_state.deinit(); | ||
| 26 | |||
| 27 | var threaded: std.Io.Threaded = .init_single_threaded; | ||
| 28 | const io: std.Io = threaded.io(); | ||
| 29 | |||
| 30 | var iter = try std.process.ArgIterator.initWithAllocator(gpa); | ||
| 31 | defer iter.deinit(); | 24 | defer iter.deinit(); |
| 32 | 25 | ||
| 33 | _ = iter.next(); | 26 | _ = iter.next(); |
| @@ -35,7 +28,7 @@ pub fn main() !void { | |||
| 35 | var diag = clap.Diagnostic{}; | 28 | var diag = clap.Diagnostic{}; |
| 36 | var res = clap.parseEx(clap.Help, &main_params, main_parsers, &iter, .{ | 29 | var res = clap.parseEx(clap.Help, &main_params, main_parsers, &iter, .{ |
| 37 | .diagnostic = &diag, | 30 | .diagnostic = &diag, |
| 38 | .allocator = gpa, | 31 | .allocator = init.gpa, |
| 39 | 32 | ||
| 40 | // Terminate the parsing of arguments after parsing the first positional (0 is passed | 33 | // Terminate the parsing of arguments after parsing the first positional (0 is passed |
| 41 | // here because parsed positionals are, like slices and arrays, indexed starting at 0). | 34 | // here because parsed positionals are, like slices and arrays, indexed starting at 0). |
| @@ -44,7 +37,7 @@ pub fn main() !void { | |||
| 44 | // not fully consumed. It can then be reused to parse the arguments for subcommands. | 37 | // not fully consumed. It can then be reused to parse the arguments for subcommands. |
| 45 | .terminating_positional = 0, | 38 | .terminating_positional = 0, |
| 46 | }) catch |err| { | 39 | }) catch |err| { |
| 47 | try diag.reportToFile(io, .stderr(), err); | 40 | try diag.reportToFile(init.io, .stderr(), err); |
| 48 | return err; | 41 | return err; |
| 49 | }; | 42 | }; |
| 50 | defer res.deinit(); | 43 | defer res.deinit(); |
| @@ -55,11 +48,11 @@ pub fn main() !void { | |||
| 55 | const command = res.positionals[0] orelse return error.MissingCommand; | 48 | const command = res.positionals[0] orelse return error.MissingCommand; |
| 56 | switch (command) { | 49 | switch (command) { |
| 57 | .help => std.debug.print("--help\n", .{}), | 50 | .help => std.debug.print("--help\n", .{}), |
| 58 | .math => try mathMain(io, gpa, &iter, res), | 51 | .math => try mathMain(init.io, init.gpa, &iter, res), |
| 59 | } | 52 | } |
| 60 | } | 53 | } |
| 61 | 54 | ||
| 62 | fn mathMain(io: std.Io, gpa: std.mem.Allocator, iter: *std.process.ArgIterator, main_args: MainArgs) !void { | 55 | fn mathMain(io: std.Io, gpa: std.mem.Allocator, iter: *std.process.Args.Iterator, main_args: MainArgs) !void { |
| 63 | // The parent arguments are not used here, but there are cases where it might be useful, so | 56 | // The parent arguments are not used here, but there are cases where it might be useful, so |
| 64 | // this example shows how to pass the arguments around. | 57 | // this example shows how to pass the arguments around. |
| 65 | _ = main_args; | 58 | _ = main_args; |
diff --git a/example/usage.zig b/example/usage.zig index 2d51cc4..c620092 100644 --- a/example/usage.zig +++ b/example/usage.zig | |||
| @@ -1,11 +1,4 @@ | |||
| 1 | pub fn main() !void { | 1 | pub fn main(init: std.process.Init) !void { |
| 2 | var gpa_state = std.heap.DebugAllocator(.{}){}; | ||
| 3 | const gpa = gpa_state.allocator(); | ||
| 4 | defer _ = gpa_state.deinit(); | ||
| 5 | |||
| 6 | var threaded: std.Io.Threaded = .init_single_threaded; | ||
| 7 | const io: std.Io = threaded.io(); | ||
| 8 | |||
| 9 | const params = comptime clap.parseParamsComptime( | 2 | const params = comptime clap.parseParamsComptime( |
| 10 | \\-h, --help Display this help and exit. | 3 | \\-h, --help Display this help and exit. |
| 11 | \\-v, --version Output version information and exit. | 4 | \\-v, --version Output version information and exit. |
| @@ -13,13 +6,13 @@ pub fn main() !void { | |||
| 13 | \\ | 6 | \\ |
| 14 | ); | 7 | ); |
| 15 | 8 | ||
| 16 | var res = try clap.parse(clap.Help, ¶ms, clap.parsers.default, .{ .allocator = gpa }); | 9 | var res = try clap.parse(clap.Help, ¶ms, clap.parsers.default, init.minimal.args, .{ .allocator = init.gpa }); |
| 17 | defer res.deinit(); | 10 | defer res.deinit(); |
| 18 | 11 | ||
| 19 | // `clap.usageToFile` is a function that can print a simple usage string. It can print any | 12 | // `clap.usageToFile` is a function that can print a simple usage string. It can print any |
| 20 | // `Param` where `Id` has a `value` method (`Param(Help)` is one such parameter). | 13 | // `Param` where `Id` has a `value` method (`Param(Help)` is one such parameter). |
| 21 | if (res.args.help != 0) | 14 | if (res.args.help != 0) |
| 22 | return clap.usageToFile(io, .stdout(), clap.Help, ¶ms); | 15 | return clap.usageToFile(init.io, .stdout(), clap.Help, ¶ms); |
| 23 | } | 16 | } |
| 24 | 17 | ||
| 25 | const clap = @import("clap"); | 18 | const clap = @import("clap"); |