diff options
Diffstat (limited to 'README.md')
| -rw-r--r-- | README.md | 137 |
1 files changed, 76 insertions, 61 deletions
| @@ -34,75 +34,98 @@ pub fn main() !void { | |||
| 34 | // First we specify what parameters our program can take. | 34 | // First we specify what parameters our program can take. |
| 35 | // 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)` |
| 36 | const params = comptime [_]clap.Param(clap.Help){ | 36 | const params = comptime [_]clap.Param(clap.Help){ |
| 37 | clap.parseParam("-h, --help Display this help and exit. ") catch unreachable, | 37 | clap.parseParam("-h, --help Display this help and exit.") catch unreachable, |
| 38 | clap.parseParam("-n, --number <NUM> An option parameter, which takes a value.") catch unreachable, | 38 | clap.parseParam("-n, --number <usize> 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, | 39 | clap.parseParam("-s, --string <str>... An option parameter which can be specified multiple times.") catch unreachable, |
| 40 | clap.parseParam("<POS>...") catch unreachable, | 40 | clap.parseParam("<str>...") catch unreachable, |
| 41 | }; | 41 | }; |
| 42 | 42 | ||
| 43 | // Initalize our diagnostics, which can be used for reporting useful errors. | 43 | // Initalize our diagnostics, which can be used for reporting useful errors. |
| 44 | // This is optional. You can also pass `.{}` to `clap.parse` if you don't | 44 | // This is optional. You can also pass `.{}` to `clap.parse` if you don't |
| 45 | // care about the extra information `Diagnostics` provides. | 45 | // care about the extra information `Diagnostics` provides. |
| 46 | var diag = clap.Diagnostic{}; | 46 | var diag = clap.Diagnostic{}; |
| 47 | var args = clap.parse(clap.Help, ¶ms, .{ .diagnostic = &diag }) catch |err| { | 47 | var res = clap.parse(clap.Help, ¶ms, clap.parsers.default, .{ |
| 48 | .diagnostic = &diag, | ||
| 49 | }) catch |err| { | ||
| 48 | // Report useful error and exit | 50 | // Report useful error and exit |
| 49 | diag.report(io.getStdErr().writer(), err) catch {}; | 51 | diag.report(io.getStdErr().writer(), err) catch {}; |
| 50 | return err; | 52 | return err; |
| 51 | }; | 53 | }; |
| 52 | defer args.deinit(); | 54 | defer res.deinit(); |
| 53 | 55 | ||
| 54 | if (args.flag("--help")) | 56 | if (res.args.help) |
| 55 | debug.print("--help\n", .{}); | 57 | debug.print("--help\n", .{}); |
| 56 | if (args.option("--number")) |n| | 58 | if (res.args.number) |n| |
| 57 | debug.print("--number = {s}\n", .{n}); | 59 | debug.print("--number = {}\n", .{n}); |
| 58 | for (args.options("--string")) |s| | 60 | for (res.args.string) |s| |
| 59 | debug.print("--string = {s}\n", .{s}); | 61 | debug.print("--string = {s}\n", .{s}); |
| 60 | for (args.positionals()) |pos| | 62 | for (res.positionals) |pos| |
| 61 | debug.print("{s}\n", .{pos}); | 63 | debug.print("{s}\n", .{pos}); |
| 62 | } | 64 | } |
| 63 | 65 | ||
| 64 | ``` | 66 | ``` |
| 65 | 67 | ||
| 66 | The data structure returned has lookup speed on par with array access (`arr[i]`) and validates | 68 | The result will contain an `args` field and a `positionals` field. `args` will have one field |
| 67 | that the strings you pass to `option`, `options` and `flag` are actually parameters that the | 69 | for each none positional parameter of your program. The name of the field will be the longest |
| 68 | program can take: | 70 | name of the parameter. |
| 71 | |||
| 72 | The fields in `args` are typed. The type is based on the name of the value the parameter takes. | ||
| 73 | Since `--number` takes a `usize` the field `res.args.number` has the type `usize`. | ||
| 74 | |||
| 75 | Note that this is only the case because `clap.parsers.default` has a field called `usize` which | ||
| 76 | contains a parser that returns `usize`. You can pass in something other than `clap.parsers.default` | ||
| 77 | if you want some other mapping. | ||
| 69 | 78 | ||
| 70 | ```zig | 79 | ```zig |
| 71 | const clap = @import("clap"); | 80 | const clap = @import("clap"); |
| 72 | const std = @import("std"); | 81 | const std = @import("std"); |
| 73 | 82 | ||
| 83 | const debug = std.debug; | ||
| 84 | const io = std.io; | ||
| 85 | const process = std.process; | ||
| 86 | |||
| 74 | pub fn main() !void { | 87 | pub fn main() !void { |
| 88 | // First we specify what parameters our program can take. | ||
| 89 | // We can use `parseParam` to parse a string to a `Param(Help)` | ||
| 75 | const params = comptime [_]clap.Param(clap.Help){ | 90 | const params = comptime [_]clap.Param(clap.Help){ |
| 76 | clap.parseParam("-h, --help Display this help and exit.") catch unreachable, | 91 | clap.parseParam("-h, --help Display this help and exit.") catch unreachable, |
| 92 | clap.parseParam("-n, --number <INT> An option parameter, which takes a value.") catch unreachable, | ||
| 93 | clap.parseParam("-s, --string <STR>... An option parameter which can be specified multiple times.") catch unreachable, | ||
| 94 | clap.parseParam("<FILE>...") catch unreachable, | ||
| 77 | }; | 95 | }; |
| 78 | 96 | ||
| 79 | var args = try clap.parse(clap.Help, ¶ms, .{}); | 97 | // Declare our own parsers which are used to map the argument strings to other |
| 80 | defer args.deinit(); | 98 | // types. |
| 99 | const parsers = comptime .{ | ||
| 100 | .STR = clap.parsers.string, | ||
| 101 | .FILE = clap.parsers.string, | ||
| 102 | .INT = clap.parsers.int(usize, 10), | ||
| 103 | }; | ||
| 81 | 104 | ||
| 82 | _ = args.flag("--helps"); | 105 | var diag = clap.Diagnostic{}; |
| 83 | } | 106 | var res = clap.parse(clap.Help, ¶ms, parsers, .{ |
| 107 | .diagnostic = &diag, | ||
| 108 | }) catch |err| { | ||
| 109 | diag.report(io.getStdErr().writer(), err) catch {}; | ||
| 110 | return err; | ||
| 111 | }; | ||
| 112 | defer res.deinit(); | ||
| 84 | 113 | ||
| 85 | ``` | 114 | if (res.args.help) |
| 115 | debug.print("--help\n", .{}); | ||
| 116 | if (res.args.number) |n| | ||
| 117 | debug.print("--number = {}\n", .{n}); | ||
| 118 | for (res.args.string) |s| | ||
| 119 | debug.print("--string = {s}\n", .{s}); | ||
| 120 | for (res.positionals) |pos| | ||
| 121 | debug.print("{s}\n", .{pos}); | ||
| 122 | } | ||
| 86 | 123 | ||
| 87 | ``` | 124 | ``` |
| 88 | zig-clap/clap/comptime.zig:109:17: error: --helps is not a parameter. | ||
| 89 | @compileError(name ++ " is not a parameter."); | ||
| 90 | ^ | ||
| 91 | zig-clap/clap/comptime.zig:77:45: note: called from here | ||
| 92 | const param = comptime findParam(name); | ||
| 93 | ^ | ||
| 94 | zig-clap/clap.zig:238:31: note: called from here | ||
| 95 | return a.clap.flag(name); | ||
| 96 | ^ | ||
| 97 | zig-clap/example/simple-error.zig:16:18: note: called from here | ||
| 98 | _ = args.flag("--helps"); | ||
| 99 | ``` | ||
| 100 | |||
| 101 | There is also a `parseEx` variant that takes an argument iterator. | ||
| 102 | 125 | ||
| 103 | ### `StreamingClap` | 126 | ### `streaming.Clap` |
| 104 | 127 | ||
| 105 | The `StreamingClap` is the base of all the other parsers. It's a streaming parser that uses an | 128 | The `streaming.Clap` is the base of all the other parsers. It's a streaming parser that uses an |
| 106 | `args.Iterator` to provide it with arguments lazily. | 129 | `args.Iterator` to provide it with arguments lazily. |
| 107 | 130 | ||
| 108 | ```zig | 131 | ```zig |
| @@ -140,7 +163,7 @@ pub fn main() !void { | |||
| 140 | // This is optional. You can also leave the `diagnostic` field unset if you | 163 | // This is optional. You can also leave the `diagnostic` field unset if you |
| 141 | // don't care about the extra information `Diagnostic` provides. | 164 | // don't care about the extra information `Diagnostic` provides. |
| 142 | var diag = clap.Diagnostic{}; | 165 | var diag = clap.Diagnostic{}; |
| 143 | var parser = clap.StreamingClap(u8, process.ArgIterator){ | 166 | var parser = clap.streaming.Clap(u8, process.ArgIterator){ |
| 144 | .params = ¶ms, | 167 | .params = ¶ms, |
| 145 | .iter = &iter, | 168 | .iter = &iter, |
| 146 | .diagnostic = &diag, | 169 | .diagnostic = &diag, |
| @@ -173,8 +196,9 @@ is generated at runtime. | |||
| 173 | 196 | ||
| 174 | ### `help` | 197 | ### `help` |
| 175 | 198 | ||
| 176 | The `help`, `helpEx` and `helpFull` are functions for printing a simple list of all parameters the | 199 | The `help` prints a simple list of all parameters the program can take. It expects the |
| 177 | program can take. | 200 | `Id` to have a `description` method and an `value` method so that it can provide that |
| 201 | in the output. | ||
| 178 | 202 | ||
| 179 | ```zig | 203 | ```zig |
| 180 | const clap = @import("clap"); | 204 | const clap = @import("clap"); |
| @@ -186,14 +210,14 @@ pub fn main() !void { | |||
| 186 | clap.parseParam("-v, --version Output version information and exit.") catch unreachable, | 210 | clap.parseParam("-v, --version Output version information and exit.") catch unreachable, |
| 187 | }; | 211 | }; |
| 188 | 212 | ||
| 189 | var args = try clap.parse(clap.Help, ¶ms, .{}); | 213 | var res = try clap.parse(clap.Help, ¶ms, clap.parsers.default, .{}); |
| 190 | defer args.deinit(); | 214 | defer res.deinit(); |
| 191 | 215 | ||
| 192 | // clap.help is a function that can print a simple help message, given a | 216 | // clap.help is a function that can print a simple help message, given a |
| 193 | // slice of Param(Help). There is also a helpEx, which can print a | 217 | // slice of Param(Help). There is also a helpEx, which can print a |
| 194 | // help message for any Param, but it is more verbose to call. | 218 | // help message for any Param, but it is more verbose to call. |
| 195 | if (args.flag("--help")) | 219 | if (res.args.help) |
| 196 | return clap.help(std.io.getStdErr().writer(), ¶ms); | 220 | return clap.help(std.io.getStdErr().writer(), clap.Help, ¶ms); |
| 197 | } | 221 | } |
| 198 | 222 | ||
| 199 | ``` | 223 | ``` |
| @@ -204,19 +228,10 @@ $ zig-out/bin/help --help | |||
| 204 | -v, --version Output version information and exit. | 228 | -v, --version Output version information and exit. |
| 205 | ``` | 229 | ``` |
| 206 | 230 | ||
| 207 | The `help` functions are the simplest to call. It only takes an `OutStream` and a slice of | ||
| 208 | `Param(Help)`. | ||
| 209 | |||
| 210 | The `helpEx` is the generic version of `help`. It can print a help message for any | ||
| 211 | `Param` give that the caller provides functions for getting the help and value strings. | ||
| 212 | |||
| 213 | The `helpFull` is even more generic, allowing the functions that get the help and value strings | ||
| 214 | to return errors and take a context as a parameter. | ||
| 215 | |||
| 216 | ### `usage` | 231 | ### `usage` |
| 217 | 232 | ||
| 218 | The `usage`, `usageEx` and `usageFull` are functions for printing a small abbreviated version | 233 | The `usage` prints a small abbreviated version of the help message. It expects the `Id` |
| 219 | of the help message. | 234 | to have a `value` method so it can provide that in the output. |
| 220 | 235 | ||
| 221 | ```zig | 236 | ```zig |
| 222 | const clap = @import("clap"); | 237 | const clap = @import("clap"); |
| @@ -224,19 +239,19 @@ const std = @import("std"); | |||
| 224 | 239 | ||
| 225 | pub fn main() !void { | 240 | pub fn main() !void { |
| 226 | const params = comptime [_]clap.Param(clap.Help){ | 241 | const params = comptime [_]clap.Param(clap.Help){ |
| 227 | clap.parseParam("-h, --help Display this help and exit. ") catch unreachable, | 242 | clap.parseParam("-h, --help Display this help and exit.") catch unreachable, |
| 228 | clap.parseParam("-v, --version Output version information and exit. ") catch unreachable, | 243 | clap.parseParam("-v, --version Output version information and exit.") catch unreachable, |
| 229 | clap.parseParam(" --value <N> An option parameter, which takes a value.") catch unreachable, | 244 | clap.parseParam(" --value <str> An option parameter, which takes a value.") catch unreachable, |
| 230 | }; | 245 | }; |
| 231 | 246 | ||
| 232 | var args = try clap.parse(clap.Help, ¶ms, .{}); | 247 | var res = try clap.parse(clap.Help, ¶ms, clap.parsers.default, .{}); |
| 233 | defer args.deinit(); | 248 | defer res.deinit(); |
| 234 | 249 | ||
| 235 | // clap.usage is a function that can print a simple usage message, given a | 250 | // clap.usage is a function that can print a simple usage message, given a |
| 236 | // slice of Param(Help). There is also a usageEx, which can print a | 251 | // slice of Param(Help). There is also a usageEx, which can print a |
| 237 | // usage message for any Param, but it is more verbose to call. | 252 | // usage message for any Param, but it is more verbose to call. |
| 238 | if (args.flag("--help")) | 253 | if (res.args.help) |
| 239 | return clap.usage(std.io.getStdErr().writer(), ¶ms); | 254 | return clap.usage(std.io.getStdErr().writer(), clap.Help, ¶ms); |
| 240 | } | 255 | } |
| 241 | 256 | ||
| 242 | ``` | 257 | ``` |