commit 94cb44d3992d6106f3821b9e0ac8ce24921b1501 Author: alanisia <2478196682@qq.com> Date: Wed Sep 20 18:49:38 2023 +0800 [CREATE] create project diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..e73c965 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +zig-cache/ +zig-out/ diff --git a/README.md b/README.md new file mode 100644 index 0000000..8e51aea --- /dev/null +++ b/README.md @@ -0,0 +1,3 @@ +# Zig CLI + +A command argument parser library for Zig. diff --git a/build.zig b/build.zig new file mode 100644 index 0000000..184ebb1 --- /dev/null +++ b/build.zig @@ -0,0 +1,18 @@ +const std = @import("std"); + +pub fn build(b: *std.Build) void { + // b.addModule("cli", .{ .source_file = .{ .path = "cli.zig" } }); + + const target = b.standardTargetOptions(.{}); + const optimize = b.standardOptimizeOption(.{}); + + const unit_tests = b.addTest(.{ + .root_source_file = .{ .path = "cli.zig" }, + .target = target, + .optimize = optimize, + }); + + const run_unit_tests = b.addRunArtifact(unit_tests); + const test_step = b.step("test", "Run unit tests"); + test_step.dependOn(&run_unit_tests.step); +} diff --git a/cli.zig b/cli.zig new file mode 100644 index 0000000..6912efc --- /dev/null +++ b/cli.zig @@ -0,0 +1,77 @@ +const std = @import("std"); +const heap = std.heap; +const mem = std.mem; +const process = std.process; +const testing = std.testing; +const Allocator = std.mem.Allocator; +const Arena = std.heap.ArenaAllocator; + +pub fn Option(T: type) Option { + return struct { + const Self = @This(); + const Action = fn (e: T) void; + + short: []const u8, + long: []const u8, + description: []const u8, + value: T, + action: Action, + }; +} + +pub const Command = struct { + name: []const u8, + commands: std.ArrayList(*Command), + options: std.ArrayList(*Option), + allocator: *Allocator, + + pub fn init(name: []const u8, allocator: *Allocator) Command { + const command = Command{ + .name = name, + .allocator = allocator, + }; + command.commands = std.ArrayList(*Command).init(command.allocator); + command.options = std.ArrayList(*Option).init(command.allocator); + return command; + } + + pub fn deinit(self: *Command) void { + self.commands.deinit(); + self.options.deinit(); + } + + pub fn addCommand(self: *Command, command: *Command) void { + _ = command; + _ = self; + } + + pub fn addOption(self: *Command, option: *Option) void { + _ = option; + _ = self; + } +}; + +pub const Parser = struct { + allocator: *Allocator, + command: Command, + + pub fn parse(self: *Parser, argsIterator: process.ArgIterator) void { + _ = self; + _ = argsIterator.skip(); + for (argsIterator.next()) |arg| { + _ = arg; + } + } +}; + +test { + const argsIterator = process.args(); + const arena = Arena.init(heap.page_allocator); + const allocator = arena.child_allocator; + defer arena.deinit(); + + const command = Command{ .name = "test" }; + + const parser = Parser{ .allocator = allocator, .command = command }; + parser.parse(argsIterator); +}