diff options
| author | 2018-05-20 01:21:15 +0200 | |
|---|---|---|
| committer | 2018-05-20 01:21:15 +0200 | |
| commit | a1f024342d33fc7fe54a657c3b07a05e33f631c3 (patch) | |
| tree | 44074ff67e1e90459beae9e8b309e79a1921ba67 /example.zig | |
| parent | core.zig tested and ready for action! (diff) | |
| download | zig-clap-a1f024342d33fc7fe54a657c3b07a05e33f631c3.tar.gz zig-clap-a1f024342d33fc7fe54a657c3b07a05e33f631c3.tar.xz zig-clap-a1f024342d33fc7fe54a657c3b07a05e33f631c3.zip | |
The old clap now uses core.zig to get the same func as before
Diffstat (limited to 'example.zig')
| -rw-r--r-- | example.zig | 73 |
1 files changed, 31 insertions, 42 deletions
diff --git a/example.zig b/example.zig index 4d63b02..14b5487 100644 --- a/example.zig +++ b/example.zig | |||
| @@ -1,12 +1,13 @@ | |||
| 1 | const std = @import("std"); | 1 | const std = @import("std"); |
| 2 | const clap = @import("clap.zig"); | 2 | const core = @import("core.zig"); |
| 3 | const clap = @import("extended.zig"); | ||
| 3 | 4 | ||
| 4 | const debug = std.debug; | 5 | const debug = std.debug; |
| 5 | const os = std.os; | 6 | const os = std.os; |
| 6 | 7 | ||
| 7 | const Clap = clap.Clap; | 8 | const Clap = clap.Clap; |
| 8 | const Command = clap.Command; | 9 | const Param = clap.Param; |
| 9 | const Argument = clap.Argument; | 10 | const Parser = clap.Parser; |
| 10 | 11 | ||
| 11 | const Options = struct { | 12 | const Options = struct { |
| 12 | print_values: bool, | 13 | print_values: bool, |
| @@ -34,46 +35,34 @@ const Options = struct { | |||
| 34 | // d = V=5 | 35 | // d = V=5 |
| 35 | 36 | ||
| 36 | pub fn main() !void { | 37 | pub fn main() !void { |
| 37 | const parser = comptime Clap(Options).init( | 38 | const parser = comptime Clap(Options) { |
| 38 | Options { | 39 | .defaults = Options { |
| 39 | .print_values = false, | 40 | .print_values = false, |
| 40 | .a = 0, | 41 | .a = 0, |
| 41 | .b = 0, | 42 | .b = 0, |
| 42 | .c = 0, | 43 | .c = 0, |
| 43 | .d = "", | 44 | .d = "", |
| 44 | } | 45 | }, |
| 45 | ) | 46 | .params = []Param { |
| 46 | .with("program_name", "My Test Command") | 47 | Param.init("a") |
| 47 | .with("author", "Hejsil") | 48 | .with("takes_value", Parser.int(i64, 10)), |
| 48 | .with("version", "v1") | 49 | Param.init("b") |
| 49 | .with("about", "Prints some values to the screen... Maybe.") | 50 | .with("takes_value", Parser.int(u64, 10)), |
| 50 | .with("command", Command.init("command") | 51 | Param.init("c") |
| 51 | .with("arguments", | 52 | .with("takes_value", Parser.int(u8, 10)), |
| 52 | []Argument { | 53 | Param.init("d") |
| 53 | Argument.arg("a") | 54 | .with("takes_value", Parser.string), |
| 54 | .with("help", "Set the a field of Option.") | 55 | Param.init("print_values") |
| 55 | .with("takes_value", clap.parse.int(i64, 10)), | 56 | .with("short", 'p') |
| 56 | Argument.arg("b") | 57 | .with("long", "print-values"), |
| 57 | .with("help", "Set the b field of Option.") | 58 | } |
| 58 | .with("takes_value", clap.parse.int(u64, 10)), | 59 | }; |
| 59 | Argument.arg("c") | ||
| 60 | .with("help", "Set the c field of Option.") | ||
| 61 | .with("takes_value", clap.parse.int(u8, 10)), | ||
| 62 | Argument.arg("d") | ||
| 63 | .with("help", "Set the d field of Option.") | ||
| 64 | .with("takes_value", clap.parse.string), | ||
| 65 | Argument.field("print_values") | ||
| 66 | .with("help", "Print all not 0 values.") | ||
| 67 | .with("short", 'p') | ||
| 68 | .with("long", "print-values"), | ||
| 69 | } | ||
| 70 | ) | ||
| 71 | ); | ||
| 72 | 60 | ||
| 73 | const args = try os.argsAlloc(debug.global_allocator); | 61 | var arg_iter = core.OsArgIterator.init(); |
| 74 | defer os.argsFree(debug.global_allocator, args); | 62 | const iter = &arg_iter.iter; |
| 63 | const command = iter.next(debug.global_allocator); | ||
| 75 | 64 | ||
| 76 | const options = try parser.parse(args[1..]); | 65 | const options = try parser.parse(debug.global_allocator, iter); |
| 77 | 66 | ||
| 78 | if (options.print_values) { | 67 | if (options.print_values) { |
| 79 | if (options.a != 0) debug.warn("a = {}\n", options.a); | 68 | if (options.a != 0) debug.warn("a = {}\n", options.a); |