disaster, wrought by mine own hands
This commit is contained in:
239
demo/demo.zig
239
demo/demo.zig
@@ -1,13 +1,12 @@
|
||||
const std = @import("std");
|
||||
const noclip = @import("noclip");
|
||||
|
||||
const CommandBuilder = noclip.CommandBuilder;
|
||||
|
||||
const Choice = enum { first, second };
|
||||
|
||||
const cli = cmd: {
|
||||
var cmd = CommandBuilder(*u32){
|
||||
.description =
|
||||
const U8Tuple = struct { u8, u8 };
|
||||
|
||||
const Main = struct {
|
||||
pub const description =
|
||||
\\The definitive noclip demonstration utility.
|
||||
\\
|
||||
\\This command demonstrates the functionality of the noclip library. cool!
|
||||
@@ -15,7 +14,7 @@ const cli = cmd: {
|
||||
\\> // implementing factorial recursively is a silly thing to do
|
||||
\\> pub fn fact(n: u64) u64 {
|
||||
\\> if (n == 0) return 1;
|
||||
\\> return n*fact(n - 1);
|
||||
\\> return n * fact(n - 1);
|
||||
\\> }
|
||||
\\
|
||||
\\Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor
|
||||
@@ -24,131 +23,143 @@ const cli = cmd: {
|
||||
\\Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore
|
||||
\\eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident,
|
||||
\\sunt in culpa qui officia deserunt mollit anim id est laborum.
|
||||
,
|
||||
};
|
||||
cmd.addOption(.{ .OutputType = struct { u8, u8 } }, .{
|
||||
.name = "test",
|
||||
.short_tag = "-t",
|
||||
.long_tag = "--test",
|
||||
.env_var = "NOCLIP_TEST",
|
||||
.description = "multi-value test option",
|
||||
.nice_type_name = "int> <int",
|
||||
});
|
||||
cmd.addOption(.{ .OutputType = Choice }, .{
|
||||
.name = "choice",
|
||||
.short_tag = "-c",
|
||||
.long_tag = "--choice",
|
||||
.default = .second,
|
||||
.env_var = "NOCLIP_CHOICE",
|
||||
.description = "enum choice option",
|
||||
.nice_type_name = "choice",
|
||||
});
|
||||
cmd.stringOption(.{
|
||||
.name = "string",
|
||||
.short_tag = "-s",
|
||||
.long_tag = "--string",
|
||||
.env_var = "NOCLIP_STRING",
|
||||
.description = "A string value option",
|
||||
});
|
||||
cmd.addOption(.{ .OutputType = u32 }, .{
|
||||
.name = "default",
|
||||
.short_tag = "-d",
|
||||
.long_tag = "--default",
|
||||
.env_var = "NOCLIP_DEFAULT",
|
||||
.default = 100,
|
||||
.description = "default value integer option",
|
||||
.nice_type_name = "uint",
|
||||
});
|
||||
cmd.addOption(.{ .OutputType = u8, .multi = true }, .{
|
||||
.name = "multi",
|
||||
.short_tag = "-m",
|
||||
.long_tag = "--multi",
|
||||
.description = "multiple specification test option",
|
||||
});
|
||||
cmd.addFlag(.{}, .{
|
||||
.name = "flag",
|
||||
.truthy = .{ .short_tag = "-f", .long_tag = "--flag" },
|
||||
.falsy = .{ .short_tag = "-F", .long_tag = "--no-flag" },
|
||||
.env_var = "NOCLIP_FLAG",
|
||||
.description = "boolean flag",
|
||||
});
|
||||
cmd.addFlag(.{ .multi = true }, .{
|
||||
.name = "multiflag",
|
||||
.truthy = .{ .short_tag = "-M" },
|
||||
.description = "multiple specification test flag ",
|
||||
});
|
||||
cmd.addOption(.{ .OutputType = u8 }, .{
|
||||
.name = "env",
|
||||
.env_var = "NOCLIP_ENVIRON",
|
||||
.description = "environment variable only option",
|
||||
});
|
||||
;
|
||||
|
||||
break :cmd cmd;
|
||||
pub const options: noclip.CommandOptions = .{};
|
||||
|
||||
pub const parameters = struct {
|
||||
// pub const tuple: noclip.Option(U8Tuple) = .{
|
||||
// .short = 't',
|
||||
// .long = "tuple",
|
||||
// .env = "NOCLIP_TEST",
|
||||
// .description = "tuple test option",
|
||||
// };
|
||||
pub const choice: noclip.Option(Choice) = .{
|
||||
.short = 'c',
|
||||
.long = "choice",
|
||||
.env = "NOCLIP_CHOICE",
|
||||
.description = "enum choice option",
|
||||
};
|
||||
pub const string: noclip.Option(noclip.String) = .{
|
||||
.short = 's',
|
||||
.long = "string",
|
||||
.env_var = "NOCLIP_STRING",
|
||||
.description = "A string value option",
|
||||
};
|
||||
pub const default: noclip.Option(u32) = .{
|
||||
.name = "default",
|
||||
.short = 'd',
|
||||
.long = "default",
|
||||
.env_var = "NOCLIP_DEFAULT",
|
||||
.default = 100,
|
||||
.description = "default value integer option",
|
||||
.nice_type_name = "uint",
|
||||
};
|
||||
pub const multi: noclip.Option(noclip.Accumulate(u8)) = .{
|
||||
.name = "multi",
|
||||
.short_tag = 'm',
|
||||
.long_tag = "multi",
|
||||
.description = "multiple specification test option",
|
||||
};
|
||||
pub const flag: noclip.FlagSet = .{
|
||||
.name = "flag",
|
||||
.truthy = .{ .short = 'f', .long = "flag" },
|
||||
.falsy = .{ .short = 'F', .long = "no-flag" },
|
||||
.env = "NOCLIP_FLAG",
|
||||
.description = "boolean flag",
|
||||
};
|
||||
// pub const multiflag: noclip.Flag = .{
|
||||
// .name = "multiflag",
|
||||
// .truthy = .{ .short = 'M' },
|
||||
// .description = "multiple specification test flag",
|
||||
// .multi = .accumulate,
|
||||
// };
|
||||
pub const env_only: noclip.Option(u8) = .{
|
||||
.env = "NOCLIP_ENVIRON",
|
||||
.description = "environment variable only option",
|
||||
};
|
||||
// pub const group: noclip.Group(bool) = .{
|
||||
// .parameters = struct {
|
||||
// pub const a: noclip.Flag = .{
|
||||
// .truthy = .{ .short = 'z' },
|
||||
// };
|
||||
// },
|
||||
// };
|
||||
};
|
||||
|
||||
pub const subcommands = struct {
|
||||
pub const subcommand = Subcommand;
|
||||
pub const deeply = struct {
|
||||
pub const info: noclip.CommandInfo = .{ .description = "start of a deeply nested subcommand" };
|
||||
pub const subcommands = struct {
|
||||
pub const nested = struct {
|
||||
pub const info: noclip.CommandInfo = .{ .description = "second level of a deeply nested subcommand" };
|
||||
pub const subcommands = struct {
|
||||
pub const subcommand = struct {
|
||||
pub const info: noclip.CommandInfo = .{ .description = "third level of a deeply nested subcommand" };
|
||||
pub const subcommands = struct {
|
||||
pub const group = struct {
|
||||
pub const info: noclip.CommandInfo = .{ .description = "final level of a deeply nested subcommand" };
|
||||
pub fn run() void {
|
||||
std.debug.print("but it does nothing\n");
|
||||
}
|
||||
};
|
||||
};
|
||||
};
|
||||
};
|
||||
};
|
||||
};
|
||||
};
|
||||
};
|
||||
|
||||
pub fn run(args: noclip.Result(Main)) void {
|
||||
if (args.choice) |choice| {
|
||||
std.debug.print("You chose: {s}\n", .{@tagName(choice)});
|
||||
} else {
|
||||
std.debug.print("You chose nothing!\n", .{});
|
||||
}
|
||||
}
|
||||
|
||||
pub fn err(report: noclip.ErrorReport) void {
|
||||
_ = report;
|
||||
}
|
||||
};
|
||||
|
||||
const subcommand = cmd: {
|
||||
var cmd = CommandBuilder([]const u8){
|
||||
const Subcommand = struct {
|
||||
pub const info: noclip.CommandInfo = .{
|
||||
.name = "subcommand",
|
||||
.description =
|
||||
\\Demonstrate subcommand functionality
|
||||
\\
|
||||
\\This command demonstrates how subcommands work.
|
||||
,
|
||||
.options = .{ .parse_error_behavior = .exit },
|
||||
};
|
||||
|
||||
pub const parameters = struct {
|
||||
pub const flag: noclip.Flag = .{
|
||||
.truthy = .{ .short = 'f', .long = "flag" },
|
||||
.falsy = .{ .long = "no-flag" },
|
||||
.env_var = "NOCLIP_SUBFLAG",
|
||||
};
|
||||
pub const first_arg: noclip.Argument(noclip.String) = .{};
|
||||
pub const second_arg: noclip.Argument(noclip.String) = .{
|
||||
.description = "This is an argument that doesn't really do anything, but it's very important.",
|
||||
};
|
||||
};
|
||||
cmd.simpleFlag(.{
|
||||
.name = "flag",
|
||||
.truthy = .{ .short_tag = "-f", .long_tag = "--flag" },
|
||||
.falsy = .{ .long_tag = "--no-flag" },
|
||||
.env_var = "NOCLIP_SUBFLAG",
|
||||
});
|
||||
cmd.stringArgument(.{ .name = "argument" });
|
||||
cmd.stringArgument(.{
|
||||
.name = "arg",
|
||||
.description = "This is an argument that doesn't really do anything, but it's very important.",
|
||||
});
|
||||
break :cmd cmd;
|
||||
};
|
||||
|
||||
fn subHandler(context: []const u8, result: subcommand.Output()) !void {
|
||||
std.debug.print("subcommand: {s}\n", .{result.argument});
|
||||
std.debug.print("context: {s}\n", .{context});
|
||||
}
|
||||
|
||||
fn cliHandler(context: *u32, result: cli.Output()) !void {
|
||||
std.debug.print("context: {d}\n", .{context.*});
|
||||
std.debug.print("callback is working {s}\n", .{result.string orelse "null"});
|
||||
std.debug.print("callback is working {any}\n", .{result.choice});
|
||||
std.debug.print("callback is working {d}\n", .{result.default});
|
||||
context.* += 1;
|
||||
}
|
||||
|
||||
pub fn main() !u8 {
|
||||
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
|
||||
defer _ = gpa.deinit();
|
||||
const allocator = gpa.allocator();
|
||||
|
||||
const base = try noclip.commandGroup(allocator, .{ .description = "base group" });
|
||||
defer base.deinitTree();
|
||||
const args = try std.process.argsAlloc(allocator);
|
||||
defer std.process.argsFree(allocator, args);
|
||||
var env = try std.process.getEnvMap(allocator);
|
||||
defer env.deinit();
|
||||
|
||||
var context: u32 = 2;
|
||||
const sc: []const u8 = "whassup";
|
||||
|
||||
try base.addSubcommand("main", try cli.createInterface(allocator, cliHandler, &context));
|
||||
try base.addSubcommand("other", try subcommand.createInterface(allocator, subHandler, &sc));
|
||||
|
||||
const group = try noclip.commandGroup(allocator, .{ .description = "final level of a deeply nested subcommand" });
|
||||
const subcon = try noclip.commandGroup(allocator, .{ .description = "third level of a deeply nested subcommand" });
|
||||
const nested = try noclip.commandGroup(allocator, .{ .description = "second level of a deeply nested subcommand" });
|
||||
const deeply = try noclip.commandGroup(allocator, .{ .description = "start of a deeply nested subcommand" });
|
||||
try base.addSubcommand("deeply", deeply);
|
||||
try deeply.addSubcommand("nested", nested);
|
||||
try nested.addSubcommand("subcommand", subcon);
|
||||
try subcon.addSubcommand("group", group);
|
||||
try group.addSubcommand("run", try cli.createInterface(allocator, cliHandler, &context));
|
||||
|
||||
base.execute() catch |err| {
|
||||
std.io.getStdErr().writeAll(base.getParseError()) catch {};
|
||||
return err;
|
||||
};
|
||||
try noclip.execute(Main, .{ .args = args, .env = env });
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
Reference in New Issue
Block a user