zig-cli/demo.zig

47 lines
1.3 KiB
Zig
Raw Normal View History

2024-01-14 10:07:26 +00:00
const std = @import("std");
const heap = std.heap;
const cli = @import("cli");
const Command = cli.Command;
const Option = cli.Option;
fn rootRun(cmd: *Command) void {
const e = cmd.getIntOption("example");
std.debug.print("{d}\n", .{e});
}
fn subcommandRun(cmd: *Command) void {
std.debug.print("{s}\n", .{cmd.config.name});
}
pub fn main() !void {
var gpa = heap.GeneralPurposeAllocator(.{}){};
const allocator = gpa.allocator();
const config = Command.CommandConfig{
.short_description = "Example",
.long_description = "Example command",
.usage = "example",
.name = "example",
.run = rootRun,
};
var root_command = Command.init(allocator, config);
try root_command.addCommand(Command.init(allocator, .{
.short_description = "SubExample",
.long_description = "Subcommand example",
.usage = "subexample",
.name = "subexample",
.run = subcommandRun,
}));
try root_command.addOption(Option{
.short = "e",
.long = "example",
.description = "example option",
.optional = false,
.global = false,
.value_type = .INT,
.value_int = 3,
});
try root_command.execute();
try root_command.help();
try root_command.printVersion();
}