78 lines
1.9 KiB
Zig
78 lines
1.9 KiB
Zig
|
|
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);
|
||
|
|
}
|