diff options
| author | 2019-11-27 23:13:31 +0900 | |
|---|---|---|
| committer | 2019-11-27 23:13:31 +0900 | |
| commit | a305e818bd986c599fff17141617bc4f890276cf (patch) | |
| tree | ff0a5c133c271d83de3e71ebc023ffc08bb1e3c9 /README.md | |
| parent | Breaking: OsIterator will now get the exe on init (diff) | |
| download | zig-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.md | 160 |
1 files changed, 96 insertions, 64 deletions
| @@ -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 | ||
| 20 | The `StreamingClap` is the base of all the other parsers. It's a streaming parser that uses an | 20 | The 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 |
| 24 | const std = @import("std"); | 23 | const std = @import("std"); |
| @@ -27,58 +26,69 @@ const clap = @import("clap"); | |||
| 27 | const debug = std.debug; | 26 | const debug = std.debug; |
| 28 | 27 | ||
| 29 | pub fn main() !void { | 28 | pub 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 | |||
| 52 | The data structure returned has lookup speed on par with array access (`arr[i]`) and validates | ||
| 53 | that the strings you pass to `option` and `flag` are actually parameters that the program can take: | ||
| 54 | |||
| 55 | ```zig | ||
| 56 | const std = @import("std"); | ||
| 57 | const clap = @import("clap"); | ||
| 58 | |||
| 59 | pub 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 | ``` | ||
| 75 | zig-clap/clap/comptime.zig:109:17: error: --helps is not a parameter. | ||
| 76 | @compileError(name ++ " is not a parameter."); | ||
| 77 | ^ | ||
| 78 | zig-clap/clap/comptime.zig:77:45: note: called from here | ||
| 79 | const param = comptime findParam(name); | ||
| 80 | ^ | ||
| 81 | zig-clap/clap.zig:238:31: note: called from here | ||
| 82 | return a.clap.flag(name); | ||
| 83 | ^ | ||
| 84 | zig-clap/example/simple-error.zig:16:18: note: called from here | ||
| 85 | _ = args.flag("--helps"); | ||
| 86 | ``` | ||
| 87 | |||
| 78 | ### `ComptimeClap` | 88 | ### `ComptimeClap` |
| 79 | 89 | ||
| 80 | The `ComptimeClap` is a wrapper for `StreamingClap`, which parses all the arguments and makes | 90 | The `ComptimeClap` is the parser used by `clap.parse`. It allows the user to use a custom argument |
| 81 | them available through three functions (`flag`, `option`, `positionals`). | 91 | iterator. |
| 82 | 92 | ||
| 83 | ```zig | 93 | ```zig |
| 84 | const std = @import("std"); | 94 | const std = @import("std"); |
| @@ -118,46 +128,68 @@ pub fn main() !void { | |||
| 118 | 128 | ||
| 119 | ``` | 129 | ``` |
| 120 | 130 | ||
| 121 | The data structure returned from this parser has lookup speed on par with array access (`arr[i]`) | 131 | ### `StreamingClap` |
| 122 | and validates that the strings you pass to `option` and `flag` are actually parameters that the | 132 | |
| 123 | program can take: | 133 | The `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 |
| 126 | const std = @import("std"); | 137 | const std = @import("std"); |
| 127 | const clap = @import("clap"); | 138 | const clap = @import("clap"); |
| 128 | 139 | ||
| 140 | const debug = std.debug; | ||
| 141 | |||
| 129 | pub fn main() !void { | 142 | pub 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 | ``` |
| 149 | zig-clap/src/comptime.zig:109:17: error: --helps is not a parameter. | ||
| 150 | @compileError(name ++ " is not a parameter."); | ||
| 151 | ^ | ||
| 152 | zig-clap/src/comptime.zig:77:45: note: called from here | ||
| 153 | const param = comptime findParam(name); | ||
| 154 | ^ | ||
| 155 | zig-clap/example/comptime-clap-error.zig:18:18: note: called from here | ||
| 156 | _ = args.flag("--helps"); | ||
| 157 | ^ | ||
| 158 | ``` | ||
| 159 | 190 | ||
| 160 | Ofc, this limits you to parameters that are comptime known. | 191 | Currently, this parse is the only parser that allow an array of `Param` that |
| 192 | is generated at runtime. | ||
| 161 | 193 | ||
| 162 | ### `help` | 194 | ### `help` |
| 163 | 195 | ||