Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/ceyhunkerti/argz
command line argument parser for zig
https://github.com/ceyhunkerti/argz
argument-parser cli zig
Last synced: 20 days ago
JSON representation
command line argument parser for zig
- Host: GitHub
- URL: https://github.com/ceyhunkerti/argz
- Owner: ceyhunkerti
- Created: 2023-08-22T17:57:21.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2024-12-27T20:40:15.000Z (27 days ago)
- Last Synced: 2024-12-27T21:25:35.064Z (27 days ago)
- Topics: argument-parser, cli, zig
- Language: Zig
- Homepage:
- Size: 9.6 MB
- Stars: 3
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# argz: CLI application helper for Zig
Supports:
- subcommands `app sub1 sub2`
- input options for primitive types `string` `integer` `boolean`
- Chained options for boolean types `-abc` is same as `-a -b -c`
- flags for on/off behavior types
- argument specification with rules eg. `1..3`
- builtin and customizable help system
- hooks for attaching user functions to specified locations.## Installation
Use zig fetch --save to pull a version of the library into your `build.zig.zon`. (This requires at least Zig 0.11.)
```sh
zig fetch --save "https://github.com/ceyhunkerti/argz/archive/refs/tags/0.0.1.tar.gz"
```Then in your `build.zig` file
```zig...
const argz = b.dependency("argz", .{
.target = target,
.optimize = optimize,
});
exe.root_module.addImport("argz", argz.module("argz"));
...```
## Options
You can specify number of options and attach those options to the command you want. Options are only valid
in the scope of the attached command.See [examples/options.zig](./examples/options.zig) for example usage.
## Arguments
Arguments are just strings and can be limited by using the `nargs` parameter.
```zig
// * : zero or more arguments
// n : exacly n arguments. n is an unsigned interger
// n.. : n or more arguments.
// ..n : up to n arguments, inclusive
// n..m : between n and m arguments, inclusive. m is and unsigned integer
// null : (default) same as zero arguments
```
See [examples/arguments.zig](./examples/options.zig) for example usage.## Subcommands
You can add as many subcommands as you like. It's a tree like structure so you can nest any number of subcommands.
```zig
//demo.zigconst std = @import("std");
const app = @import("argz");
const Command = app.Command;
pub fn mySubCommand(c: *Command) anyerror!void {
std.debug.print("@{s}: running subcommand\n", .{c.name});
}pub fn main() !void {
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
defer {
const check = gpa.deinit();
if (check == .leak) @panic("memory leaked");
}
var allocator = gpa.allocator();var root = Command.init(allocator, "root");
defer root.deinit();var sub = Command.init(allocator, "my-sub-command");
sub.run = mySubCommand;try root.addCommand(sub);
try root.parse();
try root.start();
}
``````sh
$ my-sub-command
# will print @my-sub-command running subcommand
```## Help
To access help, add `-h` or `--help` option. This will print the attached help for the current command.
See [examples/help.zig](./examples/help.zig)