diff options
Diffstat (limited to 'example/streaming-clap.zig')
| -rw-r--r-- | example/streaming-clap.zig | 22 |
1 files changed, 9 insertions, 13 deletions
diff --git a/example/streaming-clap.zig b/example/streaming-clap.zig index 9d99859..2a20bcb 100644 --- a/example/streaming-clap.zig +++ b/example/streaming-clap.zig | |||
| @@ -1,10 +1,3 @@ | |||
| 1 | const clap = @import("clap"); | ||
| 2 | const std = @import("std"); | ||
| 3 | |||
| 4 | const debug = std.debug; | ||
| 5 | const io = std.io; | ||
| 6 | const process = std.process; | ||
| 7 | |||
| 8 | pub fn main() !void { | 1 | pub fn main() !void { |
| 9 | const allocator = std.heap.page_allocator; | 2 | const allocator = std.heap.page_allocator; |
| 10 | 3 | ||
| @@ -22,7 +15,7 @@ pub fn main() !void { | |||
| 22 | .{ .id = 'f', .takes_value = .one }, | 15 | .{ .id = 'f', .takes_value = .one }, |
| 23 | }; | 16 | }; |
| 24 | 17 | ||
| 25 | var iter = try process.ArgIterator.initWithAllocator(allocator); | 18 | var iter = try std.process.ArgIterator.initWithAllocator(allocator); |
| 26 | defer iter.deinit(); | 19 | defer iter.deinit(); |
| 27 | 20 | ||
| 28 | // Skip exe argument | 21 | // Skip exe argument |
| @@ -32,7 +25,7 @@ pub fn main() !void { | |||
| 32 | // This is optional. You can also leave the `diagnostic` field unset if you | 25 | // This is optional. You can also leave the `diagnostic` field unset if you |
| 33 | // don't care about the extra information `Diagnostic` provides. | 26 | // don't care about the extra information `Diagnostic` provides. |
| 34 | var diag = clap.Diagnostic{}; | 27 | var diag = clap.Diagnostic{}; |
| 35 | var parser = clap.streaming.Clap(u8, process.ArgIterator){ | 28 | var parser = clap.streaming.Clap(u8, std.process.ArgIterator){ |
| 36 | .params = ¶ms, | 29 | .params = ¶ms, |
| 37 | .iter = &iter, | 30 | .iter = &iter, |
| 38 | .diagnostic = &diag, | 31 | .diagnostic = &diag, |
| @@ -41,19 +34,22 @@ pub fn main() !void { | |||
| 41 | // Because we use a streaming parser, we have to consume each argument parsed individually. | 34 | // Because we use a streaming parser, we have to consume each argument parsed individually. |
| 42 | while (parser.next() catch |err| { | 35 | while (parser.next() catch |err| { |
| 43 | // Report useful error and exit | 36 | // Report useful error and exit |
| 44 | diag.report(io.getStdErr().writer(), err) catch {}; | 37 | diag.report(std.io.getStdErr().writer(), err) catch {}; |
| 45 | return err; | 38 | return err; |
| 46 | }) |arg| { | 39 | }) |arg| { |
| 47 | // arg.param will point to the parameter which matched the argument. | 40 | // arg.param will point to the parameter which matched the argument. |
| 48 | switch (arg.param.id) { | 41 | switch (arg.param.id) { |
| 49 | 'h' => debug.print("Help!\n", .{}), | 42 | 'h' => std.debug.print("Help!\n", .{}), |
| 50 | 'n' => debug.print("--number = {s}\n", .{arg.value.?}), | 43 | 'n' => std.debug.print("--number = {s}\n", .{arg.value.?}), |
| 51 | 44 | ||
| 52 | // arg.value == null, if arg.param.takes_value == .none. | 45 | // arg.value == null, if arg.param.takes_value == .none. |
| 53 | // Otherwise, arg.value is the value passed with the argument, such as "-a=10" | 46 | // Otherwise, arg.value is the value passed with the argument, such as "-a=10" |
| 54 | // or "-a 10". | 47 | // or "-a 10". |
| 55 | 'f' => debug.print("{s}\n", .{arg.value.?}), | 48 | 'f' => std.debug.print("{s}\n", .{arg.value.?}), |
| 56 | else => unreachable, | 49 | else => unreachable, |
| 57 | } | 50 | } |
| 58 | } | 51 | } |
| 59 | } | 52 | } |
| 53 | |||
| 54 | const clap = @import("clap"); | ||
| 55 | const std = @import("std"); | ||