NOCLIP/demo/demo.zig

88 lines
2.8 KiB
Zig
Raw Normal View History

2022-11-20 12:54:26 -08:00
const std = @import("std");
const noclip = @import("noclip");
const context: []const u8 = "hello friend";
const ContextType = @TypeOf(context);
2022-11-20 12:54:26 -08:00
const helpFlag = noclip.HelpFlag(.{ .UserContext = ContextType });
2022-11-20 12:54:26 -08:00
const subData: noclip.CommandData = .{ .name = "subcommand", .help = "this a sub command" };
const subFlag: noclip.StringOption(ContextType) = .{ .name = "meta", .short = "-m" };
const subArg: noclip.StringArg(ContextType) = .{ .name = "sub" };
const subSpec = .{ helpFlag, subFlag, subArg };
const subCommand: noclip.CommandParser(subData, subSpec, ContextType, subCallback) = .{};
fn wrecker(zontext: ContextType, input: []const u8) ![]const u8 {
std.debug.print("ctx: {s}\n", .{zontext});
return input;
}
const cdata: noclip.CommandData = .{ .name = "main", .help = "main CLI entry point" };
const flagCheck: noclip.FlagOption(ContextType) = .{
2022-11-20 12:54:26 -08:00
.name = "flag",
.truthy = .{ .short = "-f", .long = "--flag" },
.falsy = .{ .long = "--no-flag" },
};
const inputOption: noclip.StringOption(ContextType) = .{
2022-11-20 12:54:26 -08:00
.name = "input",
.short = "-i",
.long = "--input",
.handler = wrecker,
2022-11-20 12:54:26 -08:00
.envVar = "OPTS_INPUT",
};
const outputOption: noclip.StringOption(ContextType) = .{ .name = "output", .long = "--output", .default = "waoh" };
const numberOption: noclip.ValuedOption(.{ .Output = i32, .UserContext = ContextType }) = .{ .name = "number", .short = "-n", .long = "--number" };
const argCheck: noclip.StringArg(ContextType) = .{ .name = "argument" };
const argAgain: noclip.StringArg(ContextType) = .{ .name = "another", .default = "nope" };
2022-11-20 12:54:26 -08:00
const mainSpec = .{
helpFlag,
2022-11-20 12:54:26 -08:00
flagCheck,
inputOption,
outputOption,
numberOption,
argCheck,
argAgain,
subCommand,
};
pub fn subCallback(_: ContextType, result: noclip.CommandResult(subSpec, ContextType)) !void {
std.debug.print("subcommand {any}!!!\n", .{result});
2022-11-20 12:54:26 -08:00
}
pub fn mainCommand(_: ContextType, result: noclip.CommandResult(mainSpec, ContextType)) !void {
2022-11-20 12:54:26 -08:00
std.debug.print(
\\arguments: {{
\\ .flag = {any}
2022-11-20 12:54:26 -08:00
\\ .input = {s}
\\ .output = {s}
\\ .number = {d}
\\ .argument = {s}
\\ .another = {s}
\\}}
\\
,
.{
result.flag,
result.input,
result.output,
result.number,
result.argument,
result.another,
},
);
}
pub fn main() !void {
var command: noclip.CommandParser(cdata, mainSpec, ContextType, mainCommand) = .{};
2022-11-20 12:54:26 -08:00
var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
defer arena.deinit();
const allocator = arena.allocator();
var argit = try std.process.argsWithAllocator(allocator);
_ = argit.next();
try command.execute(allocator, std.process.ArgIterator, &argit, context);
2022-11-20 12:54:26 -08:00
}