diff options
Diffstat (limited to 'README.md')
| -rw-r--r-- | README.md | 47 |
1 files changed, 24 insertions, 23 deletions
| @@ -2,6 +2,10 @@ | |||
| 2 | 2 | ||
| 3 | A simple and easy to use command line argument parser library for Zig. | 3 | A simple and easy to use command line argument parser library for Zig. |
| 4 | 4 | ||
| 5 | Looking for a version that works with `zig master`? The `zig-master` branch has | ||
| 6 | you covered. It is maintained by people who live at head (not me) and is merged | ||
| 7 | into master on every `zig` release. | ||
| 8 | |||
| 5 | ## Features | 9 | ## Features |
| 6 | 10 | ||
| 7 | * Short arguments `-a` | 11 | * Short arguments `-a` |
| @@ -36,15 +40,15 @@ pub fn main() !void { | |||
| 36 | }, | 40 | }, |
| 37 | }; | 41 | }; |
| 38 | 42 | ||
| 39 | var args = try clap.parse(clap.Help, params, std.heap.direct_allocator); | 43 | var args = try clap.parse(clap.Help, ¶ms, std.heap.page_allocator); |
| 40 | defer args.deinit(); | 44 | defer args.deinit(); |
| 41 | 45 | ||
| 42 | if (args.flag("--help")) | 46 | if (args.flag("--help")) |
| 43 | debug.warn("--help\n"); | 47 | debug.warn("--help\n", .{}); |
| 44 | if (args.option("--number")) |n| | 48 | if (args.option("--number")) |n| |
| 45 | debug.warn("--number = {}\n", n); | 49 | debug.warn("--number = {}\n", .{ n }); |
| 46 | for (args.positionals()) |pos| | 50 | for (args.positionals()) |pos| |
| 47 | debug.warn("{}\n", pos); | 51 | debug.warn("{}\n", .{ pos }); |
| 48 | } | 52 | } |
| 49 | 53 | ||
| 50 | ``` | 54 | ``` |
| @@ -63,7 +67,7 @@ pub fn main() !void { | |||
| 63 | clap.parseParam("-h, --help Display this help and exit.") catch unreachable, | 67 | clap.parseParam("-h, --help Display this help and exit.") catch unreachable, |
| 64 | }; | 68 | }; |
| 65 | 69 | ||
| 66 | var args = try clap.parse(clap.Help, params, std.heap.direct_allocator); | 70 | var args = try clap.parse(clap.Help, ¶ms, std.heap.direct_allocator); |
| 67 | defer args.deinit(); | 71 | defer args.deinit(); |
| 68 | 72 | ||
| 69 | _ = args.flag("--helps"); | 73 | _ = args.flag("--helps"); |
| @@ -97,7 +101,7 @@ const clap = @import("clap"); | |||
| 97 | const debug = std.debug; | 101 | const debug = std.debug; |
| 98 | 102 | ||
| 99 | pub fn main() !void { | 103 | pub fn main() !void { |
| 100 | const allocator = std.heap.direct_allocator; | 104 | const allocator = std.heap.page_allocator; |
| 101 | 105 | ||
| 102 | // First we specify what parameters our program can take. | 106 | // First we specify what parameters our program can take. |
| 103 | // We can use `parseParam` to parse a string to a `Param(Help)` | 107 | // We can use `parseParam` to parse a string to a `Param(Help)` |
| @@ -115,15 +119,15 @@ pub fn main() !void { | |||
| 115 | defer iter.deinit(); | 119 | defer iter.deinit(); |
| 116 | 120 | ||
| 117 | // Parse the arguments | 121 | // Parse the arguments |
| 118 | var args = try clap.ComptimeClap(clap.Help, params).parse(allocator, clap.args.OsIterator, &iter); | 122 | var args = try clap.ComptimeClap(clap.Help, ¶ms).parse(allocator, clap.args.OsIterator, &iter); |
| 119 | defer args.deinit(); | 123 | defer args.deinit(); |
| 120 | 124 | ||
| 121 | if (args.flag("--help")) | 125 | if (args.flag("--help")) |
| 122 | debug.warn("--help\n"); | 126 | debug.warn("--help\n", .{}); |
| 123 | if (args.option("--number")) |n| | 127 | if (args.option("--number")) |n| |
| 124 | debug.warn("--number = {}\n", n); | 128 | debug.warn("--number = {}\n", .{ n }); |
| 125 | for (args.positionals()) |pos| | 129 | for (args.positionals()) |pos| |
| 126 | debug.warn("{}\n", pos); | 130 | debug.warn("{}\n", .{ pos }); |
| 127 | } | 131 | } |
| 128 | 132 | ||
| 129 | ``` | 133 | ``` |
| @@ -140,7 +144,7 @@ const clap = @import("clap"); | |||
| 140 | const debug = std.debug; | 144 | const debug = std.debug; |
| 141 | 145 | ||
| 142 | pub fn main() !void { | 146 | pub fn main() !void { |
| 143 | const allocator = std.heap.direct_allocator; | 147 | const allocator = std.heap.page_allocator; |
| 144 | 148 | ||
| 145 | // First we specify what parameters our program can take. | 149 | // First we specify what parameters our program can take. |
| 146 | const params = [_]clap.Param(u8){ | 150 | const params = [_]clap.Param(u8){ |
| @@ -166,7 +170,7 @@ pub fn main() !void { | |||
| 166 | 170 | ||
| 167 | // Initialize our streaming parser. | 171 | // Initialize our streaming parser. |
| 168 | var parser = clap.StreamingClap(u8, clap.args.OsIterator){ | 172 | var parser = clap.StreamingClap(u8, clap.args.OsIterator){ |
| 169 | .params = params, | 173 | .params = ¶ms, |
| 170 | .iter = &iter, | 174 | .iter = &iter, |
| 171 | }; | 175 | }; |
| 172 | 176 | ||
| @@ -174,13 +178,13 @@ pub fn main() !void { | |||
| 174 | while (try parser.next()) |arg| { | 178 | while (try parser.next()) |arg| { |
| 175 | // arg.param will point to the parameter which matched the argument. | 179 | // arg.param will point to the parameter which matched the argument. |
| 176 | switch (arg.param.id) { | 180 | switch (arg.param.id) { |
| 177 | 'h' => debug.warn("Help!\n"), | 181 | 'h' => debug.warn("Help!\n", .{}), |
| 178 | 'n' => debug.warn("--number = {}\n", arg.value.?), | 182 | 'n' => debug.warn("--number = {}\n", .{ arg.value.? }), |
| 179 | 183 | ||
| 180 | // arg.value == null, if arg.param.takes_value == false. | 184 | // arg.value == null, if arg.param.takes_value == false. |
| 181 | // Otherwise, arg.value is the value passed with the argument, such as "-a=10" | 185 | // Otherwise, arg.value is the value passed with the argument, such as "-a=10" |
| 182 | // or "-a 10". | 186 | // or "-a 10". |
| 183 | 'f' => debug.warn("{}\n", arg.value.?), | 187 | 'f' => debug.warn("{}\n", .{ arg.value.? }), |
| 184 | else => unreachable, | 188 | else => unreachable, |
| 185 | } | 189 | } |
| 186 | } | 190 | } |
| @@ -201,16 +205,15 @@ const std = @import("std"); | |||
| 201 | const clap = @import("clap"); | 205 | const clap = @import("clap"); |
| 202 | 206 | ||
| 203 | pub fn main() !void { | 207 | pub fn main() !void { |
| 204 | const stderr_file = try std.io.getStdErr(); | 208 | const stderr_file = std.io.getStdErr(); |
| 205 | var stderr_out_stream = stderr_file.outStream(); | 209 | var stderr_out_stream = stderr_file.outStream(); |
| 206 | const stderr = &stderr_out_stream.stream; | ||
| 207 | 210 | ||
| 208 | // clap.help is a function that can print a simple help message, given a | 211 | // clap.help is a function that can print a simple help message, given a |
| 209 | // slice of Param(Help). There is also a helpEx, which can print a | 212 | // slice of Param(Help). There is also a helpEx, which can print a |
| 210 | // help message for any Param, but it is more verbose to call. | 213 | // help message for any Param, but it is more verbose to call. |
| 211 | try clap.help( | 214 | try clap.help( |
| 212 | stderr, | 215 | stderr_out_stream, |
| 213 | comptime [_]clap.Param(clap.Help){ | 216 | comptime &[_]clap.Param(clap.Help){ |
| 214 | clap.parseParam("-h, --help Display this help and exit. ") catch unreachable, | 217 | clap.parseParam("-h, --help Display this help and exit. ") catch unreachable, |
| 215 | clap.parseParam("-v, --version Output version information and exit.") catch unreachable, | 218 | clap.parseParam("-v, --version Output version information and exit.") catch unreachable, |
| 216 | }, | 219 | }, |
| @@ -243,16 +246,14 @@ const std = @import("std"); | |||
| 243 | const clap = @import("clap"); | 246 | const clap = @import("clap"); |
| 244 | 247 | ||
| 245 | pub fn main() !void { | 248 | pub fn main() !void { |
| 246 | const stderr_file = try std.io.getStdErr(); | 249 | const stderr = std.io.getStdErr().outStream(); |
| 247 | var stderr_out_stream = stderr_file.outStream(); | ||
| 248 | const stderr = &stderr_out_stream.stream; | ||
| 249 | 250 | ||
| 250 | // clap.usage is a function that can print a simple usage message, given a | 251 | // clap.usage is a function that can print a simple usage message, given a |
| 251 | // slice of Param(Help). There is also a usageEx, which can print a | 252 | // slice of Param(Help). There is also a usageEx, which can print a |
| 252 | // usage message for any Param, but it is more verbose to call. | 253 | // usage message for any Param, but it is more verbose to call. |
| 253 | try clap.usage( | 254 | try clap.usage( |
| 254 | stderr, | 255 | stderr, |
| 255 | comptime [_]clap.Param(clap.Help){ | 256 | comptime &[_]clap.Param(clap.Help){ |
| 256 | clap.parseParam("-h, --help Display this help and exit. ") catch unreachable, | 257 | clap.parseParam("-h, --help Display this help and exit. ") catch unreachable, |
| 257 | clap.parseParam("-v, --version Output version information and exit.") catch unreachable, | 258 | clap.parseParam("-v, --version Output version information and exit.") catch unreachable, |
| 258 | clap.parseParam(" --value <N> Output version information and exit.") catch unreachable, | 259 | clap.parseParam(" --value <N> Output version information and exit.") catch unreachable, |