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-27 01:31:20 -08:00
|
|
|
const subcommand = blk: {
|
|
|
|
var cmd = noclip.Command(ContextType, .{ .name = "subcommand", .help = "this a sub command" });
|
|
|
|
cmd.add(cmd.defaultHelpFlag);
|
|
|
|
cmd.add(cmd.StringOption{ .name = "meta", .short = "-m" });
|
|
|
|
cmd.add(cmd.StringArgument{ .name = "sub" });
|
|
|
|
break :blk cmd;
|
|
|
|
};
|
2022-11-26 20:29:23 -08:00
|
|
|
|
2022-11-27 01:31:20 -08:00
|
|
|
const command = blk: {
|
|
|
|
var cmd = noclip.Command(ContextType, .{ .name = "main", .help = "main CLI entry point" });
|
|
|
|
cmd.add(cmd.Flag{ .name = "flag", .truthy = .{ .short = "-f", .long = "--flag" }, .falsy = .{ .long = "--no-flag" } });
|
|
|
|
cmd.add(cmd.StringOption{
|
|
|
|
.name = "input",
|
|
|
|
.short = "-i",
|
|
|
|
.long = "--input",
|
|
|
|
.handler = printHandler,
|
|
|
|
.envVar = "OPTS_INPUT",
|
|
|
|
});
|
|
|
|
cmd.add(cmd.StringOption{ .name = "output", .long = "--output", .default = "waoh" });
|
|
|
|
cmd.add(cmd.Option(i32){ .name = "number", .short = "-n", .long = "--number" });
|
|
|
|
cmd.add(cmd.StringArgument{ .name = "argument" });
|
|
|
|
cmd.add(cmd.Argument(u32){ .name = "another", .default = 0 });
|
2022-11-26 20:29:23 -08:00
|
|
|
|
2022-11-27 01:31:20 -08:00
|
|
|
cmd.add(subcommand.Parser(subCallback));
|
|
|
|
break :blk cmd;
|
2022-11-20 12:54:26 -08:00
|
|
|
};
|
|
|
|
|
2022-11-27 01:31:20 -08:00
|
|
|
fn printHandler(ctx: ContextType, input: []const u8) ![]const u8 {
|
|
|
|
std.debug.print("ctx: {s}\n", .{ctx});
|
|
|
|
return input;
|
|
|
|
}
|
2022-11-20 12:54:26 -08:00
|
|
|
|
2022-11-27 01:31:20 -08:00
|
|
|
pub fn subCallback(_: ContextType, result: subcommand.CommandResult()) !void {
|
|
|
|
std.debug.print(
|
|
|
|
\\subcommand: {{
|
|
|
|
\\ .meta = {s}
|
|
|
|
\\ .sub = {s}
|
|
|
|
\\}}
|
|
|
|
\\
|
|
|
|
,
|
|
|
|
.{ result.meta, result.sub },
|
|
|
|
);
|
2022-11-20 12:54:26 -08:00
|
|
|
}
|
|
|
|
|
2022-11-27 01:31:20 -08:00
|
|
|
pub fn mainCommand(_: ContextType, result: command.CommandResult()) !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}
|
2022-11-27 01:31:20 -08:00
|
|
|
\\ .another = {d}
|
2022-11-20 12:54:26 -08:00
|
|
|
\\}}
|
|
|
|
\\
|
|
|
|
,
|
|
|
|
.{
|
|
|
|
result.flag,
|
|
|
|
result.input,
|
|
|
|
result.output,
|
|
|
|
result.number,
|
|
|
|
result.argument,
|
|
|
|
result.another,
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn main() !void {
|
2022-11-27 01:31:20 -08:00
|
|
|
var parser = command.Parser(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-27 01:31:20 -08:00
|
|
|
try parser.execute(allocator, std.process.ArgIterator, &argit, context);
|
2022-11-20 12:54:26 -08:00
|
|
|
}
|