summaryrefslogtreecommitdiff
path: root/README.md
diff options
context:
space:
mode:
authorGravatar Asherah Connor2020-08-28 17:26:01 +1000
committerGravatar Komari Spaghetti2020-08-28 09:43:42 +0200
commita3d2a261f59983838e3ed5f01d90f18352e6a421 (patch)
tree834fb4e483fd92a6b6d29ca89482fbfb87b7089a /README.md
parentreverse the order of these (diff)
downloadzig-clap-a3d2a261f59983838e3ed5f01d90f18352e6a421.tar.gz
zig-clap-a3d2a261f59983838e3ed5f01d90f18352e6a421.tar.xz
zig-clap-a3d2a261f59983838e3ed5f01d90f18352e6a421.zip
adjust examples, README template
Diffstat (limited to 'README.md')
-rw-r--r--README.md38
1 files changed, 23 insertions, 15 deletions
diff --git a/README.md b/README.md
index 2b38281..2037b04 100644
--- a/README.md
+++ b/README.md
@@ -14,6 +14,7 @@ into master on every `zig` release.
14* Supports both passing values using spacing and `=` (`-a 100`, `-a=100`) 14* Supports both passing values using spacing and `=` (`-a 100`, `-a=100`)
15 * Short args also support passing values with no spacing or `=` (`-a100`) 15 * Short args also support passing values with no spacing or `=` (`-a100`)
16 * This all works with chaining (`-ba 100`, `-ba=100`, `-ba100`) 16 * This all works with chaining (`-ba 100`, `-ba=100`, `-ba100`)
17* Supports options that can be specified multiple times (`-e 1 -e 2 -e 3`)
17* Print help message from parameter specification. 18* Print help message from parameter specification.
18* Parse help message to parameter specification. 19* Parse help message to parameter specification.
19 20
@@ -33,10 +34,11 @@ pub fn main() !void {
33 // First we specify what parameters our program can take. 34 // First we specify what parameters our program can take.
34 // 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)`
35 const params = comptime [_]clap.Param(clap.Help){ 36 const params = comptime [_]clap.Param(clap.Help){
36 clap.parseParam("-h, --help Display this help and exit. ") catch unreachable, 37 clap.parseParam("-h, --help Display this help and exit. ") catch unreachable,
37 clap.parseParam("-n, --number <NUM> An option parameter, which takes a value.") catch unreachable, 38 clap.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,
38 clap.Param(clap.Help){ 40 clap.Param(clap.Help){
39 .takes_value = true, 41 .takes_value = .One,
40 }, 42 },
41 }; 43 };
42 44
@@ -46,15 +48,18 @@ pub fn main() !void {
46 if (args.flag("--help")) 48 if (args.flag("--help"))
47 debug.warn("--help\n", .{}); 49 debug.warn("--help\n", .{});
48 if (args.option("--number")) |n| 50 if (args.option("--number")) |n|
49 debug.warn("--number = {}\n", .{ n }); 51 debug.warn("--number = {}\n", .{n});
52 for (args.options("--string")) |s|
53 debug.warn("--string = {}\n", .{s});
50 for (args.positionals()) |pos| 54 for (args.positionals()) |pos|
51 debug.warn("{}\n", .{ pos }); 55 debug.warn("{}\n", .{pos});
52} 56}
53 57
54``` 58```
55 59
56The data structure returned has lookup speed on par with array access (`arr[i]`) and validates 60The data structure returned has lookup speed on par with array access (`arr[i]`) and validates
57that the strings you pass to `option` and `flag` are actually parameters that the program can take: 61that the strings you pass to `option`, `options` and `flag` are actually parameters that the
62program can take:
58 63
59```zig 64```zig
60const std = @import("std"); 65const std = @import("std");
@@ -106,10 +111,11 @@ pub fn main() !void {
106 // First we specify what parameters our program can take. 111 // First we specify what parameters our program can take.
107 // We can use `parseParam` to parse a string to a `Param(Help)` 112 // We can use `parseParam` to parse a string to a `Param(Help)`
108 const params = comptime [_]clap.Param(clap.Help){ 113 const params = comptime [_]clap.Param(clap.Help){
109 clap.parseParam("-h, --help Display this help and exit. ") catch unreachable, 114 clap.parseParam("-h, --help Display this help and exit. ") catch unreachable,
110 clap.parseParam("-n, --number <NUM> An option parameter, which takes a value.") catch unreachable, 115 clap.parseParam("-n, --number <NUM> An option parameter, which takes a value.") catch unreachable,
116 clap.parseParam("-s, --string <STR>... An option parameter which can be specified multiple times.") catch unreachable,
111 clap.Param(clap.Help){ 117 clap.Param(clap.Help){
112 .takes_value = true, 118 .takes_value = .One,
113 }, 119 },
114 }; 120 };
115 121
@@ -125,9 +131,11 @@ pub fn main() !void {
125 if (args.flag("--help")) 131 if (args.flag("--help"))
126 debug.warn("--help\n", .{}); 132 debug.warn("--help\n", .{});
127 if (args.option("--number")) |n| 133 if (args.option("--number")) |n|
128 debug.warn("--number = {}\n", .{ n }); 134 debug.warn("--number = {}\n", .{n});
135 for (args.options("--string")) |s|
136 debug.warn("--string = {}\n", .{s});
129 for (args.positionals()) |pos| 137 for (args.positionals()) |pos|
130 debug.warn("{}\n", .{ pos }); 138 debug.warn("{}\n", .{pos});
131} 139}
132 140
133``` 141```
@@ -155,11 +163,11 @@ pub fn main() !void {
155 clap.Param(u8){ 163 clap.Param(u8){
156 .id = 'n', 164 .id = 'n',
157 .names = clap.Names{ .short = 'n', .long = "number" }, 165 .names = clap.Names{ .short = 'n', .long = "number" },
158 .takes_value = true, 166 .takes_value = .One,
159 }, 167 },
160 clap.Param(u8){ 168 clap.Param(u8){
161 .id = 'f', 169 .id = 'f',
162 .takes_value = true, 170 .takes_value = .One,
163 }, 171 },
164 }; 172 };
165 173
@@ -179,12 +187,12 @@ pub fn main() !void {
179 // arg.param will point to the parameter which matched the argument. 187 // arg.param will point to the parameter which matched the argument.
180 switch (arg.param.id) { 188 switch (arg.param.id) {
181 'h' => debug.warn("Help!\n", .{}), 189 'h' => debug.warn("Help!\n", .{}),
182 'n' => debug.warn("--number = {}\n", .{ arg.value.? }), 190 'n' => debug.warn("--number = {}\n", .{arg.value.?}),
183 191
184 // arg.value == null, if arg.param.takes_value == false. 192 // arg.value == null, if arg.param.takes_value == false.
185 // Otherwise, arg.value is the value passed with the argument, such as "-a=10" 193 // Otherwise, arg.value is the value passed with the argument, such as "-a=10"
186 // or "-a 10". 194 // or "-a 10".
187 'f' => debug.warn("{}\n", .{ arg.value.? }), 195 'f' => debug.warn("{}\n", .{arg.value.?}),
188 else => unreachable, 196 else => unreachable,
189 } 197 }
190 } 198 }