summaryrefslogtreecommitdiff
path: root/README.md
diff options
context:
space:
mode:
authorGravatar Ivan Stepanov2025-07-20 21:40:56 +0200
committerGravatar Jimmi Holst Christensen2025-07-21 18:52:54 +0200
commit6f103922a8133ba11773e6ad9a52e26e1d99b3e7 (patch)
tree66bc5fa5fc36f20575be1e1a59ff890ab9c8b23e /README.md
parentchore: Update setup-zig to v2 (diff)
downloadzig-clap-6f103922a8133ba11773e6ad9a52e26e1d99b3e7.tar.gz
zig-clap-6f103922a8133ba11773e6ad9a52e26e1d99b3e7.tar.xz
zig-clap-6f103922a8133ba11773e6ad9a52e26e1d99b3e7.zip
Update to Zig 0.15.0-dev.1147
Diffstat (limited to 'README.md')
-rw-r--r--README.md87
1 files changed, 56 insertions, 31 deletions
diff --git a/README.md b/README.md
index fbc19e4..2e64264 100644
--- a/README.md
+++ b/README.md
@@ -5,8 +5,9 @@ A simple and easy to use command line argument parser library for Zig.
5## Installation 5## Installation
6 6
7Developers tend to either use 7Developers tend to either use
8* The latest tagged release of Zig 8
9* The latest build of Zigs master branch 9- The latest tagged release of Zig
10- The latest build of Zigs master branch
10 11
11Depending on which developer you are, you need to run different `zig fetch` commands: 12Depending on which developer you are, you need to run different `zig fetch` commands:
12 13
@@ -29,22 +30,22 @@ exe.root_module.addImport("clap", clap.module("clap"));
29 30
30## Features 31## Features
31 32
32* Short arguments `-a` 33- Short arguments `-a`
33 * Chaining `-abc` where `a` and `b` does not take values. 34 - Chaining `-abc` where `a` and `b` does not take values.
34 * Multiple specifications are tallied (e.g. `-v -v`). 35 - Multiple specifications are tallied (e.g. `-v -v`).
35* Long arguments `--long` 36- Long arguments `--long`
36* Supports both passing values using spacing and `=` (`-a 100`, `-a=100`) 37- Supports both passing values using spacing and `=` (`-a 100`, `-a=100`)
37 * Short args also support passing values with no spacing or `=` (`-a100`) 38 - Short args also support passing values with no spacing or `=` (`-a100`)
38 * This all works with chaining (`-ba 100`, `-ba=100`, `-ba100`) 39 - This all works with chaining (`-ba 100`, `-ba=100`, `-ba100`)
39* Supports options that can be specified multiple times (`-e 1 -e 2 -e 3`) 40- Supports options that can be specified multiple times (`-e 1 -e 2 -e 3`)
40* Print help message from parameter specification. 41- Print help message from parameter specification.
41* Parse help message to parameter specification. 42- Parse help message to parameter specification.
42 43
43## API Reference 44## API Reference
44 45
45Automatically generated API Reference for the project can be found at 46Automatically generated API Reference for the project can be found at
46https://Hejsil.github.io/zig-clap. Note that Zig autodoc is in beta; the website 47https://Hejsil.github.io/zig-clap. Note that Zig autodoc is in beta; the website may be broken or
47may be broken or incomplete. 48incomplete.
48 49
49## Examples 50## Examples
50 51
@@ -76,7 +77,10 @@ pub fn main() !void {
76 .allocator = gpa.allocator(), 77 .allocator = gpa.allocator(),
77 }) catch |err| { 78 }) catch |err| {
78 // Report useful error and exit. 79 // Report useful error and exit.
79 diag.report(std.io.getStdErr().writer(), err) catch {}; 80 var buf: [1024]u8 = undefined;
81 var stderr = std.fs.File.stderr().writer(&buf);
82 diag.report(&stderr.interface, err) catch {};
83 try stderr.interface.flush();
80 return err; 84 return err;
81 }; 85 };
82 defer res.deinit(); 86 defer res.deinit();
@@ -93,7 +97,6 @@ pub fn main() !void {
93 97
94const clap = @import("clap"); 98const clap = @import("clap");
95const std = @import("std"); 99const std = @import("std");
96
97``` 100```
98 101
99The result will contain an `args` field and a `positionals` field. `args` will have one field for 102The result will contain an `args` field and a `positionals` field. `args` will have one field for
@@ -141,7 +144,10 @@ pub fn main() !void {
141 // allowed. 144 // allowed.
142 .assignment_separators = "=:", 145 .assignment_separators = "=:",
143 }) catch |err| { 146 }) catch |err| {
144 diag.report(std.io.getStdErr().writer(), err) catch {}; 147 var buf: [1024]u8 = undefined;
148 var stderr = std.fs.File.stderr().writer(&buf);
149 diag.report(&stderr.interface, err) catch {};
150 try stderr.interface.flush();
145 return err; 151 return err;
146 }; 152 };
147 defer res.deinit(); 153 defer res.deinit();
@@ -160,7 +166,6 @@ pub fn main() !void {
160 166
161const clap = @import("clap"); 167const clap = @import("clap");
162const std = @import("std"); 168const std = @import("std");
163
164``` 169```
165 170
166### Subcommands 171### Subcommands
@@ -212,7 +217,10 @@ pub fn main() !void {
212 // not fully consumed. It can then be reused to parse the arguments for subcommands. 217 // not fully consumed. It can then be reused to parse the arguments for subcommands.
213 .terminating_positional = 0, 218 .terminating_positional = 0,
214 }) catch |err| { 219 }) catch |err| {
215 diag.report(std.io.getStdErr().writer(), err) catch {}; 220 var buf: [1024]u8 = undefined;
221 var stderr = std.fs.File.stderr().writer(&buf);
222 diag.report(&stderr.interface, err) catch {};
223 try stderr.interface.flush();
216 return err; 224 return err;
217 }; 225 };
218 defer res.deinit(); 226 defer res.deinit();
@@ -248,8 +256,11 @@ fn mathMain(gpa: std.mem.Allocator, iter: *std.process.ArgIterator, main_args: M
248 .diagnostic = &diag, 256 .diagnostic = &diag,
249 .allocator = gpa, 257 .allocator = gpa,
250 }) catch |err| { 258 }) catch |err| {
251 diag.report(std.io.getStdErr().writer(), err) catch {}; 259 var buf: [1024]u8 = undefined;
252 return err; 260 var stderr = std.fs.File.stderr().writer(&buf);
261 diag.report(&stderr.interface, err) catch {};
262 try stderr.interface.flush();
263 return err; // propagate error
253 }; 264 };
254 defer res.deinit(); 265 defer res.deinit();
255 266
@@ -265,7 +276,6 @@ fn mathMain(gpa: std.mem.Allocator, iter: *std.process.ArgIterator, main_args: M
265 276
266const clap = @import("clap"); 277const clap = @import("clap");
267const std = @import("std"); 278const std = @import("std");
268
269``` 279```
270 280
271### `streaming.Clap` 281### `streaming.Clap`
@@ -310,7 +320,10 @@ pub fn main() !void {
310 // Because we use a streaming parser, we have to consume each argument parsed individually. 320 // Because we use a streaming parser, we have to consume each argument parsed individually.
311 while (parser.next() catch |err| { 321 while (parser.next() catch |err| {
312 // Report useful error and exit. 322 // Report useful error and exit.
313 diag.report(std.io.getStdErr().writer(), err) catch {}; 323 var buf: [1024]u8 = undefined;
324 var stderr = std.fs.File.stderr().writer(&buf);
325 diag.report(&stderr.interface, err) catch {};
326 try stderr.interface.flush();
314 return err; 327 return err;
315 }) |arg| { 328 }) |arg| {
316 // arg.param will point to the parameter which matched the argument. 329 // arg.param will point to the parameter which matched the argument.
@@ -329,10 +342,17 @@ pub fn main() !void {
329 342
330const clap = @import("clap"); 343const clap = @import("clap");
331const std = @import("std"); 344const std = @import("std");
345```
332 346
333``` 347```
348$ zig-out/bin/streaming-clap --help --number=1 f=10
349Help!
350--number = 1
351f=10
352```
334 353
335Currently, this parser is the only parser that allows an array of `Param` that is generated at runtime. 354Currently, this parser is the only parser that allows an array of `Param` that is generated at
355runtime.
336 356
337### `help` 357### `help`
338 358
@@ -360,13 +380,16 @@ pub fn main() !void {
360 // where `Id` has a `description` and `value` method (`Param(Help)` is one such parameter). 380 // where `Id` has a `description` and `value` method (`Param(Help)` is one such parameter).
361 // The last argument contains options as to how `help` should print those parameters. Using 381 // The last argument contains options as to how `help` should print those parameters. Using
362 // `.{}` means the default options. 382 // `.{}` means the default options.
363 if (res.args.help != 0) 383 if (res.args.help != 0) {
364 return clap.help(std.io.getStdErr().writer(), clap.Help, &params, .{}); 384 var buf: [1024]u8 = undefined;
385 var stderr = std.fs.File.stderr().writer(&buf);
386 try clap.help(&stderr.interface, clap.Help, &params, .{});
387 return stderr.interface.flush();
388 }
365} 389}
366 390
367const clap = @import("clap"); 391const clap = @import("clap");
368const std = @import("std"); 392const std = @import("std");
369
370``` 393```
371 394
372``` 395```
@@ -402,17 +425,19 @@ pub fn main() !void {
402 425
403 // `clap.usage` is a function that can print a simple help message. It can print any `Param` 426 // `clap.usage` is a function that can print a simple help message. It can print any `Param`
404 // where `Id` has a `value` method (`Param(Help)` is one such parameter). 427 // where `Id` has a `value` method (`Param(Help)` is one such parameter).
405 if (res.args.help != 0) 428 if (res.args.help != 0) {
406 return clap.usage(std.io.getStdErr().writer(), clap.Help, &params); 429 var buf: [1024]u8 = undefined;
430 var stderr = std.fs.File.stderr().writer(&buf);
431 try clap.usage(&stderr.interface, clap.Help, &params);
432 return stderr.interface.flush();
433 }
407} 434}
408 435
409const clap = @import("clap"); 436const clap = @import("clap");
410const std = @import("std"); 437const std = @import("std");
411
412``` 438```
413 439
414``` 440```
415$ zig-out/bin/usage --help 441$ zig-out/bin/usage --help
416[-hv] [--value <str>] 442[-hv] [--value <str>]
417``` 443```
418