summaryrefslogtreecommitdiff
path: root/README.md
diff options
context:
space:
mode:
Diffstat (limited to 'README.md')
-rw-r--r--README.md137
1 files changed, 76 insertions, 61 deletions
diff --git a/README.md b/README.md
index 75b4b19..55a43d9 100644
--- a/README.md
+++ b/README.md
@@ -34,75 +34,98 @@ pub fn main() !void {
34 // First we specify what parameters our program can take. 34 // First we specify what parameters our program can take.
35 // We can use `parseParam` to parse a string to a `Param(Help)` 35 // We can use `parseParam` to parse a string to a `Param(Help)`
36 const params = comptime [_]clap.Param(clap.Help){ 36 const params = comptime [_]clap.Param(clap.Help){
37 clap.parseParam("-h, --help Display this help and exit. ") catch unreachable, 37 clap.parseParam("-h, --help Display this help and exit.") catch unreachable,
38 clap.parseParam("-n, --number <NUM> An option parameter, which takes a value.") catch unreachable, 38 clap.parseParam("-n, --number <usize> An option parameter, which takes a value.") catch unreachable,
39 clap.parseParam("-s, --string <STR>... An option parameter which can be specified multiple times.") catch unreachable, 39 clap.parseParam("-s, --string <str>... An option parameter which can be specified multiple times.") catch unreachable,
40 clap.parseParam("<POS>...") catch unreachable, 40 clap.parseParam("<str>...") catch unreachable,
41 }; 41 };
42 42
43 // Initalize our diagnostics, which can be used for reporting useful errors. 43 // Initalize our diagnostics, which can be used for reporting useful errors.
44 // This is optional. You can also pass `.{}` to `clap.parse` if you don't 44 // This is optional. You can also pass `.{}` to `clap.parse` if you don't
45 // care about the extra information `Diagnostics` provides. 45 // care about the extra information `Diagnostics` provides.
46 var diag = clap.Diagnostic{}; 46 var diag = clap.Diagnostic{};
47 var args = clap.parse(clap.Help, &params, .{ .diagnostic = &diag }) catch |err| { 47 var res = clap.parse(clap.Help, &params, clap.parsers.default, .{
48 .diagnostic = &diag,
49 }) catch |err| {
48 // Report useful error and exit 50 // Report useful error and exit
49 diag.report(io.getStdErr().writer(), err) catch {}; 51 diag.report(io.getStdErr().writer(), err) catch {};
50 return err; 52 return err;
51 }; 53 };
52 defer args.deinit(); 54 defer res.deinit();
53 55
54 if (args.flag("--help")) 56 if (res.args.help)
55 debug.print("--help\n", .{}); 57 debug.print("--help\n", .{});
56 if (args.option("--number")) |n| 58 if (res.args.number) |n|
57 debug.print("--number = {s}\n", .{n}); 59 debug.print("--number = {}\n", .{n});
58 for (args.options("--string")) |s| 60 for (res.args.string) |s|
59 debug.print("--string = {s}\n", .{s}); 61 debug.print("--string = {s}\n", .{s});
60 for (args.positionals()) |pos| 62 for (res.positionals) |pos|
61 debug.print("{s}\n", .{pos}); 63 debug.print("{s}\n", .{pos});
62} 64}
63 65
64``` 66```
65 67
66The data structure returned has lookup speed on par with array access (`arr[i]`) and validates 68The result will contain an `args` field and a `positionals` field. `args` will have one field
67that the strings you pass to `option`, `options` and `flag` are actually parameters that the 69for each none positional parameter of your program. The name of the field will be the longest
68program can take: 70name of the parameter.
71
72The fields in `args` are typed. The type is based on the name of the value the parameter takes.
73Since `--number` takes a `usize` the field `res.args.number` has the type `usize`.
74
75Note that this is only the case because `clap.parsers.default` has a field called `usize` which
76contains a parser that returns `usize`. You can pass in something other than `clap.parsers.default`
77if you want some other mapping.
69 78
70```zig 79```zig
71const clap = @import("clap"); 80const clap = @import("clap");
72const std = @import("std"); 81const std = @import("std");
73 82
83const debug = std.debug;
84const io = std.io;
85const process = std.process;
86
74pub fn main() !void { 87pub fn main() !void {
88 // First we specify what parameters our program can take.
89 // We can use `parseParam` to parse a string to a `Param(Help)`
75 const params = comptime [_]clap.Param(clap.Help){ 90 const params = comptime [_]clap.Param(clap.Help){
76 clap.parseParam("-h, --help Display this help and exit.") catch unreachable, 91 clap.parseParam("-h, --help Display this help and exit.") catch unreachable,
92 clap.parseParam("-n, --number <INT> An option parameter, which takes a value.") catch unreachable,
93 clap.parseParam("-s, --string <STR>... An option parameter which can be specified multiple times.") catch unreachable,
94 clap.parseParam("<FILE>...") catch unreachable,
77 }; 95 };
78 96
79 var args = try clap.parse(clap.Help, &params, .{}); 97 // Declare our own parsers which are used to map the argument strings to other
80 defer args.deinit(); 98 // types.
99 const parsers = comptime .{
100 .STR = clap.parsers.string,
101 .FILE = clap.parsers.string,
102 .INT = clap.parsers.int(usize, 10),
103 };
81 104
82 _ = args.flag("--helps"); 105 var diag = clap.Diagnostic{};
83} 106 var res = clap.parse(clap.Help, &params, parsers, .{
107 .diagnostic = &diag,
108 }) catch |err| {
109 diag.report(io.getStdErr().writer(), err) catch {};
110 return err;
111 };
112 defer res.deinit();
84 113
85``` 114 if (res.args.help)
115 debug.print("--help\n", .{});
116 if (res.args.number) |n|
117 debug.print("--number = {}\n", .{n});
118 for (res.args.string) |s|
119 debug.print("--string = {s}\n", .{s});
120 for (res.positionals) |pos|
121 debug.print("{s}\n", .{pos});
122}
86 123
87``` 124```
88zig-clap/clap/comptime.zig:109:17: error: --helps is not a parameter.
89 @compileError(name ++ " is not a parameter.");
90 ^
91zig-clap/clap/comptime.zig:77:45: note: called from here
92 const param = comptime findParam(name);
93 ^
94zig-clap/clap.zig:238:31: note: called from here
95 return a.clap.flag(name);
96 ^
97zig-clap/example/simple-error.zig:16:18: note: called from here
98 _ = args.flag("--helps");
99```
100
101There is also a `parseEx` variant that takes an argument iterator.
102 125
103### `StreamingClap` 126### `streaming.Clap`
104 127
105The `StreamingClap` is the base of all the other parsers. It's a streaming parser that uses an 128The `streaming.Clap` is the base of all the other parsers. It's a streaming parser that uses an
106`args.Iterator` to provide it with arguments lazily. 129`args.Iterator` to provide it with arguments lazily.
107 130
108```zig 131```zig
@@ -140,7 +163,7 @@ pub fn main() !void {
140 // This is optional. You can also leave the `diagnostic` field unset if you 163 // This is optional. You can also leave the `diagnostic` field unset if you
141 // don't care about the extra information `Diagnostic` provides. 164 // don't care about the extra information `Diagnostic` provides.
142 var diag = clap.Diagnostic{}; 165 var diag = clap.Diagnostic{};
143 var parser = clap.StreamingClap(u8, process.ArgIterator){ 166 var parser = clap.streaming.Clap(u8, process.ArgIterator){
144 .params = &params, 167 .params = &params,
145 .iter = &iter, 168 .iter = &iter,
146 .diagnostic = &diag, 169 .diagnostic = &diag,
@@ -173,8 +196,9 @@ is generated at runtime.
173 196
174### `help` 197### `help`
175 198
176The `help`, `helpEx` and `helpFull` are functions for printing a simple list of all parameters the 199The `help` prints a simple list of all parameters the program can take. It expects the
177program can take. 200`Id` to have a `description` method and an `value` method so that it can provide that
201in the output.
178 202
179```zig 203```zig
180const clap = @import("clap"); 204const clap = @import("clap");
@@ -186,14 +210,14 @@ pub fn main() !void {
186 clap.parseParam("-v, --version Output version information and exit.") catch unreachable, 210 clap.parseParam("-v, --version Output version information and exit.") catch unreachable,
187 }; 211 };
188 212
189 var args = try clap.parse(clap.Help, &params, .{}); 213 var res = try clap.parse(clap.Help, &params, clap.parsers.default, .{});
190 defer args.deinit(); 214 defer res.deinit();
191 215
192 // clap.help is a function that can print a simple help message, given a 216 // clap.help is a function that can print a simple help message, given a
193 // slice of Param(Help). There is also a helpEx, which can print a 217 // slice of Param(Help). There is also a helpEx, which can print a
194 // help message for any Param, but it is more verbose to call. 218 // help message for any Param, but it is more verbose to call.
195 if (args.flag("--help")) 219 if (res.args.help)
196 return clap.help(std.io.getStdErr().writer(), &params); 220 return clap.help(std.io.getStdErr().writer(), clap.Help, &params);
197} 221}
198 222
199``` 223```
@@ -204,19 +228,10 @@ $ zig-out/bin/help --help
204 -v, --version Output version information and exit. 228 -v, --version Output version information and exit.
205``` 229```
206 230
207The `help` functions are the simplest to call. It only takes an `OutStream` and a slice of
208`Param(Help)`.
209
210The `helpEx` is the generic version of `help`. It can print a help message for any
211`Param` give that the caller provides functions for getting the help and value strings.
212
213The `helpFull` is even more generic, allowing the functions that get the help and value strings
214to return errors and take a context as a parameter.
215
216### `usage` 231### `usage`
217 232
218The `usage`, `usageEx` and `usageFull` are functions for printing a small abbreviated version 233The `usage` prints a small abbreviated version of the help message. It expects the `Id`
219of the help message. 234to have a `value` method so it can provide that in the output.
220 235
221```zig 236```zig
222const clap = @import("clap"); 237const clap = @import("clap");
@@ -224,19 +239,19 @@ const std = @import("std");
224 239
225pub fn main() !void { 240pub fn main() !void {
226 const params = comptime [_]clap.Param(clap.Help){ 241 const params = comptime [_]clap.Param(clap.Help){
227 clap.parseParam("-h, --help Display this help and exit. ") catch unreachable, 242 clap.parseParam("-h, --help Display this help and exit.") catch unreachable,
228 clap.parseParam("-v, --version Output version information and exit. ") catch unreachable, 243 clap.parseParam("-v, --version Output version information and exit.") catch unreachable,
229 clap.parseParam(" --value <N> An option parameter, which takes a value.") catch unreachable, 244 clap.parseParam(" --value <str> An option parameter, which takes a value.") catch unreachable,
230 }; 245 };
231 246
232 var args = try clap.parse(clap.Help, &params, .{}); 247 var res = try clap.parse(clap.Help, &params, clap.parsers.default, .{});
233 defer args.deinit(); 248 defer res.deinit();
234 249
235 // clap.usage is a function that can print a simple usage message, given a 250 // clap.usage is a function that can print a simple usage message, given a
236 // slice of Param(Help). There is also a usageEx, which can print a 251 // slice of Param(Help). There is also a usageEx, which can print a
237 // usage message for any Param, but it is more verbose to call. 252 // usage message for any Param, but it is more verbose to call.
238 if (args.flag("--help")) 253 if (res.args.help)
239 return clap.usage(std.io.getStdErr().writer(), &params); 254 return clap.usage(std.io.getStdErr().writer(), clap.Help, &params);
240} 255}
241 256
242``` 257```