diff options
Diffstat (limited to '')
| -rw-r--r-- | test.zig | 56 |
1 files changed, 38 insertions, 18 deletions
| @@ -198,29 +198,49 @@ test "clap: all" { | |||
| 198 | } | 198 | } |
| 199 | 199 | ||
| 200 | test "clap.Example" { | 200 | test "clap.Example" { |
| 201 | const program_args = [][]const u8{ | 201 | // Fake program arguments. Don't mind them |
| 202 | "-h", "--help", | 202 | const program_args = [][]const u8{ |
| 203 | "-v", "--version", | 203 | "-h", "--help", |
| 204 | "-v", "--version", | ||
| 204 | "file.zig", | 205 | "file.zig", |
| 205 | }; | 206 | }; |
| 206 | 207 | ||
| 207 | const warn = @import("std").debug.warn; | 208 | const warn = @import("std").debug.warn; |
| 208 | const c = @import("clap.zig"); | 209 | const c = @import("clap.zig"); |
| 209 | 210 | ||
| 210 | const params = []c.Param(u8){ | 211 | // Initialize the parameters you want your program to take. |
| 211 | c.Param(u8).init('h', false, c.Names.prefix("help")), | 212 | // `Param` has a type passed in, which will determin the type |
| 212 | c.Param(u8).init('v', false, c.Names.prefix("version")), | 213 | // of `Param.id`. This field can then be used to identify the |
| 213 | c.Param(u8).init('f', true, c.Names.none()), | 214 | // `Param`, or something else entirely. |
| 215 | // Example: You could have the `id` be a function, and then | ||
| 216 | // call it in the loop further down. | ||
| 217 | const params = []c.Param(u8){ | ||
| 218 | c.Param(u8).init('h', false, c.Names.prefix("help")), | ||
| 219 | c.Param(u8).init('v', false, c.Names.prefix("version")), | ||
| 220 | c.Param(u8).init('f', true, c.Names.none()), | ||
| 214 | }; | 221 | }; |
| 215 | 222 | ||
| 216 | var iter = &c.ArgSliceIterator.init(program_args).iter; | 223 | // Here, we use an `ArgSliceIterator` which iterates over |
| 217 | var parser = c.Clap(u8, c.ArgSliceIterator.Error).init(params, iter); | 224 | // a slice of arguments. For real program, you would probably |
| 218 | 225 | // use `OsArgIterator`. | |
| 219 | while (parser.next() catch unreachable) |arg| { | 226 | var iter = &c.ArgSliceIterator.init(program_args).iter; |
| 220 | switch (arg.param.id) { | 227 | var parser = c.Clap(u8, c.ArgSliceIterator.Error).init(params, iter); |
| 221 | 'h' => warn("Help!\n"), | 228 | |
| 222 | 'v' => warn("1.1.1\n"), | 229 | // Iterate over all arguments passed to the program. |
| 223 | 'f' => warn("{}\n", arg.value.?), | 230 | // In real code, you should probably handle the errors |
| 231 | // `parser.next` returns. | ||
| 232 | while (parser.next() catch unreachable) |arg| { | ||
| 233 | // `arg.param` is a pointer to its matching `Param` | ||
| 234 | // from the `params` array. | ||
| 235 | switch (arg.param.id) { | ||
| 236 | 'h' => warn("Help!\n"), | ||
| 237 | 'v' => warn("1.1.1\n"), | ||
| 238 | |||
| 239 | // `arg.value` is `null`, if `arg.param.takes_value` | ||
| 240 | // is `false`. Otherwise, `arg.value` is the value | ||
| 241 | // passed with the argument, such as `-a=10` or | ||
| 242 | // `-a 10`. | ||
| 243 | 'f' => warn("{}\n", arg.value.?), | ||
| 224 | else => unreachable, | 244 | else => unreachable, |
| 225 | } | 245 | } |
| 226 | } | 246 | } |