A blazing-fast, zero-cost CLI framework for Zig β inspired by Cobra (Go) and clap (Rust).
Build modular, ergonomic, and high-performance CLIs with ease.
Tip
π§± Each command is modular and self-contained.
See docs.md for full usage, examples, and internals.
- Modular commands & subcommands
- Fast flag parsing (
--flag
,--flag=value
, shorthand-abc
) - Type-safe support for
bool
,int
,string
- Auto help/version/deprecation handling
- Works like Cobra or clap, but in Zig
zig fetch --save=zli https://github.com/xcaeser/zli/archive/v3.1.5.tar.gz
Add to your build.zig
:
const zli_dep = b.dependency("zli", .{ .target = target });
exe.root_module.addImport("zli", zli_dep.module("zli"));
your-app/
βββ build.zig
βββ src/
β βββ main.zig
β βββ cli/
β βββ root.zig
β βββ run.zig
β βββ version.zig
- Each command is in its own file
- You explicitly register subcommands
- Root is the entry point
// src/main.zig
const std = @import("std");
const cli = @import("cli/root.zig");
pub fn main() !void {
const allocator = std.heap.smp_allocator;
var root = try cli.build(allocator);
defer root.deinit();
try root.execute();
}
// src/cli/root.zig
const std = @import("std");
const zli = @import("zli");
const run = @import("run.zig");
const version = @import("version.zig");
pub fn build(allocator: std.mem.Allocator) !*zli.Command {
const root = try zli.Command.init(allocator, .{
.name = "blitz",
.description = "Your dev toolkit CLI",
}, showHelp);
try root.addCommands(&.{
try run.register(allocator),
try version.register(allocator),
});
return root;
}
fn showHelp(ctx: zli.CommandContext) !void {
try ctx.command.printHelp();
// try ctx.command.listCommands();
}
// src/cli/run.zig
const std = @import("std");
const zli = @import("zli");
const now_flag = zli.Flag{
.name = "now",
.shortcut = "n",
.description = "Run immediately",
.flag_type = .Bool,
.default_value = .{ .Bool = false },
};
pub fn register(allocator: std.mem.Allocator) !*zli.Command {
const cmd = try zli.Command.init(allocator, .{
.name = "run",
.description = "Run your workflow",
}, run);
try cmd.addFlag(now_flag);
return cmd;
}
fn run(ctx: zli.CommandContext) !void {
const now = ctx.command.getBoolValue("now");
std.debug.print("Running now: {}\n", .{now});
// do something with ctx: ctx.root, ctx.direct_parent, ctx.command ...
// do whatever you want here
}
// src/cli/version.zig
const std = @import("std");
const zli = @import("zli");
pub fn register(allocator: std.mem.Allocator) !*zli.Command {
return zli.Command.init(allocator, .{
.name = "version",
.shortcut = "v",
.description = "Show CLI version",
}, show);
}
fn show(ctx: zli.CommandContext) !void {
std.debug.print("{}\n", .{ctx.root.options.version});
}
- Commands & subcommands
- Flags & shorthands
- Type-safe flag values
- Help/version auto handling
- Deprecation notices
- Positional args
- Command aliases
- Persistent flags
MIT. See LICENSE. Contributions welcome.