diff options
| author | 2019-11-09 13:12:34 +0100 | |
|---|---|---|
| committer | 2019-11-09 13:12:34 +0100 | |
| commit | dc1431c20883677f8176033ed2fb8ee4360a1b8e (patch) | |
| tree | 179651a820cfcc031f597091620d8e381a6e26cb | |
| parent | fmt, mv src/ clap/ and run fmt on build (diff) | |
| download | zig-clap-dc1431c20883677f8176033ed2fb8ee4360a1b8e.tar.gz zig-clap-dc1431c20883677f8176033ed2fb8ee4360a1b8e.tar.xz zig-clap-dc1431c20883677f8176033ed2fb8ee4360a1b8e.zip | |
Breaking: OsIterator will now get the exe on init
| -rw-r--r-- | README.md | 14 | ||||
| -rw-r--r-- | clap/args.zig | 12 | ||||
| -rw-r--r-- | example/comptime-clap.zig | 7 | ||||
| -rw-r--r-- | example/streaming-clap.zig | 7 |
4 files changed, 18 insertions, 22 deletions
| @@ -48,13 +48,10 @@ pub fn main() !void { | |||
| 48 | 48 | ||
| 49 | // We then initialize an argument iterator. We will use the OsIterator as it nicely | 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. | 50 | // wraps iterating over arguments the most efficient way on each os. |
| 51 | var iter = clap.args.OsIterator.init(allocator); | 51 | var iter = try clap.args.OsIterator.init(allocator); |
| 52 | defer iter.deinit(); | 52 | defer iter.deinit(); |
| 53 | 53 | ||
| 54 | // Consume the exe arg. | 54 | // Initialize our streaming parser. |
| 55 | const exe = try iter.next(); | ||
| 56 | |||
| 57 | // Finally we initialize our streaming parser. | ||
| 58 | var parser = clap.StreamingClap(u8, clap.args.OsIterator){ | 55 | var parser = clap.StreamingClap(u8, clap.args.OsIterator){ |
| 59 | .params = params, | 56 | .params = params, |
| 60 | .iter = &iter, | 57 | .iter = &iter, |
| @@ -104,13 +101,10 @@ pub fn main() !void { | |||
| 104 | 101 | ||
| 105 | // We then initialize an argument iterator. We will use the OsIterator as it nicely | 102 | // We then initialize an argument iterator. We will use the OsIterator as it nicely |
| 106 | // wraps iterating over arguments the most efficient way on each os. | 103 | // wraps iterating over arguments the most efficient way on each os. |
| 107 | var iter = clap.args.OsIterator.init(allocator); | 104 | var iter = try clap.args.OsIterator.init(allocator); |
| 108 | defer iter.deinit(); | 105 | defer iter.deinit(); |
| 109 | 106 | ||
| 110 | // Consume the exe arg. | 107 | // Parse the arguments |
| 111 | const exe = try iter.next(); | ||
| 112 | |||
| 113 | // Finally we can parse the arguments | ||
| 114 | var args = try clap.ComptimeClap(clap.Help, params).parse(allocator, clap.args.OsIterator, &iter); | 108 | var args = try clap.ComptimeClap(clap.Help, params).parse(allocator, clap.args.OsIterator, &iter); |
| 115 | defer args.deinit(); | 109 | defer args.deinit(); |
| 116 | 110 | ||
diff --git a/clap/args.zig b/clap/args.zig index 4234ada..b699438 100644 --- a/clap/args.zig +++ b/clap/args.zig | |||
| @@ -50,11 +50,19 @@ pub const OsIterator = struct { | |||
| 50 | arena: heap.ArenaAllocator, | 50 | arena: heap.ArenaAllocator, |
| 51 | args: process.ArgIterator, | 51 | args: process.ArgIterator, |
| 52 | 52 | ||
| 53 | pub fn init(allocator: *mem.Allocator) OsIterator { | 53 | /// The executable path (this is the first argument passed to the program) |
| 54 | return OsIterator{ | 54 | /// TODO: Is it the right choice for this to be null? Maybe `init` should |
| 55 | /// return an error when we have no exe. | ||
| 56 | exe_arg: ?[]const u8, | ||
| 57 | |||
| 58 | pub fn init(allocator: *mem.Allocator) Error!OsIterator { | ||
| 59 | var res = OsIterator{ | ||
| 55 | .arena = heap.ArenaAllocator.init(allocator), | 60 | .arena = heap.ArenaAllocator.init(allocator), |
| 56 | .args = process.args(), | 61 | .args = process.args(), |
| 62 | .exe_arg = undefined, | ||
| 57 | }; | 63 | }; |
| 64 | res.exe_arg = try res.next(); | ||
| 65 | return res; | ||
| 58 | } | 66 | } |
| 59 | 67 | ||
| 60 | pub fn deinit(iter: *OsIterator) void { | 68 | pub fn deinit(iter: *OsIterator) void { |
diff --git a/example/comptime-clap.zig b/example/comptime-clap.zig index 8e0eb12..4d0ace3 100644 --- a/example/comptime-clap.zig +++ b/example/comptime-clap.zig | |||
| @@ -18,13 +18,10 @@ pub fn main() !void { | |||
| 18 | 18 | ||
| 19 | // We then initialize an argument iterator. We will use the OsIterator as it nicely | 19 | // We then initialize an argument iterator. We will use the OsIterator as it nicely |
| 20 | // wraps iterating over arguments the most efficient way on each os. | 20 | // wraps iterating over arguments the most efficient way on each os. |
| 21 | var iter = clap.args.OsIterator.init(allocator); | 21 | var iter = try clap.args.OsIterator.init(allocator); |
| 22 | defer iter.deinit(); | 22 | defer iter.deinit(); |
| 23 | 23 | ||
| 24 | // Consume the exe arg. | 24 | // Parse the arguments |
| 25 | const exe = try iter.next(); | ||
| 26 | |||
| 27 | // Finally we can parse the arguments | ||
| 28 | var args = try clap.ComptimeClap(clap.Help, params).parse(allocator, clap.args.OsIterator, &iter); | 25 | var args = try clap.ComptimeClap(clap.Help, params).parse(allocator, clap.args.OsIterator, &iter); |
| 29 | defer args.deinit(); | 26 | defer args.deinit(); |
| 30 | 27 | ||
diff --git a/example/streaming-clap.zig b/example/streaming-clap.zig index d15e5b7..0361d46 100644 --- a/example/streaming-clap.zig +++ b/example/streaming-clap.zig | |||
| @@ -25,13 +25,10 @@ pub fn main() !void { | |||
| 25 | 25 | ||
| 26 | // We then initialize an argument iterator. We will use the OsIterator as it nicely | 26 | // We then initialize an argument iterator. We will use the OsIterator as it nicely |
| 27 | // wraps iterating over arguments the most efficient way on each os. | 27 | // wraps iterating over arguments the most efficient way on each os. |
| 28 | var iter = clap.args.OsIterator.init(allocator); | 28 | var iter = try clap.args.OsIterator.init(allocator); |
| 29 | defer iter.deinit(); | 29 | defer iter.deinit(); |
| 30 | 30 | ||
| 31 | // Consume the exe arg. | 31 | // Initialize our streaming parser. |
| 32 | const exe = try iter.next(); | ||
| 33 | |||
| 34 | // Finally we initialize our streaming parser. | ||
| 35 | var parser = clap.StreamingClap(u8, clap.args.OsIterator){ | 32 | var parser = clap.StreamingClap(u8, clap.args.OsIterator){ |
| 36 | .params = params, | 33 | .params = params, |
| 37 | .iter = &iter, | 34 | .iter = &iter, |