summaryrefslogtreecommitdiff
path: root/README.md
diff options
context:
space:
mode:
authorGravatar Jimmi Holst Christensen2019-11-27 23:13:31 +0900
committerGravatar Jimmi Holst Christensen2019-11-27 23:13:31 +0900
commita305e818bd986c599fff17141617bc4f890276cf (patch)
treeff0a5c133c271d83de3e71ebc023ffc08bb1e3c9 /README.md
parentBreaking: OsIterator will now get the exe on init (diff)
downloadzig-clap-a305e818bd986c599fff17141617bc4f890276cf.tar.gz
zig-clap-a305e818bd986c599fff17141617bc4f890276cf.tar.xz
zig-clap-a305e818bd986c599fff17141617bc4f890276cf.zip
Add clap.parse as the simplest way of using the lib
Diffstat (limited to 'README.md')
-rw-r--r--README.md160
1 files changed, 96 insertions, 64 deletions
diff --git a/README.md b/README.md
index 6c556b9..0555a0f 100644
--- a/README.md
+++ b/README.md
@@ -15,10 +15,9 @@ A simple and easy to use command line argument parser library for Zig.
15 15
16## Examples 16## Examples
17 17
18### `StreamingClap` 18### `clap.parse`
19 19
20The `StreamingClap` is the base of all the other parsers. It's a streaming parser that uses an 20The simplest way to use this library is to just call the `clap.parse` function.
21`args.Iterator` to provide it with arguments lazily.
22 21
23```zig 22```zig
24const std = @import("std"); 23const std = @import("std");
@@ -27,58 +26,69 @@ const clap = @import("clap");
27const debug = std.debug; 26const debug = std.debug;
28 27
29pub fn main() !void { 28pub fn main() !void {
30 const allocator = std.heap.direct_allocator;
31
32 // First we specify what parameters our program can take. 29 // First we specify what parameters our program can take.
33 const params = [_]clap.Param(u8){ 30 // We can use `parseParam` to parse a string to a `Param(Help)`
34 clap.Param(u8){ 31 const params = comptime [_]clap.Param(clap.Help){
35 .id = 'h', 32 clap.parseParam("-h, --help Display this help and exit. ") catch unreachable,
36 .names = clap.Names{ .short = 'h', .long = "help" }, 33 clap.parseParam("-n, --number <NUM> An option parameter, which takes a value.") catch unreachable,
37 }, 34 clap.Param(clap.Help){
38 clap.Param(u8){
39 .id = 'n',
40 .names = clap.Names{ .short = 'n', .long = "number" },
41 .takes_value = true,
42 },
43 clap.Param(u8){
44 .id = 'f',
45 .takes_value = true, 35 .takes_value = true,
46 }, 36 },
47 }; 37 };
48 38
49 // We then initialize an argument iterator. We will use the OsIterator as it nicely 39 var args = try clap.parse(clap.Help, params, std.heap.direct_allocator);
50 // wraps iterating over arguments the most efficient way on each os. 40 defer args.deinit();
51 var iter = try clap.args.OsIterator.init(allocator);
52 defer iter.deinit();
53 41
54 // Initialize our streaming parser. 42 if (args.flag("--help"))
55 var parser = clap.StreamingClap(u8, clap.args.OsIterator){ 43 debug.warn("--help\n");
56 .params = params, 44 if (args.option("--number")) |n|
57 .iter = &iter, 45 debug.warn("--number = {}\n", n);
46 for (args.positionals()) |pos|
47 debug.warn("{}\n", pos);
48}
49
50```
51
52The data structure returned has lookup speed on par with array access (`arr[i]`) and validates
53that the strings you pass to `option` and `flag` are actually parameters that the program can take:
54
55```zig
56const std = @import("std");
57const clap = @import("clap");
58
59pub fn main() !void {
60 // First we specify what parameters our program can take.
61 // We can use `parseParam` to parse a string to a `Param(Help)`
62 const params = comptime [_]clap.Param(clap.Help){
63 clap.parseParam("-h, --help Display this help and exit.") catch unreachable,
58 }; 64 };
59 65
60 // Because we use a streaming parser, we have to consume each argument parsed individually. 66 var args = try clap.parse(clap.Help, params, std.heap.direct_allocator);
61 while (try parser.next()) |arg| { 67 defer args.deinit();
62 // arg.param will point to the parameter which matched the argument.
63 switch (arg.param.id) {
64 'h' => debug.warn("Help!\n"),
65 'n' => debug.warn("--number = {}\n", arg.value.?),
66 68
67 // arg.value == null, if arg.param.takes_value == false. 69 _ = args.flag("--helps");
68 // Otherwise, arg.value is the value passed with the argument, such as "-a=10"
69 // or "-a 10".
70 'f' => debug.warn("{}\n", arg.value.?),
71 else => unreachable,
72 }
73 }
74} 70}
75 71
76``` 72```
77 73
74```
75zig-clap/clap/comptime.zig:109:17: error: --helps is not a parameter.
76 @compileError(name ++ " is not a parameter.");
77 ^
78zig-clap/clap/comptime.zig:77:45: note: called from here
79 const param = comptime findParam(name);
80 ^
81zig-clap/clap.zig:238:31: note: called from here
82 return a.clap.flag(name);
83 ^
84zig-clap/example/simple-error.zig:16:18: note: called from here
85 _ = args.flag("--helps");
86```
87
78### `ComptimeClap` 88### `ComptimeClap`
79 89
80The `ComptimeClap` is a wrapper for `StreamingClap`, which parses all the arguments and makes 90The `ComptimeClap` is the parser used by `clap.parse`. It allows the user to use a custom argument
81them available through three functions (`flag`, `option`, `positionals`). 91iterator.
82 92
83```zig 93```zig
84const std = @import("std"); 94const std = @import("std");
@@ -118,46 +128,68 @@ pub fn main() !void {
118 128
119``` 129```
120 130
121The data structure returned from this parser has lookup speed on par with array access (`arr[i]`) 131### `StreamingClap`
122and validates that the strings you pass to `option` and `flag` are actually parameters that the 132
123program can take: 133The `StreamingClap` is the base of all the other parsers. It's a streaming parser that uses an
134`args.Iterator` to provide it with arguments lazily.
124 135
125```zig 136```zig
126const std = @import("std"); 137const std = @import("std");
127const clap = @import("clap"); 138const clap = @import("clap");
128 139
140const debug = std.debug;
141
129pub fn main() !void { 142pub fn main() !void {
130 const allocator = std.heap.direct_allocator; 143 const allocator = std.heap.direct_allocator;
131 144
132 const params = [_]clap.Param(void){clap.Param(void){ 145 // First we specify what parameters our program can take.
133 .names = clap.Names{ .short = 'h', .long = "help" }, 146 const params = [_]clap.Param(u8){
134 }}; 147 clap.Param(u8){
148 .id = 'h',
149 .names = clap.Names{ .short = 'h', .long = "help" },
150 },
151 clap.Param(u8){
152 .id = 'n',
153 .names = clap.Names{ .short = 'n', .long = "number" },
154 .takes_value = true,
155 },
156 clap.Param(u8){
157 .id = 'f',
158 .takes_value = true,
159 },
160 };
135 161
136 var iter = clap.args.OsIterator.init(allocator); 162 // We then initialize an argument iterator. We will use the OsIterator as it nicely
163 // wraps iterating over arguments the most efficient way on each os.
164 var iter = try clap.args.OsIterator.init(allocator);
137 defer iter.deinit(); 165 defer iter.deinit();
138 const exe = try iter.next();
139 166
140 var args = try clap.ComptimeClap(void, params).parse(allocator, clap.args.OsIterator, &iter); 167 // Initialize our streaming parser.
141 defer args.deinit(); 168 var parser = clap.StreamingClap(u8, clap.args.OsIterator){
169 .params = params,
170 .iter = &iter,
171 };
142 172
143 _ = args.flag("--helps"); 173 // Because we use a streaming parser, we have to consume each argument parsed individually.
144} 174 while (try parser.next()) |arg| {
175 // arg.param will point to the parameter which matched the argument.
176 switch (arg.param.id) {
177 'h' => debug.warn("Help!\n"),
178 'n' => debug.warn("--number = {}\n", arg.value.?),
145 179
146``` 180 // arg.value == null, if arg.param.takes_value == false.
181 // Otherwise, arg.value is the value passed with the argument, such as "-a=10"
182 // or "-a 10".
183 'f' => debug.warn("{}\n", arg.value.?),
184 else => unreachable,
185 }
186 }
187}
147 188
148``` 189```
149zig-clap/src/comptime.zig:109:17: error: --helps is not a parameter.
150 @compileError(name ++ " is not a parameter.");
151 ^
152zig-clap/src/comptime.zig:77:45: note: called from here
153 const param = comptime findParam(name);
154 ^
155zig-clap/example/comptime-clap-error.zig:18:18: note: called from here
156 _ = args.flag("--helps");
157 ^
158```
159 190
160Ofc, this limits you to parameters that are comptime known. 191Currently, this parse is the only parser that allow an array of `Param` that
192is generated at runtime.
161 193
162### `help` 194### `help`
163 195