summaryrefslogtreecommitdiff
path: root/README.md
diff options
context:
space:
mode:
authorGravatar Jimmi Holst Christensen2022-02-25 19:40:00 +0100
committerGravatar Jimmi Holst Christensen2022-02-25 19:40:00 +0100
commitcfaac64c404fb1c2e892880410aa3b7dd881ea58 (patch)
tree0dca149e43daaaef41f55fa61375ab361c36a39c /README.md
parentFix minor typos in README.md (diff)
downloadzig-clap-cfaac64c404fb1c2e892880410aa3b7dd881ea58.tar.gz
zig-clap-cfaac64c404fb1c2e892880410aa3b7dd881ea58.tar.xz
zig-clap-cfaac64c404fb1c2e892880410aa3b7dd881ea58.zip
Change clap into generating a struct
This changes - `.flag`, `.option`, `.options` and `.positionals` are now just fields you access. - Move the current `clap.parse` and friends into `clap.untyped.parse` - This is in preperation for `clap.typed.parse`
Diffstat (limited to 'README.md')
-rw-r--r--README.md48
1 files changed, 24 insertions, 24 deletions
diff --git a/README.md b/README.md
index 75b4b19..347250e 100644
--- a/README.md
+++ b/README.md
@@ -34,30 +34,30 @@ 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.untyped.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.untyped.parseParam("-n, --number <NUM> 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.untyped.parseParam("-s, --string <STR>... An option parameter which can be specified multiple times.") catch unreachable,
40 clap.parseParam("<POS>...") catch unreachable, 40 clap.untyped.parseParam("<POS>...") 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.untyped.parse(clap.Help, &params, .{ .diagnostic = &diag }) catch |err| {
48 // Report useful error and exit 48 // Report useful error and exit
49 diag.report(io.getStdErr().writer(), err) catch {}; 49 diag.report(io.getStdErr().writer(), err) catch {};
50 return err; 50 return err;
51 }; 51 };
52 defer args.deinit(); 52 defer res.deinit();
53 53
54 if (args.flag("--help")) 54 if (res.args.help)
55 debug.print("--help\n", .{}); 55 debug.print("--help\n", .{});
56 if (args.option("--number")) |n| 56 if (res.args.number) |n|
57 debug.print("--number = {s}\n", .{n}); 57 debug.print("--number = {s}\n", .{n});
58 for (args.options("--string")) |s| 58 for (res.args.string) |s|
59 debug.print("--string = {s}\n", .{s}); 59 debug.print("--string = {s}\n", .{s});
60 for (args.positionals()) |pos| 60 for (res.positionals) |pos|
61 debug.print("{s}\n", .{pos}); 61 debug.print("{s}\n", .{pos});
62} 62}
63 63
@@ -100,9 +100,9 @@ zig-clap/example/simple-error.zig:16:18: note: called from here
100 100
101There is also a `parseEx` variant that takes an argument iterator. 101There is also a `parseEx` variant that takes an argument iterator.
102 102
103### `StreamingClap` 103### `streaming.Clap`
104 104
105The `StreamingClap` is the base of all the other parsers. It's a streaming parser that uses an 105The `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. 106`args.Iterator` to provide it with arguments lazily.
107 107
108```zig 108```zig
@@ -140,7 +140,7 @@ pub fn main() !void {
140 // This is optional. You can also leave the `diagnostic` field unset if you 140 // This is optional. You can also leave the `diagnostic` field unset if you
141 // don't care about the extra information `Diagnostic` provides. 141 // don't care about the extra information `Diagnostic` provides.
142 var diag = clap.Diagnostic{}; 142 var diag = clap.Diagnostic{};
143 var parser = clap.StreamingClap(u8, process.ArgIterator){ 143 var parser = clap.streaming.Clap(u8, process.ArgIterator){
144 .params = &params, 144 .params = &params,
145 .iter = &iter, 145 .iter = &iter,
146 .diagnostic = &diag, 146 .diagnostic = &diag,
@@ -182,17 +182,17 @@ const std = @import("std");
182 182
183pub fn main() !void { 183pub fn main() !void {
184 const params = comptime [_]clap.Param(clap.Help){ 184 const params = comptime [_]clap.Param(clap.Help){
185 clap.parseParam("-h, --help Display this help and exit. ") catch unreachable, 185 clap.untyped.parseParam("-h, --help Display this help and exit. ") catch unreachable,
186 clap.parseParam("-v, --version Output version information and exit.") catch unreachable, 186 clap.untyped.parseParam("-v, --version Output version information and exit.") catch unreachable,
187 }; 187 };
188 188
189 var args = try clap.parse(clap.Help, &params, .{}); 189 var res = try clap.untyped.parse(clap.Help, &params, .{});
190 defer args.deinit(); 190 defer res.deinit();
191 191
192 // clap.help is a function that can print a simple help message, given a 192 // 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 193 // 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. 194 // help message for any Param, but it is more verbose to call.
195 if (args.flag("--help")) 195 if (res.args.help)
196 return clap.help(std.io.getStdErr().writer(), &params); 196 return clap.help(std.io.getStdErr().writer(), &params);
197} 197}
198 198
@@ -224,18 +224,18 @@ const std = @import("std");
224 224
225pub fn main() !void { 225pub fn main() !void {
226 const params = comptime [_]clap.Param(clap.Help){ 226 const params = comptime [_]clap.Param(clap.Help){
227 clap.parseParam("-h, --help Display this help and exit. ") catch unreachable, 227 clap.untyped.parseParam("-h, --help Display this help and exit.") catch unreachable,
228 clap.parseParam("-v, --version Output version information and exit. ") catch unreachable, 228 clap.untyped.parseParam("-v, --version Output version information and exit.") catch unreachable,
229 clap.parseParam(" --value <N> An option parameter, which takes a value.") catch unreachable, 229 clap.untyped.parseParam(" --value <N> An option parameter, which takes a value.") catch unreachable,
230 }; 230 };
231 231
232 var args = try clap.parse(clap.Help, &params, .{}); 232 var res = try clap.untyped.parse(clap.Help, &params, .{});
233 defer args.deinit(); 233 defer res.deinit();
234 234
235 // clap.usage is a function that can print a simple usage message, given a 235 // 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 236 // 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. 237 // usage message for any Param, but it is more verbose to call.
238 if (args.flag("--help")) 238 if (res.args.help)
239 return clap.usage(std.io.getStdErr().writer(), &params); 239 return clap.usage(std.io.getStdErr().writer(), &params);
240} 240}
241 241