zig-cli/demo.zig

44 lines
1.2 KiB
Zig
Raw Permalink 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;
2024-01-16 05:57:39 +00:00
fn rootRun(cmd: *Command) anyerror!void {
2024-01-14 10:07:26 +00:00
const e = cmd.getIntOption("example");
std.debug.print("{d}\n", .{e});
}
2024-01-16 05:57:39 +00:00
fn subcommandRun(cmd: *Command) anyerror!void {
2024-01-14 10:07:26 +00:00
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,
};
2024-01-16 05:57:39 +00:00
var root_command = try Command.init(allocator, config);
const subcommand = try Command.init(allocator, .{
2024-01-14 10:07:26 +00:00
.short_description = "SubExample",
.long_description = "Subcommand example",
.usage = "subexample",
.name = "subexample",
.run = subcommandRun,
2024-01-16 05:57:39 +00:00
});
try root_command.addCommand(subcommand);
2024-01-14 10:07:26 +00:00
try root_command.addOption(Option{
.short = "e",
.long = "example",
.description = "example option",
.value_type = .INT,
.value_int = 3,
});
try root_command.execute();
}