summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--README.md54
-rw-r--r--example/README.md.template28
-rw-r--r--example/simple-ex.zig2
-rw-r--r--example/simple.zig4
-rw-r--r--example/streaming-clap.zig4
-rw-r--r--example/subcommands.zig16
6 files changed, 52 insertions, 56 deletions
diff --git a/README.md b/README.md
index 6cc8ca9..81b6ad0 100644
--- a/README.md
+++ b/README.md
@@ -45,8 +45,8 @@ exe.root_module.addImport("clap", clap.module("clap"));
45 45
46## API Reference 46## API Reference
47 47
48Automatically generated API Reference for the project 48Automatically generated API Reference for the project can be found at https://Hejsil.github.io/
49can be found at https://Hejsil.github.io/zig-clap. 49zig-clap.
50Note that Zig autodoc is in beta; the website may be broken or incomplete. 50Note that Zig autodoc is in beta; the website may be broken or incomplete.
51 51
52## Examples 52## Examples
@@ -61,7 +61,7 @@ pub fn main() !void {
61 defer _ = gpa.deinit(); 61 defer _ = gpa.deinit();
62 62
63 // First we specify what parameters our program can take. 63 // First we specify what parameters our program can take.
64 // We can use `parseParamsComptime` to parse a string into an array of `Param(Help)` 64 // We can use `parseParamsComptime` to parse a string into an array of `Param(Help)`.
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 \\-n, --number <usize> An option parameter, which takes a value. 67 \\-n, --number <usize> An option parameter, which takes a value.
@@ -78,7 +78,7 @@ pub fn main() !void {
78 .diagnostic = &diag, 78 .diagnostic = &diag,
79 .allocator = gpa.allocator(), 79 .allocator = gpa.allocator(),
80 }) catch |err| { 80 }) catch |err| {
81 // Report useful error and exit 81 // Report useful error and exit.
82 diag.report(std.io.getStdErr().writer(), err) catch {}; 82 diag.report(std.io.getStdErr().writer(), err) catch {};
83 return err; 83 return err;
84 }; 84 };
@@ -99,16 +99,16 @@ const std = @import("std");
99 99
100``` 100```
101 101
102The result will contain an `args` field and a `positionals` field. `args` will have one field 102The result will contain an `args` field and a `positionals` field. `args` will have one field for
103for each none positional parameter of your program. The name of the field will be the longest 103each non-positional parameter of your program. The name of the field will be the longest name of the
104name of the parameter. `positionals` be a tuple with one field for each positional parameter. 104parameter. `positionals` will be a tuple with one field for each positional parameter.
105 105
106The fields in `args` and `postionals` are typed. The type is based on the name of the value the 106The fields in `args` and `postionals` are typed. The type is based on the name of the value the
107parameter takes. Since `--number` takes a `usize` the field `res.args.number` has the type `usize`. 107parameter takes. Since `--number` takes a `usize` the field `res.args.number` has the type `usize`.
108 108
109Note that this is only the case because `clap.parsers.default` has a field called `usize` which 109Note that this is only the case because `clap.parsers.default` has a field called `usize` which
110contains a parser that returns `usize`. You can pass in something other than 110contains a parser that returns `usize`. You can pass in something other than `clap.parsers.default`
111`clap.parsers.default` if you want some other mapping. 111if you want some other mapping.
112 112
113```zig 113```zig
114pub fn main() !void { 114pub fn main() !void {
@@ -116,7 +116,7 @@ pub fn main() !void {
116 defer _ = gpa.deinit(); 116 defer _ = gpa.deinit();
117 117
118 // First we specify what parameters our program can take. 118 // First we specify what parameters our program can take.
119 // We can use `parseParamsComptime` to parse a string into an array of `Param(Help)` 119 // We can use `parseParamsComptime` to parse a string into an array of `Param(Help)`.
120 const params = comptime clap.parseParamsComptime( 120 const params = comptime clap.parseParamsComptime(
121 \\-h, --help Display this help and exit. 121 \\-h, --help Display this help and exit.
122 \\-n, --number <INT> An option parameter, which takes a value. 122 \\-n, --number <INT> An option parameter, which takes a value.
@@ -172,7 +172,7 @@ There is an option for `clap.parse` and `clap.parseEx` called `terminating_posit
172for users of `clap` to implement subcommands in their cli application: 172for users of `clap` to implement subcommands in their cli application:
173 173
174```zig 174```zig
175// These are our subcommands 175// These are our subcommands.
176const SubCommands = enum { 176const SubCommands = enum {
177 help, 177 help,
178 math, 178 math,
@@ -182,7 +182,7 @@ const main_parsers = .{
182 .command = clap.parsers.enumeration(SubCommands), 182 .command = clap.parsers.enumeration(SubCommands),
183}; 183};
184 184
185// The parameters for main. Parameters for the subcommands are specified further down 185// The parameters for `main`. Parameters for the subcommands are specified further down.
186const main_params = clap.parseParamsComptime( 186const main_params = clap.parseParamsComptime(
187 \\-h, --help Display this help and exit. 187 \\-h, --help Display this help and exit.
188 \\<command> 188 \\<command>
@@ -190,7 +190,7 @@ const main_params = clap.parseParamsComptime(
190); 190);
191 191
192// To pass around arguments returned by clap, `clap.Result` and `clap.ResultEx` can be used to 192// To pass around arguments returned by clap, `clap.Result` and `clap.ResultEx` can be used to
193// get the return type of `clap.parse` and `clap.parseEx` 193// get the return type of `clap.parse` and `clap.parseEx`.
194const MainArgs = clap.ResultEx(clap.Help, &main_params, main_parsers); 194const MainArgs = clap.ResultEx(clap.Help, &main_params, main_parsers);
195 195
196pub fn main() !void { 196pub fn main() !void {
@@ -212,7 +212,7 @@ pub fn main() !void {
212 // here because parsed positionals are, like slices and arrays, indexed starting at 0). 212 // here because parsed positionals are, like slices and arrays, indexed starting at 0).
213 // 213 //
214 // This will terminate the parsing after parsing the subcommand enum and leave `iter` 214 // This will terminate the parsing after parsing the subcommand enum and leave `iter`
215 // not fully consumed. It can then be reused to parse the arguments for subcommands 215 // not fully consumed. It can then be reused to parse the arguments for subcommands.
216 .terminating_positional = 0, 216 .terminating_positional = 0,
217 }) catch |err| { 217 }) catch |err| {
218 diag.report(std.io.getStdErr().writer(), err) catch {}; 218 diag.report(std.io.getStdErr().writer(), err) catch {};
@@ -231,11 +231,11 @@ pub fn main() !void {
231} 231}
232 232
233fn mathMain(gpa: std.mem.Allocator, iter: *std.process.ArgIterator, main_args: MainArgs) !void { 233fn mathMain(gpa: std.mem.Allocator, iter: *std.process.ArgIterator, main_args: MainArgs) !void {
234 // The parent arguments are not used it, but there are cases where it might be useful, so 234 // The parent arguments are not used here, but there are cases where it might be useful, so
235 // the example shows how to pass the arguments around. 235 // this example shows how to pass the arguments around.
236 _ = main_args; 236 _ = main_args;
237 237
238 // The parameters for the subcommand 238 // The parameters for the subcommand.
239 const params = comptime clap.parseParamsComptime( 239 const params = comptime clap.parseParamsComptime(
240 \\-h, --help Display this help and exit. 240 \\-h, --help Display this help and exit.
241 \\-a, --add Add the two numbers 241 \\-a, --add Add the two numbers
@@ -245,7 +245,7 @@ fn mathMain(gpa: std.mem.Allocator, iter: *std.process.ArgIterator, main_args: M
245 \\ 245 \\
246 ); 246 );
247 247
248 // Here we pass the partially parsed argument iterator 248 // Here we pass the partially parsed argument iterator.
249 var diag = clap.Diagnostic{}; 249 var diag = clap.Diagnostic{};
250 var res = clap.parseEx(clap.Help, &params, clap.parsers.default, iter, .{ 250 var res = clap.parseEx(clap.Help, &params, clap.parsers.default, iter, .{
251 .diagnostic = &diag, 251 .diagnostic = &diag,
@@ -297,7 +297,7 @@ pub fn main() !void {
297 var iter = try std.process.ArgIterator.initWithAllocator(allocator); 297 var iter = try std.process.ArgIterator.initWithAllocator(allocator);
298 defer iter.deinit(); 298 defer iter.deinit();
299 299
300 // Skip exe argument 300 // Skip exe argument.
301 _ = iter.next(); 301 _ = iter.next();
302 302
303 // Initialize our diagnostics, which can be used for reporting useful errors. 303 // Initialize our diagnostics, which can be used for reporting useful errors.
@@ -312,7 +312,7 @@ pub fn main() !void {
312 312
313 // Because we use a streaming parser, we have to consume each argument parsed individually. 313 // Because we use a streaming parser, we have to consume each argument parsed individually.
314 while (parser.next() catch |err| { 314 while (parser.next() catch |err| {
315 // Report useful error and exit 315 // Report useful error and exit.
316 diag.report(std.io.getStdErr().writer(), err) catch {}; 316 diag.report(std.io.getStdErr().writer(), err) catch {};
317 return err; 317 return err;
318 }) |arg| { 318 }) |arg| {
@@ -335,15 +335,13 @@ const std = @import("std");
335 335
336``` 336```
337 337
338Currently, this parser is the only parser that allows an array of `Param` that 338Currently, this parser is the only parser that allows an array of `Param` that is generated at runtime.
339is generated at runtime.
340 339
341### `help` 340### `help`
342 341
343The `help` prints a simple list of all parameters the program can take. It expects the 342`help` prints a simple list of all parameters the program can take. It expects the `Id` to have a
344`Id` to have a `description` method and an `value` method so that it can provide that 343`description` method and an `value` method so that it can provide that in the output. `HelpOptions`
345in the output. `HelpOptions` is passed to `help` to control how the help message is 344is passed to `help` to control how the help message is printed.
346printed.
347 345
348```zig 346```zig
349pub fn main() !void { 347pub fn main() !void {
@@ -385,8 +383,8 @@ $ zig-out/bin/help --help
385 383
386### `usage` 384### `usage`
387 385
388The `usage` prints a small abbreviated version of the help message. It expects the `Id` 386`usage` prints a small abbreviated version of the help message. It expects the `Id` to have a
389to have a `value` method so it can provide that in the output. 387`value` method so it can provide that in the output.
390 388
391```zig 389```zig
392pub fn main() !void { 390pub fn main() !void {
diff --git a/example/README.md.template b/example/README.md.template
index bcd3774..bbf5cb8 100644
--- a/example/README.md.template
+++ b/example/README.md.template
@@ -45,8 +45,8 @@ exe.root_module.addImport("clap", clap.module("clap"));
45 45
46## API Reference 46## API Reference
47 47
48Automatically generated API Reference for the project 48Automatically generated API Reference for the project can be found at https://Hejsil.github.io/
49can be found at https://Hejsil.github.io/zig-clap. 49zig-clap.
50Note that Zig autodoc is in beta; the website may be broken or incomplete. 50Note that Zig autodoc is in beta; the website may be broken or incomplete.
51 51
52## Examples 52## Examples
@@ -59,16 +59,16 @@ The simplest way to use this library is to just call the `clap.parse` function.
59{s} 59{s}
60``` 60```
61 61
62The result will contain an `args` field and a `positionals` field. `args` will have one field 62The result will contain an `args` field and a `positionals` field. `args` will have one field for
63for each none positional parameter of your program. The name of the field will be the longest 63each non-positional parameter of your program. The name of the field will be the longest name of the
64name of the parameter. `positionals` be a tuple with one field for each positional parameter. 64parameter. `positionals` will be a tuple with one field for each positional parameter.
65 65
66The fields in `args` and `postionals` are typed. The type is based on the name of the value the 66The fields in `args` and `postionals` are typed. The type is based on the name of the value the
67parameter takes. Since `--number` takes a `usize` the field `res.args.number` has the type `usize`. 67parameter takes. Since `--number` takes a `usize` the field `res.args.number` has the type `usize`.
68 68
69Note that this is only the case because `clap.parsers.default` has a field called `usize` which 69Note that this is only the case because `clap.parsers.default` has a field called `usize` which
70contains a parser that returns `usize`. You can pass in something other than 70contains a parser that returns `usize`. You can pass in something other than `clap.parsers.default`
71`clap.parsers.default` if you want some other mapping. 71if you want some other mapping.
72 72
73```zig 73```zig
74{s} 74{s}
@@ -92,15 +92,13 @@ The `streaming.Clap` is the base of all the other parsers. It's a streaming pars
92{s} 92{s}
93``` 93```
94 94
95Currently, this parser is the only parser that allows an array of `Param` that 95Currently, this parser is the only parser that allows an array of `Param` that is generated at runtime.
96is generated at runtime.
97 96
98### `help` 97### `help`
99 98
100The `help` prints a simple list of all parameters the program can take. It expects the 99`help` prints a simple list of all parameters the program can take. It expects the `Id` to have a
101`Id` to have a `description` method and an `value` method so that it can provide that 100`description` method and an `value` method so that it can provide that in the output. `HelpOptions`
102in the output. `HelpOptions` is passed to `help` to control how the help message is 101is passed to `help` to control how the help message is printed.
103printed.
104 102
105```zig 103```zig
106{s} 104{s}
@@ -117,8 +115,8 @@ $ zig-out/bin/help --help
117 115
118### `usage` 116### `usage`
119 117
120The `usage` prints a small abbreviated version of the help message. It expects the `Id` 118`usage` prints a small abbreviated version of the help message. It expects the `Id` to have a
121to have a `value` method so it can provide that in the output. 119`value` method so it can provide that in the output.
122 120
123```zig 121```zig
124{s} 122{s}
diff --git a/example/simple-ex.zig b/example/simple-ex.zig
index 154e486..22f657f 100644
--- a/example/simple-ex.zig
+++ b/example/simple-ex.zig
@@ -3,7 +3,7 @@ pub fn main() !void {
3 defer _ = gpa.deinit(); 3 defer _ = gpa.deinit();
4 4
5 // First we specify what parameters our program can take. 5 // First we specify what parameters our program can take.
6 // We can use `parseParamsComptime` to parse a string into an array of `Param(Help)` 6 // We can use `parseParamsComptime` to parse a string into an array of `Param(Help)`.
7 const params = comptime clap.parseParamsComptime( 7 const params = comptime clap.parseParamsComptime(
8 \\-h, --help Display this help and exit. 8 \\-h, --help Display this help and exit.
9 \\-n, --number <INT> An option parameter, which takes a value. 9 \\-n, --number <INT> An option parameter, which takes a value.
diff --git a/example/simple.zig b/example/simple.zig
index 74157a6..2b7bf0a 100644
--- a/example/simple.zig
+++ b/example/simple.zig
@@ -3,7 +3,7 @@ pub fn main() !void {
3 defer _ = gpa.deinit(); 3 defer _ = gpa.deinit();
4 4
5 // First we specify what parameters our program can take. 5 // First we specify what parameters our program can take.
6 // We can use `parseParamsComptime` to parse a string into an array of `Param(Help)` 6 // We can use `parseParamsComptime` to parse a string into an array of `Param(Help)`.
7 const params = comptime clap.parseParamsComptime( 7 const params = comptime clap.parseParamsComptime(
8 \\-h, --help Display this help and exit. 8 \\-h, --help Display this help and exit.
9 \\-n, --number <usize> An option parameter, which takes a value. 9 \\-n, --number <usize> An option parameter, which takes a value.
@@ -20,7 +20,7 @@ pub fn main() !void {
20 .diagnostic = &diag, 20 .diagnostic = &diag,
21 .allocator = gpa.allocator(), 21 .allocator = gpa.allocator(),
22 }) catch |err| { 22 }) catch |err| {
23 // Report useful error and exit 23 // Report useful error and exit.
24 diag.report(std.io.getStdErr().writer(), err) catch {}; 24 diag.report(std.io.getStdErr().writer(), err) catch {};
25 return err; 25 return err;
26 }; 26 };
diff --git a/example/streaming-clap.zig b/example/streaming-clap.zig
index 2a20bcb..054c401 100644
--- a/example/streaming-clap.zig
+++ b/example/streaming-clap.zig
@@ -18,7 +18,7 @@ pub fn main() !void {
18 var iter = try std.process.ArgIterator.initWithAllocator(allocator); 18 var iter = try std.process.ArgIterator.initWithAllocator(allocator);
19 defer iter.deinit(); 19 defer iter.deinit();
20 20
21 // Skip exe argument 21 // Skip exe argument.
22 _ = iter.next(); 22 _ = iter.next();
23 23
24 // Initialize our diagnostics, which can be used for reporting useful errors. 24 // Initialize our diagnostics, which can be used for reporting useful errors.
@@ -33,7 +33,7 @@ pub fn main() !void {
33 33
34 // Because we use a streaming parser, we have to consume each argument parsed individually. 34 // Because we use a streaming parser, we have to consume each argument parsed individually.
35 while (parser.next() catch |err| { 35 while (parser.next() catch |err| {
36 // Report useful error and exit 36 // Report useful error and exit.
37 diag.report(std.io.getStdErr().writer(), err) catch {}; 37 diag.report(std.io.getStdErr().writer(), err) catch {};
38 return err; 38 return err;
39 }) |arg| { 39 }) |arg| {
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,