From e33f6377f0718880908f9712ba47d83c2c9abfb9 Mon Sep 17 00:00:00 2001 From: Meghan Denny Date: Sat, 11 Sep 2021 01:31:19 -0700 Subject: [PATCH] initial code implementation --- .gitignore | 3 +++ build.zig | 27 +++++++++++++++++++ src/lib.zig | 73 ++++++++++++++++++++++++++++++++++++++++++++++++++++ src/main.zig | 5 ++++ zig.mod | 7 +++++ zigmod.lock | 2 ++ 6 files changed, 117 insertions(+) create mode 100644 .gitignore create mode 100644 build.zig create mode 100644 src/lib.zig create mode 100644 src/main.zig create mode 100644 zig.mod create mode 100644 zigmod.lock diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..71969716e3e9ca87fdafa1839c25c6daf7da7560 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +zig-* +.zigmod +deps.zig diff --git a/build.zig b/build.zig new file mode 100644 index 0000000000000000000000000000000000000000..d102e72285bc44cea10e17974fd8144642404933 --- /dev/null +++ b/build.zig @@ -0,0 +1,27 @@ +const std = @import("std"); + +pub fn build(b: *std.build.Builder) void { + // Standard target options allows the person running `zig build` to choose + // what target to build for. Here we do not override the defaults, which + // means any target is allowed, and the default is native. Other options + // for restricting supported target set are available. + const target = b.standardTargetOptions(.{}); + + // Standard release options allow the person running `zig build` to select + // between Debug, ReleaseSafe, ReleaseFast, and ReleaseSmall. + const mode = b.standardReleaseOptions(); + + const exe = b.addExecutable("zig-flag", "src/main.zig"); + exe.setTarget(target); + exe.setBuildMode(mode); + exe.install(); + + const run_cmd = exe.run(); + run_cmd.step.dependOn(b.getInstallStep()); + if (b.args) |args| { + run_cmd.addArgs(args); + } + + const run_step = b.step("run", "Run the app"); + run_step.dependOn(&run_cmd.step); +} diff --git a/src/lib.zig b/src/lib.zig new file mode 100644 index 0000000000000000000000000000000000000000..a552eac231b9ab36562fbfcef1047c05bfd80afc --- /dev/null +++ b/src/lib.zig @@ -0,0 +1,73 @@ +const std = @import("std"); +const string = []const u8; +const List = std.ArrayList(string); +const extras = @import("extras"); + +var singles: std.StringArrayHashMap(string) = undefined; +var multis: std.StringArrayHashMap(List) = undefined; + +pub fn init(alloc: *std.mem.Allocator) void { + singles = std.StringArrayHashMap(string).init(alloc); + multis = std.StringArrayHashMap(List).init(alloc); +} + +pub fn addSingle(name: string) !void { + try singles.putNoClobber(name, ""); +} + +pub fn addMulti(name: string) !void { + try multis.putNoClobber(name, List.init(multis.allocator)); +} + +pub const FlagDashKind = enum { + single, + double, + + pub fn hypen(self: FlagDashKind) string { + return switch (self) { + .single => "-", + .double => "--", + }; + } +}; + +pub fn parse(k: FlagDashKind) !std.process.ArgIterator { + const dash = k.hypen(); + var argiter = std.process.args(); + defer argiter.deinit(); + var argi: usize = 0; + blk: while (argiter.next(singles.allocator)) |item| : (argi += 1) { + if (argi == 0) continue; + const data = try item; + const name = extras.trimPrefix(data, dash); + if (data.len == name.len) return error.BadFlag; + + for (singles.keys()) |jtem| { + if (std.mem.eql(u8, name, jtem)) { + const value = try argiter.next(singles.allocator).?; + try singles.put(name, value); + continue :blk; + } + } + for (multis.keys()) |jtem| { + if (std.mem.eql(u8, name, jtem)) { + const value = try argiter.next(multis.allocator).?; + try multis.get(name).?.append(value); + continue :blk; + } + } + std.log.err("Unrecognized argument: {s}{s}", .{ dash, name }); + std.os.exit(1); + } + return argiter; +} + +pub fn getSingle(name: string) ?string { + const x = singles.get(name).?; + return if (x.len > 0) x else null; +} + +pub fn getMulti(name: string) ?[]const string { + const x = multis.get(name).?.toOwnedSlice(); + return if (x.len > 0) x else null; +} diff --git a/src/main.zig b/src/main.zig new file mode 100644 index 0000000000000000000000000000000000000000..d29869ff88feb94cd5a5fd28d760824920f269ac --- /dev/null +++ b/src/main.zig @@ -0,0 +1,5 @@ +const std = @import("std"); + +pub fn main() anyerror!void { + std.log.info("All your codebase are belong to us.", .{}); +} diff --git a/zig.mod b/zig.mod new file mode 100644 index 0000000000000000000000000000000000000000..4f6013c15483e8dabf3b309637cc83122bfa7501 --- /dev/null +++ b/zig.mod @@ -0,0 +1,7 @@ +id: pm68dn67ppvlmnp1taw31k1t9exei2oqvfx2l9nuxgmeeo32 +name: flag +main: src/lib.zig +license: MIT +description: Config management for Zig. +dependencies: + - src: git https://github.com/nektro/zig-extras diff --git a/zigmod.lock b/zigmod.lock new file mode 100644 index 0000000000000000000000000000000000000000..c859ef41f2f20dc660bcff21a94fbf466d739d07 --- /dev/null +++ b/zigmod.lock @@ -0,0 +1,2 @@ +2 +git https://github.com/nektro/zig-extras commit-0657e287853475843c27660242e9bbb117374c6b -- 2.54.0