From 2eeb78ee98432c9fefefa728ba42871be5e15b49 Mon Sep 17 00:00:00 2001 From: Jimmi HC Date: Fri, 18 May 2018 16:01:55 +0200 Subject: Started work one a simpler core api * This api will then be used to recreate the current api * Users who don't want the complicate api can also use the simpler one --- core.zig | 78 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 core.zig (limited to 'core.zig') diff --git a/core.zig b/core.zig new file mode 100644 index 0000000..087de9b --- /dev/null +++ b/core.zig @@ -0,0 +1,78 @@ +const std = @import("std"); +const builtin = @import("builtin"); + +const os = std.os; +const is_windows = builtin.os == Os.windows; + +pub fn Param(comptime Id: type) type { + return struct { + const Self = this; + + id: Id, + short: ?u8, + long: ?[]const u8, + index: ?usize, + takes_value: bool, + required: bool, + + /// Initialize a parameter. + /// If ::name.len == 0, then it's a value parameter: "some-command value". + /// If ::name.len == 1, then it's a short parameter: "some-command -s". + /// If ::name.len > 1, then it's a long parameter: "some-command --long". + pub fn init(id: Id, name: []const u8) Self { + return { + .id = id, + .short = if (name.len == 1) name[0] else null, + .long = if (name.len > 1) name else null, + .index = null, + .takes_value = false, + .required = false, + }; + } + + pub fn with(param: &const Self, comptime field_name: []const u8, value: var) Self { + var res = *param; + @field(res, field_name) = value; + return res; + } + }; +} + +pub fn Arg(comptime Id: type) type { + return struct { + id: Id, + value: ?[]const u8, + }; +} + + +pub fn args() ArgIterator { + return ArgIterator.init(); +} + +pub fn Iterator(comptime Id: type) type { + return struct { + const Self = this; + const Buffer = if (is_windows) [1024 * 2]u8 else void; + + windows_buffer: Buffer, + params: Param(Id), + args: os.ArgIterator, + exe: []const u8, + + pub fn init(params: []const Param(Id)) Self { + return Self { + .params = params, + . + }; + } + + fn innerNext(iter: &Self) ?[]const u8 { + //if (builtin.os == Os.windows) { + // return iter.args.next(allocator); + //} else { + // return iter.args.nextPosix(); + //} + } + } +} -- cgit v1.2.3