diff options
| author | 2026-01-25 09:27:04 +1100 | |
|---|---|---|
| committer | 2026-01-24 23:27:04 +0100 | |
| commit | 1d3d273524e3c180f015ff1e93c83075e4634e2c (patch) | |
| tree | 55693ab9cf02ecc4c3f171882259ef41adab4fb7 /example | |
| parent | fix: Regression in simple-ex example (diff) | |
| download | zig-clap-1d3d273524e3c180f015ff1e93c83075e4634e2c.tar.gz zig-clap-1d3d273524e3c180f015ff1e93c83075e4634e2c.tar.xz zig-clap-1d3d273524e3c180f015ff1e93c83075e4634e2c.zip | |
Diffstat (limited to '')
| -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 |
6 files changed, 24 insertions, 66 deletions
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"); |