summaryrefslogtreecommitdiff
path: root/example/subcommands.zig
diff options
context:
space:
mode:
authorGravatar JustAnotherCodemonkey2024-11-25 07:57:42 +0000
committerGravatar GitHub2024-11-25 08:57:42 +0100
commit560c8dd72ab25d556123846254040c5bf9ad3ba4 (patch)
treea5c57b24b67fdc1fc71abf444d422062453921be /example/subcommands.zig
parentrefactor: switch on signedness (diff)
downloadzig-clap-560c8dd72ab25d556123846254040c5bf9ad3ba4.tar.gz
zig-clap-560c8dd72ab25d556123846254040c5bf9ad3ba4.tar.xz
zig-clap-560c8dd72ab25d556123846254040c5bf9ad3ba4.zip
Patch up README and fix comments in example files. (#143)
The readme was also reflowed to 100 cols. I probably should have put that in a separate commit but oh well. The comments were not subjected to reflowing (yet).
Diffstat (limited to '')
-rw-r--r--example/subcommands.zig16
1 files changed, 8 insertions, 8 deletions
diff --git a/example/subcommands.zig b/example/subcommands.zig
index fcd4fee..8223f31 100644
--- a/example/subcommands.zig
+++ b/example/subcommands.zig
@@ -1,4 +1,4 @@
1// These are our subcommands 1// These are our subcommands.
2const SubCommands = enum { 2const SubCommands = enum {
3 help, 3 help,
4 math, 4 math,
@@ -8,7 +8,7 @@ const main_parsers = .{
8 .command = clap.parsers.enumeration(SubCommands), 8 .command = clap.parsers.enumeration(SubCommands),
9}; 9};
10 10
11// The parameters for main. Parameters for the subcommands are specified further down 11// The parameters for `main`. Parameters for the subcommands are specified further down.
12const main_params = clap.parseParamsComptime( 12const main_params = clap.parseParamsComptime(
13 \\-h, --help Display this help and exit. 13 \\-h, --help Display this help and exit.
14 \\<command> 14 \\<command>
@@ -16,7 +16,7 @@ const main_params = clap.parseParamsComptime(
16); 16);
17 17
18// To pass around arguments returned by clap, `clap.Result` and `clap.ResultEx` can be used to 18// To pass around arguments returned by clap, `clap.Result` and `clap.ResultEx` can be used to
19// get the return type of `clap.parse` and `clap.parseEx` 19// get the return type of `clap.parse` and `clap.parseEx`.
20const MainArgs = clap.ResultEx(clap.Help, &main_params, main_parsers); 20const MainArgs = clap.ResultEx(clap.Help, &main_params, main_parsers);
21 21
22pub fn main() !void { 22pub fn main() !void {
@@ -38,7 +38,7 @@ pub fn main() !void {
38 // here because parsed positionals are, like slices and arrays, indexed starting at 0). 38 // here because parsed positionals are, like slices and arrays, indexed starting at 0).
39 // 39 //
40 // This will terminate the parsing after parsing the subcommand enum and leave `iter` 40 // This will terminate the parsing after parsing the subcommand enum and leave `iter`
41 // not fully consumed. It can then be reused to parse the arguments for subcommands 41 // not fully consumed. It can then be reused to parse the arguments for subcommands.
42 .terminating_positional = 0, 42 .terminating_positional = 0,
43 }) catch |err| { 43 }) catch |err| {
44 diag.report(std.io.getStdErr().writer(), err) catch {}; 44 diag.report(std.io.getStdErr().writer(), err) catch {};
@@ -57,11 +57,11 @@ pub fn main() !void {
57} 57}
58 58
59fn mathMain(gpa: std.mem.Allocator, iter: *std.process.ArgIterator, main_args: MainArgs) !void { 59fn mathMain(gpa: std.mem.Allocator, iter: *std.process.ArgIterator, main_args: MainArgs) !void {
60 // The parent arguments are not used it, but there are cases where it might be useful, so 60 // The parent arguments are not used here, but there are cases where it might be useful, so
61 // the example shows how to pass the arguments around. 61 // this example shows how to pass the arguments around.
62 _ = main_args; 62 _ = main_args;
63 63
64 // The parameters for the subcommand 64 // The parameters for the subcommand.
65 const params = comptime clap.parseParamsComptime( 65 const params = comptime clap.parseParamsComptime(
66 \\-h, --help Display this help and exit. 66 \\-h, --help Display this help and exit.
67 \\-a, --add Add the two numbers 67 \\-a, --add Add the two numbers
@@ -71,7 +71,7 @@ fn mathMain(gpa: std.mem.Allocator, iter: *std.process.ArgIterator, main_args: M
71 \\ 71 \\
72 ); 72 );
73 73
74 // Here we pass the partially parsed argument iterator 74 // Here we pass the partially parsed argument iterator.
75 var diag = clap.Diagnostic{}; 75 var diag = clap.Diagnostic{};
76 var res = clap.parseEx(clap.Help, &params, clap.parsers.default, iter, .{ 76 var res = clap.parseEx(clap.Help, &params, clap.parsers.default, iter, .{
77 .diagnostic = &diag, 77 .diagnostic = &diag,