https://github.com/operachi061/mini-parser
A very-minimal command-line parser
https://github.com/operachi061/mini-parser
argument-parsing command-line command-line-parser minimal zig zig-package ziglang
Last synced: 3 months ago
JSON representation
A very-minimal command-line parser
- Host: GitHub
- URL: https://github.com/operachi061/mini-parser
- Owner: Operachi061
- License: unlicense
- Created: 2025-04-07T12:06:37.000Z (9 months ago)
- Default Branch: main
- Last Pushed: 2025-07-28T20:59:28.000Z (6 months ago)
- Last Synced: 2025-07-28T22:33:38.602Z (6 months ago)
- Topics: argument-parsing, command-line, command-line-parser, minimal, zig, zig-package, ziglang
- Language: Zig
- Homepage:
- Size: 10.7 KB
- Stars: 20
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# mini-parser
mini-parser is a very-minimal parser for the [Zig](https://ziglang.org) language.
## Example
```zig
const std = @import("std");
const mini_parser = @import("mini-parser");
const debug = std.debug;
const posix = std.posix;
const usage =
\\Usage: example [OPTIONS]
\\
\\Options:
\\ --help, -h Display help list
\\ --text=TEXT, -t Print the text
\\ --bool, -b Enable the boolean
\\
;
pub fn main() !void {
var i: usize = 0;
while (std.os.argv.len > i) : (i += 1) {
var args: usize = 1;
blk: while (true) {
const parser = try mini_parser.init(i, args, &.{
.{ .long = "help", .short = 'h', .type = .boolean },
.{ .long = "text", .short = 't', .type = .argument },
.{ .long = "bool", .short = 'b', .type = .boolean },
});
switch (parser.arg) {
0 => {
debug.print("no argument was given.\n", .{});
posix.exit(0);
},
1 => {
debug.print("argument '{s}' does not exist.\n", .{parser.val});
posix.exit(0);
},
'h' => {
debug.print("{s}\n", .{usage});
posix.exit(0);
},
't' => {
debug.print("Text: {s}\n", .{parser.val});
},
'b' => {
debug.print("Enabled boolean!\n", .{});
},
else => {},
}
if (args != parser.args) {
args = parser.args;
continue :blk;
}
break;
}
}
}
```
## Installation
Fetch mini-parser package to `build.zig.zon`:
```sh
zig fetch --save git+https://github.com/Operachi061/mini-parser
```
Then add following to `build.zig`:
```zig
const mini_parser = b.dependency("mini_parser", .{});
exe.root_module.addImport("mini-parser", mini_parser.module("mini-parser"));
```
After building, test it via:
```sh
./example -tb foo --help
```
## Unlicense
This project is based on the [UNLICENSE](https://github.com/Operachi061/mini-parser/blob/main/UNLICENSE).