2022-11-20 12:54:26 -08:00
|
|
|
const std = @import("std");
|
|
|
|
const noclip = @import("noclip");
|
|
|
|
|
2022-11-26 20:29:23 -08:00
|
|
|
const context: []const u8 = "hello friend";
|
|
|
|
const ContextType = @TypeOf(context);
|
2022-11-20 12:54:26 -08:00
|
|
|
|
2022-11-26 20:29:23 -08:00
|
|
|
const helpFlag = noclip.HelpFlag(.{ .UserContext = ContextType });
|
2022-11-20 12:54:26 -08:00
|
|
|
|
2022-11-26 20:29:23 -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" },
|
|
|
|
};
|
2022-11-26 20:29:23 -08:00
|
|
|
const inputOption: noclip.StringOption(ContextType) = .{
|
2022-11-20 12:54:26 -08:00
|
|
|
.name = "input",
|
|
|
|
.short = "-i",
|
|
|
|
.long = "--input",
|
2022-11-26 20:29:23 -08:00
|
|
|
.handler = wrecker,
|
2022-11-20 12:54:26 -08:00
|
|
|
.envVar = "OPTS_INPUT",
|
|
|
|
};
|
2022-11-26 20:29:23 -08:00
|
|
|
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 = .{
|
2022-11-26 20:29:23 -08:00
|
|
|
helpFlag,
|
2022-11-20 12:54:26 -08:00
|
|
|
flagCheck,
|
|
|
|
inputOption,
|
|
|
|
outputOption,
|
|
|
|
numberOption,
|
|
|
|
argCheck,
|
|
|
|
argAgain,
|
|
|
|
subCommand,
|
|
|
|
};
|
|
|
|
|
2022-11-26 20:29:23 -08:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2022-11-26 20:29:23 -08:00
|
|
|
pub fn mainCommand(_: ContextType, result: noclip.CommandResult(mainSpec, ContextType)) !void {
|
2022-11-20 12:54:26 -08:00
|
|
|
std.debug.print(
|
|
|
|
\\arguments: {{
|
2022-11-26 20:29:23 -08:00
|
|
|
\\ .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 {
|
2022-11-26 20:29:23 -08:00
|
|
|
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();
|
|
|
|
|
2022-11-26 20:29:23 -08:00
|
|
|
try command.execute(allocator, std.process.ArgIterator, &argit, context);
|
2022-11-20 12:54:26 -08:00
|
|
|
}
|