[CREATE] create project

This commit is contained in:
alanisia 2023-09-20 18:49:38 +08:00
commit 94cb44d399
4 changed files with 100 additions and 0 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
zig-cache/
zig-out/

3
README.md Normal file
View File

@ -0,0 +1,3 @@
# Zig CLI
A command argument parser library for Zig.

18
build.zig Normal file
View File

@ -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);
}

77
cli.zig Normal file
View File

@ -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);
}