diff options
Diffstat (limited to 'README.md')
| -rw-r--r-- | README.md | 237 |
1 files changed, 163 insertions, 74 deletions
| @@ -18,27 +18,64 @@ A simple and easy to use command line argument parser library for Zig. | |||
| 18 | The `StreamingClap` is the base of all the other parsers. It's a streaming parser that uses an | 18 | The `StreamingClap` is the base of all the other parsers. It's a streaming parser that uses an |
| 19 | `args.Iterator` to provide it with arguments lazily. | 19 | `args.Iterator` to provide it with arguments lazily. |
| 20 | 20 | ||
| 21 | ```rust | 21 | ```zig |
| 22 | const params = []clap.Param(u8){ | 22 | const std = @import("std"); |
| 23 | clap.Param(u8).flag('h', clap.Names.both("help")), | 23 | const clap = @import("clap"); |
| 24 | clap.Param(u8).option('n', clap.Names.both("number")), | 24 | |
| 25 | clap.Param(u8).positional('f'), | 25 | const debug = std.debug; |
| 26 | }; | 26 | |
| 27 | 27 | pub fn main() !void { | |
| 28 | var iter = clap.args.OsIterator.init(allocator); | 28 | var direct_allocator = std.heap.DirectAllocator.init(); |
| 29 | defer iter.deinit(); | 29 | const allocator = &direct_allocator.allocator; |
| 30 | const exe = try iter.next(); | 30 | defer direct_allocator.deinit(); |
| 31 | 31 | ||
| 32 | var parser = clap.StreamingClap(u8, clap.args.OsIterator).init(params, &iter); | 32 | // First we specify what parameters our program can take. |
| 33 | 33 | const params = [_]clap.Param(u8){ | |
| 34 | while (try parser.next()) |arg| { | 34 | clap.Param(u8){ |
| 35 | switch (arg.param.id) { | 35 | .id = 'h', |
| 36 | 'h' => debug.warn("Help!\n"), | 36 | .names = clap.Names{ .short = 'h', .long = "help" }, |
| 37 | 'n' => debug.warn("--number = {}\n", arg.value.?), | 37 | }, |
| 38 | 'f' => debug.warn("{}\n", arg.value.?), | 38 | clap.Param(u8){ |
| 39 | else => unreachable, | 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, | ||
| 46 | }, | ||
| 47 | }; | ||
| 48 | |||
| 49 | // We then initialize an argument iterator. We will use the OsIterator as it nicely | ||
| 50 | // wraps iterating over arguments the most efficient way on each os. | ||
| 51 | var iter = clap.args.OsIterator.init(allocator); | ||
| 52 | defer iter.deinit(); | ||
| 53 | |||
| 54 | // Consume the exe arg. | ||
| 55 | const exe = try iter.next(); | ||
| 56 | |||
| 57 | // Finally we initialize our streaming parser. | ||
| 58 | var parser = clap.StreamingClap(u8, clap.args.OsIterator){ | ||
| 59 | .params = params, | ||
| 60 | .iter = &iter, | ||
| 61 | }; | ||
| 62 | |||
| 63 | // Because we use a streaming parser, we have to consume each argument parsed individually. | ||
| 64 | while (try parser.next()) |arg| { | ||
| 65 | // arg.param will point to the parameter which matched the argument. | ||
| 66 | switch (arg.param.id) { | ||
| 67 | 'h' => debug.warn("Help!\n"), | ||
| 68 | 'n' => debug.warn("--number = {}\n", arg.value.?), | ||
| 69 | |||
| 70 | // arg.value == null, if arg.param.takes_value == false. | ||
| 71 | // Otherwise, arg.value is the value passed with the argument, such as "-a=10" | ||
| 72 | // or "-a 10". | ||
| 73 | 'f' => debug.warn("{}\n", arg.value.?), | ||
| 74 | else => unreachable, | ||
| 75 | } | ||
| 40 | } | 76 | } |
| 41 | } | 77 | } |
| 78 | |||
| 42 | ``` | 79 | ``` |
| 43 | 80 | ||
| 44 | ### `ComptimeClap` | 81 | ### `ComptimeClap` |
| @@ -46,57 +83,100 @@ while (try parser.next()) |arg| { | |||
| 46 | The `ComptimeClap` is a wrapper for `StreamingClap`, which parses all the arguments and makes | 83 | The `ComptimeClap` is a wrapper for `StreamingClap`, which parses all the arguments and makes |
| 47 | them available through three functions (`flag`, `option`, `positionals`). | 84 | them available through three functions (`flag`, `option`, `positionals`). |
| 48 | 85 | ||
| 49 | ```rust | 86 | ```zig |
| 50 | const params = comptime []clap.Param(void){ | 87 | const std = @import("std"); |
| 51 | clap.Param(void).flag({}, clap.Names.both("help")), | 88 | const clap = @import("clap"); |
| 52 | clap.Param(void).option({}, clap.Names.both("number")), | 89 | |
| 53 | clap.Param(void).positional({}), | 90 | const debug = std.debug; |
| 54 | }; | 91 | |
| 55 | 92 | pub fn main() !void { | |
| 56 | var iter = clap.args.OsIterator.init(allocator); | 93 | const stdout_file = try std.io.getStdOut(); |
| 57 | defer iter.deinit(); | 94 | var stdout_out_stream = stdout_file.outStream(); |
| 58 | const exe = try iter.next(); | 95 | const stdout = &stdout_out_stream.stream; |
| 59 | 96 | ||
| 60 | var args = try clap.ComptimeClap(void, params).parse(allocator, clap.args.OsIterator, &iter); | 97 | var direct_allocator = std.heap.DirectAllocator.init(); |
| 61 | defer args.deinit(); | 98 | const allocator = &direct_allocator.allocator; |
| 62 | 99 | defer direct_allocator.deinit(); | |
| 63 | if (args.flag("--help")) | 100 | |
| 64 | debug.warn("Help!\n"); | 101 | // First we specify what parameters our program can take. |
| 65 | if (args.option("--number")) |n| | 102 | const params = [_]clap.Param([]const u8){ |
| 66 | debug.warn("--number = {}\n", n); | 103 | clap.Param([]const u8){ |
| 67 | for (args.positionals()) |pos| | 104 | .id = "Display this help and exit.", |
| 68 | debug.warn("{}\n", pos); | 105 | .names = clap.Names{ .short = 'h', .long = "help" }, |
| 106 | }, | ||
| 107 | clap.Param([]const u8){ | ||
| 108 | .id = "An option parameter, which takes a value.", | ||
| 109 | .names = clap.Names{ .short = 'n', .long = "number" }, | ||
| 110 | .takes_value = true, | ||
| 111 | }, | ||
| 112 | clap.Param([]const u8){ | ||
| 113 | .id = "", | ||
| 114 | .takes_value = true, | ||
| 115 | }, | ||
| 116 | }; | ||
| 117 | |||
| 118 | // We then initialize an argument iterator. We will use the OsIterator as it nicely | ||
| 119 | // wraps iterating over arguments the most efficient way on each os. | ||
| 120 | var iter = clap.args.OsIterator.init(allocator); | ||
| 121 | defer iter.deinit(); | ||
| 122 | |||
| 123 | // Consume the exe arg. | ||
| 124 | const exe = try iter.next(); | ||
| 125 | |||
| 126 | // Finally we can parse the arguments | ||
| 127 | var args = try clap.ComptimeClap([]const u8, params).parse(allocator, clap.args.OsIterator, &iter); | ||
| 128 | defer args.deinit(); | ||
| 129 | |||
| 130 | if (args.flag("--help")) | ||
| 131 | debug.warn("--help\n"); | ||
| 132 | if (args.option("--number")) |n| | ||
| 133 | debug.warn("--number = {}\n", n); | ||
| 134 | for (args.positionals()) |pos| | ||
| 135 | debug.warn("{}\n", pos); | ||
| 136 | } | ||
| 137 | |||
| 69 | ``` | 138 | ``` |
| 70 | 139 | ||
| 71 | The data structure returned from this parser has lookup speed on par with array access (`arr[i]`) | 140 | The data structure returned from this parser has lookup speed on par with array access (`arr[i]`) |
| 72 | and validates that the strings you pass to `option` and `flag` are actually parameters that the | 141 | and validates that the strings you pass to `option` and `flag` are actually parameters that the |
| 73 | program can take: | 142 | program can take: |
| 74 | 143 | ||
| 75 | ```rust | 144 | ```zig |
| 76 | const params = comptime []clap.Param(void){ | 145 | const std = @import("std"); |
| 77 | clap.Param(void).flag({}, clap.Names.both("help")), | 146 | const clap = @import("clap"); |
| 78 | }; | 147 | |
| 148 | pub fn main() !void { | ||
| 149 | const params = [_]clap.Param(void){ | ||
| 150 | clap.Param(void){ | ||
| 151 | .names = clap.Names{ .short = 'h', .long = "help" } | ||
| 152 | }, | ||
| 153 | }; | ||
| 154 | |||
| 155 | var direct_allocator = std.heap.DirectAllocator.init(); | ||
| 156 | const allocator = &direct_allocator.allocator; | ||
| 157 | defer direct_allocator.deinit(); | ||
| 79 | 158 | ||
| 80 | var iter = clap.args.OsIterator.init(allocator); | 159 | var iter = clap.args.OsIterator.init(allocator); |
| 81 | defer iter.deinit(); | 160 | defer iter.deinit(); |
| 82 | const exe = try iter.next(); | 161 | const exe = try iter.next(); |
| 83 | 162 | ||
| 84 | var args = try clap.ComptimeClap(void, params).parse(allocator, clap.args.OsIterator, &iter); | 163 | var args = try clap.ComptimeClap(void, params).parse(allocator, clap.args.OsIterator, &iter); |
| 85 | defer args.deinit(); | 164 | defer args.deinit(); |
| 165 | |||
| 166 | _ = args.flag("--helps"); | ||
| 167 | } | ||
| 86 | 168 | ||
| 87 | if (args.flag("--helps")) | ||
| 88 | debug.warn("Help!\n"); | ||
| 89 | ``` | 169 | ``` |
| 90 | 170 | ||
| 91 | ``` | 171 | ``` |
| 92 | zig-clap/src/comptime.zig:103:17: error: --helps is not a parameter. | 172 | zig-clap/src/comptime.zig:116:17: error: --helps is not a parameter. |
| 93 | @compileError(name ++ " is not a parameter."); | 173 | @compileError(name ++ " is not a parameter."); |
| 94 | ^ | 174 | ^ |
| 95 | zig-clap/src/comptime.zig:71:45: note: called from here | 175 | zig-clap/src/comptime.zig:84:45: note: called from here |
| 96 | const param = comptime findParam(name); | 176 | const param = comptime findParam(name); |
| 97 | ^ | 177 | ^ |
| 98 | zig-clap/example/comptime-clap.zig:41:18: note: called from here | 178 | zig-clap/example/comptime-clap-error.zig:22:18: note: called from here |
| 99 | if (args.flag("--helps")) | 179 | _ = args.flag("--helps"); |
| 100 | ^ | 180 | ^ |
| 101 | ``` | 181 | ``` |
| 102 | 182 | ||
| @@ -107,29 +187,38 @@ Ofc, this limits you to parameters that are comptime known. | |||
| 107 | The `help`, `helpEx` and `helpFull` are functions for printing a simple list of all parameters the | 187 | The `help`, `helpEx` and `helpFull` are functions for printing a simple list of all parameters the |
| 108 | program can take. | 188 | program can take. |
| 109 | 189 | ||
| 110 | ```rust | 190 | ```zig |
| 111 | const stderr_file = try std.io.getStdErr(); | 191 | const std = @import("std"); |
| 112 | var stderr_out_stream = stderr_file.outStream(); | 192 | const clap = @import("clap"); |
| 113 | const stderr = &stderr_out_stream.stream; | 193 | |
| 114 | 194 | pub fn main() !void { | |
| 115 | try clap.help( | 195 | const stderr_file = try std.io.getStdErr(); |
| 116 | stderr, | 196 | var stderr_out_stream = stderr_file.outStream(); |
| 117 | []clap.Param([]const u8){ | 197 | const stderr = &stderr_out_stream.stream; |
| 118 | clap.Param([]const u8).flag( | 198 | |
| 119 | "Display this help and exit.", | 199 | // clap.help is a function that can print a simple help message, given a |
| 120 | clap.Names.both("help"), | 200 | // slice of Param([]const u8). There is also a helpEx, which can print a |
| 121 | ), | 201 | // help message for any Param, but it is more verbose to call. |
| 122 | clap.Param([]const u8).flag( | 202 | try clap.help( |
| 123 | "Output version information and exit.", | 203 | stderr, |
| 124 | clap.Names.both("version"), | 204 | [_]clap.Param([]const u8){ |
| 125 | ), | 205 | clap.Param([]const u8){ |
| 126 | }, | 206 | .id = "Display this help and exit.", |
| 127 | ); | 207 | .names = clap.Names{ .short = 'h', .long = "help" } |
| 208 | }, | ||
| 209 | clap.Param([]const u8){ | ||
| 210 | .id = "Output version information and exit.", | ||
| 211 | .names = clap.Names{ .short = 'v', .long = "version" } | ||
| 212 | }, | ||
| 213 | }, | ||
| 214 | ); | ||
| 215 | } | ||
| 216 | |||
| 128 | ``` | 217 | ``` |
| 129 | 218 | ||
| 130 | ``` | 219 | ``` |
| 131 | -h, --help Display this help and exit. | 220 | -h, --help Display this help and exit. |
| 132 | -v, --version Output version information and exit. | 221 | -v, --version Output version information and exit. |
| 133 | ``` | 222 | ``` |
| 134 | 223 | ||
| 135 | The `help` function is the simplest to call. It only takes an `OutStream` and a slice of | 224 | The `help` function is the simplest to call. It only takes an `OutStream` and a slice of |