diff options
| author | 2020-11-10 18:53:57 +0100 | |
|---|---|---|
| committer | 2020-11-10 18:53:57 +0100 | |
| commit | b7e6ebf36e2ac4314e6bff65f1d64466ea82a18a (patch) | |
| tree | f979a760149f4c7a2d9b9113a872d8d7e6d56bef /README.md | |
| parent | Update ci to use 0.7.0 (diff) | |
| download | zig-clap-b7e6ebf36e2ac4314e6bff65f1d64466ea82a18a.tar.gz zig-clap-b7e6ebf36e2ac4314e6bff65f1d64466ea82a18a.tar.xz zig-clap-b7e6ebf36e2ac4314e6bff65f1d64466ea82a18a.zip | |
Deprecate ComptimeClap in favor of parseExv0.3.0
Diffstat (limited to '')
| -rw-r--r-- | README.md | 56 |
1 files changed, 2 insertions, 54 deletions
| @@ -99,59 +99,7 @@ zig-clap/example/simple-error.zig:16:18: note: called from here | |||
| 99 | _ = args.flag("--helps"); | 99 | _ = args.flag("--helps"); |
| 100 | ``` | 100 | ``` |
| 101 | 101 | ||
| 102 | ### `ComptimeClap` | 102 | There is also a `parseEx` variant that takes an argument iterator. |
| 103 | |||
| 104 | The `ComptimeClap` is the parser used by `clap.parse`. It allows the user to use a custom argument | ||
| 105 | iterator. | ||
| 106 | |||
| 107 | ```zig | ||
| 108 | const std = @import("std"); | ||
| 109 | const clap = @import("clap"); | ||
| 110 | |||
| 111 | const debug = std.debug; | ||
| 112 | |||
| 113 | pub fn main() !void { | ||
| 114 | const allocator = std.heap.page_allocator; | ||
| 115 | |||
| 116 | // First we specify what parameters our program can take. | ||
| 117 | // We can use `parseParam` to parse a string to a `Param(Help)` | ||
| 118 | const params = comptime [_]clap.Param(clap.Help){ | ||
| 119 | clap.parseParam("-h, --help Display this help and exit. ") catch unreachable, | ||
| 120 | clap.parseParam("-n, --number <NUM> An option parameter, which takes a value.") catch unreachable, | ||
| 121 | clap.parseParam("-s, --string <STR>... An option parameter which can be specified multiple times.") catch unreachable, | ||
| 122 | clap.parseParam("<POS>...") catch unreachable, | ||
| 123 | }; | ||
| 124 | const Clap = clap.ComptimeClap(clap.Help, clap.args.OsIterator, ¶ms); | ||
| 125 | |||
| 126 | // We then initialize an argument iterator. We will use the OsIterator as it nicely | ||
| 127 | // wraps iterating over arguments the most efficient way on each os. | ||
| 128 | var iter = try clap.args.OsIterator.init(allocator); | ||
| 129 | defer iter.deinit(); | ||
| 130 | |||
| 131 | // Initalize our diagnostics, which can be used for reporting useful errors. | ||
| 132 | // This is optional. You can also just pass `null` to `parser.next` if you | ||
| 133 | // don't care about the extra information `Diagnostics` provides. | ||
| 134 | var diag: clap.Diagnostic = undefined; | ||
| 135 | |||
| 136 | // Parse the arguments | ||
| 137 | var args = Clap.parse(allocator, &iter, &diag) catch |err| { | ||
| 138 | // Report useful error and exit | ||
| 139 | diag.report(std.io.getStdErr().outStream(), err) catch {}; | ||
| 140 | return err; | ||
| 141 | }; | ||
| 142 | defer args.deinit(); | ||
| 143 | |||
| 144 | if (args.flag("--help")) | ||
| 145 | debug.warn("--help\n", .{}); | ||
| 146 | if (args.option("--number")) |n| | ||
| 147 | debug.warn("--number = {}\n", .{n}); | ||
| 148 | for (args.options("--string")) |s| | ||
| 149 | debug.warn("--string = {}\n", .{s}); | ||
| 150 | for (args.positionals()) |pos| | ||
| 151 | debug.warn("{}\n", .{pos}); | ||
| 152 | } | ||
| 153 | |||
| 154 | ``` | ||
| 155 | 103 | ||
| 156 | ### `StreamingClap` | 104 | ### `StreamingClap` |
| 157 | 105 | ||
| @@ -222,7 +170,7 @@ pub fn main() !void { | |||
| 222 | 170 | ||
| 223 | ``` | 171 | ``` |
| 224 | 172 | ||
| 225 | Currently, this parse is the only parser that allow an array of `Param` that | 173 | Currently, this parse is the only parser that allow an array of `Param` tha |
| 226 | is generated at runtime. | 174 | is generated at runtime. |
| 227 | 175 | ||
| 228 | ### `help` | 176 | ### `help` |