https://github.com/xcaeser/zli
๐ Zig command-line interfaces made easy. A blazing fast CLI framework. Build ergonomic, high-performance command-line tools with zig.
https://github.com/xcaeser/zli
cli zig zig-library
Last synced: 16 days ago
JSON representation
๐ Zig command-line interfaces made easy. A blazing fast CLI framework. Build ergonomic, high-performance command-line tools with zig.
- Host: GitHub
- URL: https://github.com/xcaeser/zli
- Owner: xcaeser
- License: mit
- Created: 2025-05-14T20:39:08.000Z (6 months ago)
- Default Branch: main
- Last Pushed: 2025-06-19T17:25:46.000Z (5 months ago)
- Last Synced: 2025-06-19T18:39:56.433Z (5 months ago)
- Topics: cli, zig, zig-library
- Language: Zig
- Homepage:
- Size: 146 KB
- Stars: 236
- Watchers: 1
- Forks: 7
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
- awesome-zig - xcaeser/zli - line interfaces made easy. A blazing fast CLI framework. Build ergonomic, high-performance command-line tools with zig. (Command Line and Argument Parser / Linker)
README
### ๐ zli - a blazing-fast CLI framework for Zig.
Build modular, ergonomic, and high-performance CLIs with ease.
Batteries included. [ZLI reference docs](https://xcaeser.github.io/zli)
[](https://github.com/xcaeser/zli/actions/workflows/main.yml)
[](README.md)
[](LICENSE)
[](https://github.com/xcaeser)
[](https://github.com/xcaeser/zli/releases)
## ๐ Features
- Modular commands & subcommands
- Fast flag parsing (`--flag`, `--flag=value`, shorthand `-abc`)
- Type-safe support for `bool`, `int`, `string`
- Named positional arguments with `required`, `optional`, `variadic`
- Auto help/version/deprecation handling
- Pretty help output with aligned flags & args
- Spinners for a more interactive experience
- Usage hints, context-aware
## ๐ฆ Installation
```sh
zig fetch --save=zli https://github.com/xcaeser/zli/archive/v4.2.0.tar.gz
```
Add to your `build.zig`:
```zig
const zli_dep = b.dependency("zli", .{ .target = target, .optimize = optimize });
exe.root_module.addImport("zli", zli_dep.module("zli"));
```
## ๐ Recommended Structure (but you can do what you want)
```
your-app/
โโโ build.zig
โโโ src/
โ โโโ main.zig
โ โโโ cli/
โ โโโ root.zig // zli entrypoint
โ โโโ run.zig // subcommand 1
โ โโโ version.zig // subcommand 1
... // subcommand of subcommands, go nuts
```
- Each command is in its own file
- You explicitly register subcommands
- `root.zig` is the entry point
## ๐งช Example
### Your program
```zig
// src/main.zig
const std = @import("std");
const fs = std.fs;
const cli = @import("cli/root.zig");
pub fn main() !void {
var dbg = std.heap.DebugAllocator(.{}).init;
const allocator = switch (builtin.mode) {
.Debug => dbg.allocator(),
.ReleaseFast, .ReleaseSafe, .ReleaseSmall => std.heap.smp_allocator,
};
defer if (builtin.mode == .Debug) std.debug.assert(dbg.deinit() == .ok);
var stdout_writer = fs.File.stdout().writerStreaming(&.{});
var stdout = &stdout_writer.interface;
var buf: [4096]u8 = undefined;
var stdin_reader = fs.File.stdin().readerStreaming(&buf);
const stdin = &stdin_reader.interface;
const root = try cli.build(stdout, stdin, allocator);
defer root.deinit();
try root.execute(.{}); // Or pass data with: try root.execute(.{ .data = &my_data });
try writer.flush(); // Don't forget to flush!
}
```
### Root command - entrypoint
```zig
// src/cli/root.zig
const std = @import("std");
const Writer = std.Io.Writer;
const Reader = std.Io.Reader;
const zli = @import("zli");
const run = @import("run.zig");
const version = @import("version.zig");
pub fn build(writer: *Writer, reader: *Reader, allocator: std.mem.Allocator) !*zli.Command {
const root = try zli.Command.init(writer, reader, allocator, .{
.name = "blitz",
.description = "Your dev toolkit CLI",
}, showHelp);
try root.addCommands(&.{
try run.register(writer, reader, allocator),
try version.register(writer, reader, allocator),
});
return root;
}
fn showHelp(ctx: zli.CommandContext) !void {
try ctx.command.printHelp();
}
```
### Run subcommand
```zig
// src/cli/run.zig
const std = @import("std");
const Writer = std.Io.Writer;
const Reader = std.Io.Reader;
const zli = @import("zli");
const now_flag = zli.Flag{
.name = "now",
.shortcut = "n",
.description = "Run immediately",
.type = .Bool,
.default_value = .{ .Bool = false },
};
pub fn register(writer: *Writer, reader: *Reader, allocator: std.mem.Allocator) !*zli.Command {
const cmd = try zli.Command.init(writer, reader, allocator, .{
.name = "run",
.description = "Run your workflow",
}, run);
try cmd.addFlag(now_flag);
try cmd.addPositionalArg(.{
.name = "script",
.description = "Script to execute",
.required = true,
});
try cmd.addPositionalArg(.{
.name = "env",
.description = "Environment name",
.required = false,
});
return cmd;
}
fn run(ctx: zli.CommandContext) !void {
const now = ctx.flag("now", bool); // type-safe flag access
const script = ctx.getArg("script") orelse {
try ctx.writer.print("Missing script arg\n", .{});
return;
};
const env = ctx.getArg("env") orelse "default";
std.debug.print("Running {s} in {s} (now = {})\n", .{ script, env, now });
// You can also get other commands by name:
// if (ctx.root.findCommand("create")) |create_cmd| {
// try create_cmd.printUsageLine();
// }
// if you passed data to your root command, you can access it here:
// const object = ctx.getContextData(type_of_your_data); // can be struct, []const u8, etc., object is a pointer.
};
```
### Version subcommand
```zig
// src/cli/version.zig
const std = @import("std");
const Writer = std.Io.Writer;
const Reader = std.Io.Reader;
const zli = @import("zli");
pub fn register(writer: *Writer, reader: *Reader, allocator: std.mem.Allocator) !*zli.Command {
return zli.Command.init(writer, writer, allocator, .{
.name = "version",
.shortcut = "v",
.description = "Show CLI version",
}, show);
}
fn show(ctx: zli.CommandContext) !void {
std.debug.print("{?}\n", .{ctx.root.options.version});
}
```
### Spinners example
Available funtions:
- `spinner.start`: to add a new line. sets the spinner to running
- `spinner.updateStyle`: to update the spinner style
- `spinner.updateMessage`: to update text of a running spinner
- `spinner.succeed`, `fail`, `info`, `preserve`: mandatory to complete a line you started. each `spinner.start` needs a `spinner.succeed`, `fail` etc.. spinner after this action is done for that specific line
- Recommendation: use `spinner.print` instead of your own `writer.print` to not have non-displayed messages as spinner works on its own thread
```zig
const std = @import("std");
const zli = @import("zli");
pub fn run(ctx: zli.CommandContext) !void {
var spinner = ctx.spinner;
spinner.updateStyle(.{ .frames = Spinner.SpinnerStyles.earth, .refresh_rate_ms = 150 }); // many styles available
// Step 1
try spinner.start("Step 1", .{}); // New line
std.Thread.sleep(2000 * std.time.ns_per_ms);
try spinner.succeed("Step 1 success", .{}); // each start must be closed with succeed, fail, info, preserve
spinner.updateStyle(.{ .frames = Spinner.SpinnerStyles.weather, .refresh_rate_ms = 150 }); // many styles available
// Step 2
try spinner.start("Step 2", .{}); // New line
std.Thread.sleep(3000 * std.time.ns_per_ms);
spinner.updateStyle(.{ .frames = Spinner.SpinnerStyles.dots, .refresh_rate_ms = 150 }); // many styles available
try spinner.updateMessage("Step 2: Calculating things...", .{}); // update the text of step 2
const i = work(); // do some work
try spinner.info("Step 2 info: {d}", .{i});
// Step 3
try spinner.start("Step 3", .{});
std.Thread.sleep(2000 * std.time.ns_per_ms);
try spinner.fail("Step 3 fail", .{});
try spinner.print("Finish\n", .{}); // instead of using ctx.writer or another writer to avoid concurrency issues
}
fn work() u128 {
var i: u128 = 1;
for (0..100000000) |t| {
i = (t + i);
}
return i;
}
```
## โ
Features Checklist
- [x] Commands & subcommands
- [x] Command aliases
- [x] Flags & shorthands
- [x] Type-safe flag values
- [x] Positional args (required, optional, variadic)
- [x] Named access: `ctx.getArg("name")`
- [x] Context data
- [x] Help/version auto handling
- [x] Deprecation notices
- [x] Pretty-aligned help for flags & args
- [x] Clean usage output like Cobra
- [x] Spinners and loading state (very powerful)
- [ ] Persistent flags
## ๐ Documentation
See [docs.md](docs.md) for full usage, examples, and internals.
## ๐ License
MIT. See [LICENSE](LICENSE). Contributions welcome.