summaryrefslogtreecommitdiff
path: root/README.md
diff options
context:
space:
mode:
Diffstat (limited to 'README.md')
-rw-r--r--README.md54
1 files changed, 26 insertions, 28 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 {