summaryrefslogtreecommitdiff
path: root/README.md
diff options
context:
space:
mode:
authorGravatar Jimmi Holst Christensen2018-11-14 14:06:20 +0100
committerGravatar Jimmi Holst Christensen2018-11-14 14:06:20 +0100
commitc564b168785e740c37f47da4942825b25cb8b4ec (patch)
tree880252c7a63bdc91f23ba5e13b593d4ca9ad8277 /README.md
parentZig fmt (diff)
downloadzig-clap-c564b168785e740c37f47da4942825b25cb8b4ec.tar.gz
zig-clap-c564b168785e740c37f47da4942825b25cb8b4ec.tar.xz
zig-clap-c564b168785e740c37f47da4942825b25cb8b4ec.zip
Restructured and make StreamingClap simpler
* Also added a ComptimeClap
Diffstat (limited to '')
-rw-r--r--README.md100
1 files changed, 96 insertions, 4 deletions
diff --git a/README.md b/README.md
index f4c3766..92f53c2 100644
--- a/README.md
+++ b/README.md
@@ -1,17 +1,109 @@
1# zig-clap 1# zig-clap
2 2
3A simple and easy to use command line argument parser library for Zig. 3A simple and easy to use command line argument parser library for Zig.
4It's ment as a thin layer of abstraction over parsing arguments which allows
5for further abstraction on top, such as filling in a `HashMap`.
6 4
7## Features 5## Features
8 6
9See [example](https://github.com/Hejsil/zig-clap/blob/38a51948069f405864ab327826b5975a6d0c93a8/test.zig#L200-L247).
10* Short arguments `-a` 7* Short arguments `-a`
11 * Chaining `-abc` where `a` and `b` does not take values. 8 * Chaining `-abc` where `a` and `b` does not take values.
12* Long arguments `--long` 9* Long arguments `--long`
13* Bare arguments `bare`
14* Supports both passing values using spacing and `=` (`-a 100`, `-a=100`) 10* Supports both passing values using spacing and `=` (`-a 100`, `-a=100`)
15 * Short args also support passing values with no spacing or `=` (`-a100`) 11 * Short args also support passing values with no spacing or `=` (`-a100`)
16 * This all works with chaining (`-ba 100`, `-ba=100`, `-ba100`) 12 * This all works with chaining (`-ba 100`, `-ba=100`, `-ba100`)
17 13
14## Examples
15
16### `StreamingClap`
17
18The `StreamingClap` is base of all the other parsers. It's a streaming parser that uses an
19`args.Iterator` to provide it with arguments lazily.
20
21```rust
22const params = []clap.Param(u8){
23 clap.Param(u8).init('h', false, clap.Names.prefix("help")),
24 clap.Param(u8).init('n', true, clap.Names.prefix("number")),
25 clap.Param(u8).init('f', true, clap.Names.positional()),
26};
27
28var os_iter = clap.args.OsIterator.init(allocator);
29const iter = &os_iter.iter;
30defer os_iter.deinit();
31
32const exe = try iter.next();
33
34var parser = clap.StreamingClap(u8, clap.args.OsIterator.Error).init(params, iter);
35
36while (try parser.next()) |arg| {
37 switch (arg.param.id) {
38 'h' => debug.warn("Help!\n"),
39 'n' => debug.warn("--number = {}\n", arg.value.?),
40 'f' => debug.warn("{}\n", arg.value.?),
41 else => unreachable,
42 }
43 }
44```
45
46### `ComptimeClap`
47
48The `ComptimeClap` is a wrapper for `StreamingClap`, which parses all the arguments and makes
49them available through three functions (`flag`, `option`, `positionals`).
50
51```rust
52const params = comptime []clap.Param(void){
53 clap.Param(void).init({}, false, clap.Names.prefix("help")),
54 clap.Param(void).init({}, true, clap.Names.prefix("number")),
55 clap.Param(void).init({}, true, clap.Names.positional()),
56};
57
58var os_iter = clap.args.OsIterator.init(allocator);
59const iter = &os_iter.iter;
60defer os_iter.deinit();
61
62const exe = try iter.next();
63
64var args = try clap.ComptimeClap(void, params).parse(allocator, clap.args.OsIterator.Error, iter);
65defer args.deinit();
66
67if (args.flag("--help"))
68 debug.warn("Help!\n");
69if (args.option("--number")) |n|
70 debug.warn("--number = {}\n", n);
71for (args.positionals()) |pos|
72 debug.warn("{}\n", pos);
73```
74
75The data structure returned from this parser has lookup speed on par with array access (`arr[i]`)
76and validates that the strings you pass to `option` and `flag` are actually parameters that the
77program can take:
78
79```rust
80const params = comptime []clap.Param(void){
81 clap.Param(void).init({}, false, clap.Names.prefix("help")),
82};
83
84var os_iter = clap.args.OsIterator.init(allocator);
85const iter = &os_iter.iter;
86defer os_iter.deinit();
87
88const exe = try iter.next();
89
90var args = try clap.ComptimeClap(params).parse(allocator, clap.args.OsIterator.Error, iter);
91defer args.deinit();
92
93if (args.flag("--helps"))
94 debug.warn("Help!\n");
95```
96
97```
98zig-clap/src/comptime.zig:103:17: error: --helps is not a parameter.
99 @compileError(name ++ " is not a parameter.");
100 ^
101zig-clap/src/comptime.zig:71:45: note: called from here
102 const param = comptime findParam(name);
103 ^
104zig-clap/example/comptime-clap.zig:41:18: note: called from here
105 if (args.flag("--helps"))
106 ^
107```
108
109Ofc, this limits you to use only parameters that are comptime known.