44 lines
1.2 KiB
Zig
44 lines
1.2 KiB
Zig
const std = @import("std");
|
|
const heap = std.heap;
|
|
const cli = @import("cli");
|
|
const Command = cli.Command;
|
|
const Option = cli.Option;
|
|
|
|
fn rootRun(cmd: *Command) anyerror!void {
|
|
const e = cmd.getIntOption("example");
|
|
std.debug.print("{d}\n", .{e});
|
|
}
|
|
|
|
fn subcommandRun(cmd: *Command) anyerror!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 = try Command.init(allocator, config);
|
|
const subcommand = try Command.init(allocator, .{
|
|
.short_description = "SubExample",
|
|
.long_description = "Subcommand example",
|
|
.usage = "subexample",
|
|
.name = "subexample",
|
|
.run = subcommandRun,
|
|
});
|
|
try root_command.addCommand(subcommand);
|
|
try root_command.addOption(Option{
|
|
.short = "e",
|
|
.long = "example",
|
|
.description = "example option",
|
|
.value_type = .INT,
|
|
.value_int = 3,
|
|
});
|
|
try root_command.execute();
|
|
}
|