From dc1431c20883677f8176033ed2fb8ee4360a1b8e Mon Sep 17 00:00:00 2001 From: Jimmi Holst Christensen Date: Sat, 9 Nov 2019 13:12:34 +0100 Subject: Breaking: OsIterator will now get the exe on init --- README.md | 14 ++++---------- clap/args.zig | 12 ++++++++++-- example/comptime-clap.zig | 7 ++----- example/streaming-clap.zig | 7 ++----- 4 files changed, 18 insertions(+), 22 deletions(-) diff --git a/README.md b/README.md index ce83cf0..6c556b9 100644 --- a/README.md +++ b/README.md @@ -48,13 +48,10 @@ pub fn main() !void { // We then initialize an argument iterator. We will use the OsIterator as it nicely // wraps iterating over arguments the most efficient way on each os. - var iter = clap.args.OsIterator.init(allocator); + var iter = try clap.args.OsIterator.init(allocator); defer iter.deinit(); - // Consume the exe arg. - const exe = try iter.next(); - - // Finally we initialize our streaming parser. + // Initialize our streaming parser. var parser = clap.StreamingClap(u8, clap.args.OsIterator){ .params = params, .iter = &iter, @@ -104,13 +101,10 @@ pub fn main() !void { // We then initialize an argument iterator. We will use the OsIterator as it nicely // wraps iterating over arguments the most efficient way on each os. - var iter = clap.args.OsIterator.init(allocator); + var iter = try clap.args.OsIterator.init(allocator); defer iter.deinit(); - // Consume the exe arg. - const exe = try iter.next(); - - // Finally we can parse the arguments + // Parse the arguments var args = try clap.ComptimeClap(clap.Help, params).parse(allocator, clap.args.OsIterator, &iter); defer args.deinit(); 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 { arena: heap.ArenaAllocator, args: process.ArgIterator, - pub fn init(allocator: *mem.Allocator) OsIterator { - return OsIterator{ + /// The executable path (this is the first argument passed to the program) + /// TODO: Is it the right choice for this to be null? Maybe `init` should + /// return an error when we have no exe. + exe_arg: ?[]const u8, + + pub fn init(allocator: *mem.Allocator) Error!OsIterator { + var res = OsIterator{ .arena = heap.ArenaAllocator.init(allocator), .args = process.args(), + .exe_arg = undefined, }; + res.exe_arg = try res.next(); + return res; } 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 { // We then initialize an argument iterator. We will use the OsIterator as it nicely // wraps iterating over arguments the most efficient way on each os. - var iter = clap.args.OsIterator.init(allocator); + var iter = try clap.args.OsIterator.init(allocator); defer iter.deinit(); - // Consume the exe arg. - const exe = try iter.next(); - - // Finally we can parse the arguments + // Parse the arguments var args = try clap.ComptimeClap(clap.Help, params).parse(allocator, clap.args.OsIterator, &iter); defer args.deinit(); 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 { // We then initialize an argument iterator. We will use the OsIterator as it nicely // wraps iterating over arguments the most efficient way on each os. - var iter = clap.args.OsIterator.init(allocator); + var iter = try clap.args.OsIterator.init(allocator); defer iter.deinit(); - // Consume the exe arg. - const exe = try iter.next(); - - // Finally we initialize our streaming parser. + // Initialize our streaming parser. var parser = clap.StreamingClap(u8, clap.args.OsIterator){ .params = params, .iter = &iter, -- cgit v1.2.3