summaryrefslogtreecommitdiff
path: root/README.md
diff options
context:
space:
mode:
authorGravatar Jimmi Holst Christensen2024-10-22 17:46:47 +0200
committerGravatar Jimmi Holst Christensen2024-10-22 17:48:34 +0200
commite73b56aa4bcb7e53144ef96ee978f2a19b32669d (patch)
tree0ab5e3d426e25d2ad9d2e0cd0015fc010d9ea182 /README.md
parentfeat: Add `terminating_positional` to `clap.ParseOptions` (diff)
downloadzig-clap-e73b56aa4bcb7e53144ef96ee978f2a19b32669d.tar.gz
zig-clap-e73b56aa4bcb7e53144ef96ee978f2a19b32669d.tar.xz
zig-clap-e73b56aa4bcb7e53144ef96ee978f2a19b32669d.zip
refactor: Always access using full namespace
This is my new preferred style of programming Zig :)
Diffstat (limited to 'README.md')
-rw-r--r--README.md77
1 files changed, 33 insertions, 44 deletions
diff --git a/README.md b/README.md
index bc38b58..c6e14e9 100644
--- a/README.md
+++ b/README.md
@@ -56,12 +56,6 @@ Note that Zig autodoc is in beta; the website may be broken or incomplete.
56The simplest way to use this library is to just call the `clap.parse` function. 56The simplest way to use this library is to just call the `clap.parse` function.
57 57
58```zig 58```zig
59const clap = @import("clap");
60const std = @import("std");
61
62const debug = std.debug;
63const io = std.io;
64
65pub fn main() !void { 59pub fn main() !void {
66 var gpa = std.heap.GeneralPurposeAllocator(.{}){}; 60 var gpa = std.heap.GeneralPurposeAllocator(.{}){};
67 defer _ = gpa.deinit(); 61 defer _ = gpa.deinit();
@@ -85,21 +79,24 @@ pub fn main() !void {
85 .allocator = gpa.allocator(), 79 .allocator = gpa.allocator(),
86 }) catch |err| { 80 }) catch |err| {
87 // Report useful error and exit 81 // Report useful error and exit
88 diag.report(io.getStdErr().writer(), err) catch {}; 82 diag.report(std.io.getStdErr().writer(), err) catch {};
89 return err; 83 return err;
90 }; 84 };
91 defer res.deinit(); 85 defer res.deinit();
92 86
93 if (res.args.help != 0) 87 if (res.args.help != 0)
94 debug.print("--help\n", .{}); 88 std.debug.print("--help\n", .{});
95 if (res.args.number) |n| 89 if (res.args.number) |n|
96 debug.print("--number = {}\n", .{n}); 90 std.debug.print("--number = {}\n", .{n});
97 for (res.args.string) |s| 91 for (res.args.string) |s|
98 debug.print("--string = {s}\n", .{s}); 92 std.debug.print("--string = {s}\n", .{s});
99 for (res.positionals) |pos| 93 for (res.positionals) |pos|
100 debug.print("{s}\n", .{pos}); 94 std.debug.print("{s}\n", .{pos});
101} 95}
102 96
97const clap = @import("clap");
98const std = @import("std");
99
103``` 100```
104 101
105The 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
@@ -114,13 +111,6 @@ contains a parser that returns `usize`. You can pass in something other than
114`clap.parsers.default` if you want some other mapping. 111`clap.parsers.default` if you want some other mapping.
115 112
116```zig 113```zig
117const clap = @import("clap");
118const std = @import("std");
119
120const debug = std.debug;
121const io = std.io;
122const process = std.process;
123
124pub fn main() !void { 114pub fn main() !void {
125 var gpa = std.heap.GeneralPurposeAllocator(.{}){}; 115 var gpa = std.heap.GeneralPurposeAllocator(.{}){};
126 defer _ = gpa.deinit(); 116 defer _ = gpa.deinit();
@@ -154,23 +144,26 @@ pub fn main() !void {
154 // allowed. 144 // allowed.
155 .assignment_separators = "=:", 145 .assignment_separators = "=:",
156 }) catch |err| { 146 }) catch |err| {
157 diag.report(io.getStdErr().writer(), err) catch {}; 147 diag.report(std.io.getStdErr().writer(), err) catch {};
158 return err; 148 return err;
159 }; 149 };
160 defer res.deinit(); 150 defer res.deinit();
161 151
162 if (res.args.help != 0) 152 if (res.args.help != 0)
163 debug.print("--help\n", .{}); 153 std.debug.print("--help\n", .{});
164 if (res.args.number) |n| 154 if (res.args.number) |n|
165 debug.print("--number = {}\n", .{n}); 155 std.debug.print("--number = {}\n", .{n});
166 if (res.args.answer) |a| 156 if (res.args.answer) |a|
167 debug.print("--answer = {s}\n", .{@tagName(a)}); 157 std.debug.print("--answer = {s}\n", .{@tagName(a)});
168 for (res.args.string) |s| 158 for (res.args.string) |s|
169 debug.print("--string = {s}\n", .{s}); 159 std.debug.print("--string = {s}\n", .{s});
170 for (res.positionals) |pos| 160 for (res.positionals) |pos|
171 debug.print("{s}\n", .{pos}); 161 std.debug.print("{s}\n", .{pos});
172} 162}
173 163
164const clap = @import("clap");
165const std = @import("std");
166
174``` 167```
175 168
176### `streaming.Clap` 169### `streaming.Clap`
@@ -179,13 +172,6 @@ The `streaming.Clap` is the base of all the other parsers. It's a streaming pars
179`args.Iterator` to provide it with arguments lazily. 172`args.Iterator` to provide it with arguments lazily.
180 173
181```zig 174```zig
182const clap = @import("clap");
183const std = @import("std");
184
185const debug = std.debug;
186const io = std.io;
187const process = std.process;
188
189pub fn main() !void { 175pub fn main() !void {
190 const allocator = std.heap.page_allocator; 176 const allocator = std.heap.page_allocator;
191 177
@@ -203,7 +189,7 @@ pub fn main() !void {
203 .{ .id = 'f', .takes_value = .one }, 189 .{ .id = 'f', .takes_value = .one },
204 }; 190 };
205 191
206 var iter = try process.ArgIterator.initWithAllocator(allocator); 192 var iter = try std.process.ArgIterator.initWithAllocator(allocator);
207 defer iter.deinit(); 193 defer iter.deinit();
208 194
209 // Skip exe argument 195 // Skip exe argument
@@ -213,7 +199,7 @@ pub fn main() !void {
213 // This is optional. You can also leave the `diagnostic` field unset if you 199 // This is optional. You can also leave the `diagnostic` field unset if you
214 // don't care about the extra information `Diagnostic` provides. 200 // don't care about the extra information `Diagnostic` provides.
215 var diag = clap.Diagnostic{}; 201 var diag = clap.Diagnostic{};
216 var parser = clap.streaming.Clap(u8, process.ArgIterator){ 202 var parser = clap.streaming.Clap(u8, std.process.ArgIterator){
217 .params = &params, 203 .params = &params,
218 .iter = &iter, 204 .iter = &iter,
219 .diagnostic = &diag, 205 .diagnostic = &diag,
@@ -222,23 +208,26 @@ pub fn main() !void {
222 // Because we use a streaming parser, we have to consume each argument parsed individually. 208 // Because we use a streaming parser, we have to consume each argument parsed individually.
223 while (parser.next() catch |err| { 209 while (parser.next() catch |err| {
224 // Report useful error and exit 210 // Report useful error and exit
225 diag.report(io.getStdErr().writer(), err) catch {}; 211 diag.report(std.io.getStdErr().writer(), err) catch {};
226 return err; 212 return err;
227 }) |arg| { 213 }) |arg| {
228 // arg.param will point to the parameter which matched the argument. 214 // arg.param will point to the parameter which matched the argument.
229 switch (arg.param.id) { 215 switch (arg.param.id) {
230 'h' => debug.print("Help!\n", .{}), 216 'h' => std.debug.print("Help!\n", .{}),
231 'n' => debug.print("--number = {s}\n", .{arg.value.?}), 217 'n' => std.debug.print("--number = {s}\n", .{arg.value.?}),
232 218
233 // arg.value == null, if arg.param.takes_value == .none. 219 // arg.value == null, if arg.param.takes_value == .none.
234 // Otherwise, arg.value is the value passed with the argument, such as "-a=10" 220 // Otherwise, arg.value is the value passed with the argument, such as "-a=10"
235 // or "-a 10". 221 // or "-a 10".
236 'f' => debug.print("{s}\n", .{arg.value.?}), 222 'f' => std.debug.print("{s}\n", .{arg.value.?}),
237 else => unreachable, 223 else => unreachable,
238 } 224 }
239 } 225 }
240} 226}
241 227
228const clap = @import("clap");
229const std = @import("std");
230
242``` 231```
243 232
244Currently, this parser is the only parser that allows an array of `Param` that 233Currently, this parser is the only parser that allows an array of `Param` that
@@ -252,9 +241,6 @@ in the output. `HelpOptions` is passed to `help` to control how the help message
252printed. 241printed.
253 242
254```zig 243```zig
255const clap = @import("clap");
256const std = @import("std");
257
258pub fn main() !void { 244pub fn main() !void {
259 var gpa = std.heap.GeneralPurposeAllocator(.{}){}; 245 var gpa = std.heap.GeneralPurposeAllocator(.{}){};
260 defer _ = gpa.deinit(); 246 defer _ = gpa.deinit();
@@ -271,13 +257,16 @@ pub fn main() !void {
271 defer res.deinit(); 257 defer res.deinit();
272 258
273 // `clap.help` is a function that can print a simple help message. It can print any `Param` 259 // `clap.help` is a function that can print a simple help message. It can print any `Param`
274 // where `Id` has a `describtion` and `value` method (`Param(Help)` is one such parameter). 260 // where `Id` has a `description` and `value` method (`Param(Help)` is one such parameter).
275 // The last argument contains options as to how `help` should print those parameters. Using 261 // The last argument contains options as to how `help` should print those parameters. Using
276 // `.{}` means the default options. 262 // `.{}` means the default options.
277 if (res.args.help != 0) 263 if (res.args.help != 0)
278 return clap.help(std.io.getStdErr().writer(), clap.Help, &params, .{}); 264 return clap.help(std.io.getStdErr().writer(), clap.Help, &params, .{});
279} 265}
280 266
267const clap = @import("clap");
268const std = @import("std");
269
281``` 270```
282 271
283``` 272```
@@ -295,9 +284,6 @@ The `usage` prints a small abbreviated version of the help message. It expects t
295to have a `value` method so it can provide that in the output. 284to have a `value` method so it can provide that in the output.
296 285
297```zig 286```zig
298const clap = @import("clap");
299const std = @import("std");
300
301pub fn main() !void { 287pub fn main() !void {
302 var gpa = std.heap.GeneralPurposeAllocator(.{}){}; 288 var gpa = std.heap.GeneralPurposeAllocator(.{}){};
303 defer _ = gpa.deinit(); 289 defer _ = gpa.deinit();
@@ -320,6 +306,9 @@ pub fn main() !void {
320 return clap.usage(std.io.getStdErr().writer(), clap.Help, &params); 306 return clap.usage(std.io.getStdErr().writer(), clap.Help, &params);
321} 307}
322 308
309const clap = @import("clap");
310const std = @import("std");
311
323``` 312```
324 313
325``` 314```