{"id":28286746,"url":"https://github.com/xcaeser/zli","last_synced_at":"2025-10-30T20:08:11.149Z","repository":{"id":293341776,"uuid":"983732269","full_name":"xcaeser/zli","owner":"xcaeser","description":"📟 Zig command-line interfaces made easy. A blazing fast CLI framework. Build ergonomic, high-performance command-line tools with zig.","archived":false,"fork":false,"pushed_at":"2025-06-19T17:25:46.000Z","size":149,"stargazers_count":236,"open_issues_count":3,"forks_count":7,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-06-19T18:39:56.433Z","etag":null,"topics":["cli","zig","zig-library"],"latest_commit_sha":null,"homepage":"","language":"Zig","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/xcaeser.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2025-05-14T20:39:08.000Z","updated_at":"2025-06-19T17:23:55.000Z","dependencies_parsed_at":"2025-05-14T21:41:16.754Z","dependency_job_id":"5ddc6c3b-99eb-4672-98b8-53fa9252de75","html_url":"https://github.com/xcaeser/zli","commit_stats":null,"previous_names":["xcaeser/zli"],"tags_count":27,"template":false,"template_full_name":null,"purl":"pkg:github/xcaeser/zli","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xcaeser%2Fzli","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xcaeser%2Fzli/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xcaeser%2Fzli/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xcaeser%2Fzli/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/xcaeser","download_url":"https://codeload.github.com/xcaeser/zli/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xcaeser%2Fzli/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":261534477,"owners_count":23173409,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cli","zig","zig-library"],"created_at":"2025-05-21T21:12:35.927Z","updated_at":"2025-10-30T20:08:11.143Z","avatar_url":"https://github.com/xcaeser.png","language":"Zig","funding_links":[],"categories":["Zig","Language Essentials"],"sub_categories":["Command Line and Argument Parser"],"readme":"### 📟 zli - a blazing-fast CLI framework for Zig.\n\nBuild modular, ergonomic, and high-performance CLIs with ease.\n\nBatteries included. [ZLI reference docs](https://xcaeser.github.io/zli)\n\n[![Tests](https://github.com/xcaeser/zli/actions/workflows/main.yml/badge.svg)](https://github.com/xcaeser/zli/actions/workflows/main.yml)\n[![Zig Version](https://img.shields.io/badge/Zig_Version-0.15.2-orange.svg?logo=zig)](README.md)\n[![License: MIT](https://img.shields.io/badge/License-MIT-lightgrey.svg?logo=cachet)](LICENSE)\n[![Built by xcaeser](https://img.shields.io/badge/Built%20by-@xcaeser-blue)](https://github.com/xcaeser)\n[![Version](https://img.shields.io/badge/ZLI-v4.2.0-green)](https://github.com/xcaeser/zli/releases)\n\n## 🚀 Features\n\n- Modular commands \u0026 subcommands\n- Fast flag parsing (`--flag`, `--flag=value`, shorthand `-abc`)\n- Type-safe support for `bool`, `int`, `string`\n- Named positional arguments with `required`, `optional`, `variadic`\n- Auto help/version/deprecation handling\n- Pretty help output with aligned flags \u0026 args\n- Spinners for a more interactive experience\n- Usage hints, context-aware\n\n## 📦 Installation\n\n```sh\nzig fetch --save=zli https://github.com/xcaeser/zli/archive/v4.2.0.tar.gz\n```\n\nAdd to your `build.zig`:\n\n```zig\nconst zli_dep = b.dependency(\"zli\", .{ .target = target, .optimize = optimize });\nexe.root_module.addImport(\"zli\", zli_dep.module(\"zli\"));\n```\n\n## 🗂 Recommended Structure (but you can do what you want)\n\n```\nyour-app/\n├── build.zig\n├── src/\n│   ├── main.zig\n│   └── cli/\n│       ├── root.zig // zli entrypoint\n│       ├── run.zig  // subcommand 1\n│       └── version.zig // subcommand 1\n        ... // subcommand of subcommands, go nuts\n```\n\n- Each command is in its own file\n- You explicitly register subcommands\n- `root.zig` is the entry point\n\n## 🧪 Example\n\n### Your program\n\n```zig\n// src/main.zig\nconst std = @import(\"std\");\nconst fs = std.fs;\nconst cli = @import(\"cli/root.zig\");\n\npub fn main() !void {\n    var dbg = std.heap.DebugAllocator(.{}).init;\n\n    const allocator = switch (builtin.mode) {\n        .Debug =\u003e dbg.allocator(),\n        .ReleaseFast, .ReleaseSafe, .ReleaseSmall =\u003e std.heap.smp_allocator,\n    };\n\n    defer if (builtin.mode == .Debug) std.debug.assert(dbg.deinit() == .ok);\n\n    var stdout_writer = fs.File.stdout().writerStreaming(\u0026.{});\n    var stdout = \u0026stdout_writer.interface;\n\n    var buf: [4096]u8 = undefined;\n    var stdin_reader = fs.File.stdin().readerStreaming(\u0026buf);\n    const stdin = \u0026stdin_reader.interface;\n\n    const root = try cli.build(stdout, stdin, allocator);\n    defer root.deinit();\n\n    try root.execute(.{}); // Or pass data with: try root.execute(.{ .data = \u0026my_data });\n\n    try writer.flush(); // Don't forget to flush!\n}\n```\n\n### Root command - entrypoint\n\n```zig\n// src/cli/root.zig\nconst std = @import(\"std\");\nconst Writer = std.Io.Writer;\nconst Reader = std.Io.Reader;\nconst zli = @import(\"zli\");\n\nconst run = @import(\"run.zig\");\nconst version = @import(\"version.zig\");\n\npub fn build(writer: *Writer, reader: *Reader, allocator: std.mem.Allocator) !*zli.Command {\n    const root = try zli.Command.init(writer, reader, allocator, .{\n        .name = \"blitz\",\n        .description = \"Your dev toolkit CLI\",\n    }, showHelp);\n\n    try root.addCommands(\u0026.{\n        try run.register(writer, reader, allocator),\n        try version.register(writer, reader, allocator),\n    });\n\n    return root;\n}\n\nfn showHelp(ctx: zli.CommandContext) !void {\n    try ctx.command.printHelp();\n}\n```\n\n### Run subcommand\n\n```zig\n// src/cli/run.zig\nconst std = @import(\"std\");\nconst Writer = std.Io.Writer;\nconst Reader = std.Io.Reader;\nconst zli = @import(\"zli\");\n\nconst now_flag = zli.Flag{\n    .name = \"now\",\n    .shortcut = \"n\",\n    .description = \"Run immediately\",\n    .type = .Bool,\n    .default_value = .{ .Bool = false },\n};\n\npub fn register(writer: *Writer, reader: *Reader, allocator: std.mem.Allocator) !*zli.Command {\n    const cmd = try zli.Command.init(writer, reader, allocator, .{\n        .name = \"run\",\n        .description = \"Run your workflow\",\n    }, run);\n\n    try cmd.addFlag(now_flag);\n    try cmd.addPositionalArg(.{\n        .name = \"script\",\n        .description = \"Script to execute\",\n        .required = true,\n    });\n    try cmd.addPositionalArg(.{\n        .name = \"env\",\n        .description = \"Environment name\",\n        .required = false,\n    });\n\n    return cmd;\n}\n\nfn run(ctx: zli.CommandContext) !void {\n    const now = ctx.flag(\"now\", bool); // type-safe flag access\n\n    const script = ctx.getArg(\"script\") orelse {\n        try ctx.writer.print(\"Missing script arg\\n\", .{});\n        return;\n    };\n    const env = ctx.getArg(\"env\") orelse \"default\";\n\n    std.debug.print(\"Running {s} in {s} (now = {})\\n\", .{ script, env, now });\n\n    // You can also get other commands by name:\n    // if (ctx.root.findCommand(\"create\")) |create_cmd| {\n    //    try create_cmd.printUsageLine();\n    // }\n\n    // if you passed data to your root command, you can access it here:\n    // const object = ctx.getContextData(type_of_your_data); // can be struct, []const u8, etc., object is a pointer.\n\n};\n```\n\n### Version subcommand\n\n```zig\n// src/cli/version.zig\nconst std = @import(\"std\");\nconst Writer = std.Io.Writer;\nconst Reader = std.Io.Reader;\nconst zli = @import(\"zli\");\n\npub fn register(writer: *Writer, reader: *Reader, allocator: std.mem.Allocator) !*zli.Command {\n    return zli.Command.init(writer, writer, allocator, .{\n        .name = \"version\",\n        .shortcut = \"v\",\n        .description = \"Show CLI version\",\n    }, show);\n}\n\nfn show(ctx: zli.CommandContext) !void {\n    std.debug.print(\"{?}\\n\", .{ctx.root.options.version});\n}\n```\n\n### Spinners example\n\nAvailable funtions:\n\n- `spinner.start`: to add a new line. sets the spinner to running\n- `spinner.updateStyle`: to update the spinner style\n- `spinner.updateMessage`: to update text of a running spinner\n- `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\n- Recommendation: use `spinner.print` instead of your own `writer.print` to not have non-displayed messages as spinner works on its own thread\n\n```zig\nconst std = @import(\"std\");\nconst zli = @import(\"zli\");\n\npub fn run(ctx: zli.CommandContext) !void {\n    var spinner = ctx.spinner;\n    spinner.updateStyle(.{ .frames = Spinner.SpinnerStyles.earth, .refresh_rate_ms = 150 }); // many styles available\n\n    // Step 1\n    try spinner.start(\"Step 1\", .{}); // New line\n    std.Thread.sleep(2000 * std.time.ns_per_ms);\n\n    try spinner.succeed(\"Step 1 success\", .{}); // each start must be closed with succeed, fail, info, preserve\n\n    spinner.updateStyle(.{ .frames = Spinner.SpinnerStyles.weather, .refresh_rate_ms = 150 }); // many styles available\n\n    // Step 2\n    try spinner.start(\"Step 2\", .{}); // New line\n    std.Thread.sleep(3000 * std.time.ns_per_ms);\n\n    spinner.updateStyle(.{ .frames = Spinner.SpinnerStyles.dots, .refresh_rate_ms = 150 }); // many styles available\n    try spinner.updateMessage(\"Step 2: Calculating things...\", .{}); // update the text of step 2\n\n    const i = work(); // do some work\n\n    try spinner.info(\"Step 2 info: {d}\", .{i});\n\n    // Step 3\n    try spinner.start(\"Step 3\", .{});\n    std.Thread.sleep(2000 * std.time.ns_per_ms);\n\n    try spinner.fail(\"Step 3 fail\", .{});\n\n    try spinner.print(\"Finish\\n\", .{}); // instead of using ctx.writer or another writer to avoid concurrency issues\n}\n\nfn work() u128 {\n    var i: u128 = 1;\n    for (0..100000000) |t| {\n        i = (t + i);\n    }\n\n    return i;\n}\n```\n\n## ✅ Features Checklist\n\n- [x] Commands \u0026 subcommands\n- [x] Command aliases\n- [x] Flags \u0026 shorthands\n- [x] Type-safe flag values\n- [x] Positional args (required, optional, variadic)\n- [x] Named access: `ctx.getArg(\"name\")`\n- [x] Context data\n- [x] Help/version auto handling\n- [x] Deprecation notices\n- [x] Pretty-aligned help for flags \u0026 args\n- [x] Clean usage output like Cobra\n- [x] Spinners and loading state (very powerful)\n- [ ] Persistent flags\n\n## 📚 Documentation\n\nSee [docs.md](docs.md) for full usage, examples, and internals.\n\n## 📝 License\n\nMIT. See [LICENSE](LICENSE). Contributions welcome.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fxcaeser%2Fzli","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fxcaeser%2Fzli","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fxcaeser%2Fzli/lists"}