Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/Gonzih/zigra
Command line toolkit for Zig
https://github.com/Gonzih/zigra
Last synced: 3 months ago
JSON representation
Command line toolkit for Zig
- Host: GitHub
- URL: https://github.com/Gonzih/zigra
- Owner: Gonzih
- Created: 2023-01-09T05:36:40.000Z (about 2 years ago)
- Default Branch: main
- Last Pushed: 2023-01-13T17:51:26.000Z (about 2 years ago)
- Last Synced: 2024-08-03T04:08:17.208Z (7 months ago)
- Language: Zig
- Homepage:
- Size: 647 KB
- Stars: 24
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
- awesome-zig - zigra🗒️Command line toolkit for Zig
README
# Zigra
![Zigra](zigra.jpg)
Command line toolkit for Zig.
Inspired by [cobra](https://github.com/spf13/cobra).* Separate each subcommand into its own package/file.
* Command specific allocator accessible via Context (`ctx.allocator`).
* Modular and composable API.# Example
```zig
const std = @import("std");
const zigra = @import("zigra");const Enum = enum {
Extract,
Transform,
Load,
};const Handler = struct {
pub const Self = @This();
v: usize = 0,pub fn init(_: *Self, _: *zigra.Context) !void {}
pub fn run(_: *Self, ctx: *zigra.Context) !void {
std.debug.print("runner {s}\n", .{ctx.cmd});
}pub fn deinit(_: *Self) void {}
};const SubHandler = struct {
pub const Self = @This();verbose: bool = false,
threads: usize = 0,
kind: Enum = .Extract,
id: []const u8 = "",pub fn init(self: *Self, ctx: *zigra.Context) !void {
try ctx.bind(bool, &self.verbose, "verbose", "v", "Verbose output");
try ctx.bind(usize, &self.threads, "threads", "t", "Number of threads to run");
try ctx.bind(Enum, &self.kind, "worker-kind", "w", "Which worker to run");
try ctx.bind([]const u8, &self.id, "uuid", "u", "Unique identifier for the worker");
}pub fn run(self: *Self, ctx: *zigra.Context) !void {
std.debug.print("cmd: {s}\n verbose: {}\n threads: {}\n kind: {any}\n id: {s}\n", .{
ctx.cmd,
self.verbose,
self.threads,
self.kind,
self.id,
});
}pub fn deinit(_: *Self) void {}
};var h1 = Handler{};
var r1 = zigra.Runner.make(&h1);var h2 = SubHandler{};
var r2 = zigra.Runner.make(&h2);pub fn main() !void {
const allocator = std.heap.page_allocator;var root = try zigra.Command.init(allocator, &r1, "app", "My CLI App");
var one = try zigra.Command.init(allocator, &r2, "one", "First subcommand");
try root.addSubcommand(&one);defer root.deinit();
try root.exec();
}
``````
./app --helpapp: My CLI App
Commands:
one: First subcommandArguments:
--verbose, -v (true or false): Verbose output
--threads, -t (integer): Number of threads to run
--worker-kind, -w (enum): Which worker to run
--uuid, -u (string): Unique identifier for the worker./app one --verbose=true --threads=20 --worker-kind=Extract --uuid=secret-id
cmd: one
verbose: false
threads: 20
kind: main.Enum.Extract
id: secret-id```