Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/dmgk/zig-getopt
POSIX-compatible getopt(3) implementation in Zig
https://github.com/dmgk/zig-getopt
option-parser option-parsing zig zig-package ziglang
Last synced: 3 months ago
JSON representation
POSIX-compatible getopt(3) implementation in Zig
- Host: GitHub
- URL: https://github.com/dmgk/zig-getopt
- Owner: dmgk
- License: 0bsd
- Created: 2021-05-20T14:17:44.000Z (over 3 years ago)
- Default Branch: master
- Last Pushed: 2023-08-14T15:03:19.000Z (over 1 year ago)
- Last Synced: 2023-08-14T16:10:36.927Z (over 1 year ago)
- Topics: option-parser, option-parsing, zig, zig-package, ziglang
- Language: Zig
- Homepage:
- Size: 9.77 KB
- Stars: 8
- Watchers: 1
- Forks: 2
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Minimal POSIX getopt(3) implementation in Zig
This is a minimal, allocation-free getopt(3) implementation with [POSIX-conforming](http://pubs.opengroup.org/onlinepubs/9699919799/functions/getopt.html) argument parsing semantics.
## Example
```zig
const std = @import("std");
const debug = std.debug;
const getopt = @import("getopt.zig");pub fn main() void {
var arg: []const u8 = undefined;
var verbose: bool = false;var opts = getopt.getopt("a:vh");
while (opts.next()) |maybe_opt| {
if (maybe_opt) |opt| {
switch (opt.opt) {
'a' => {
arg = opt.arg.?;
debug.print("arg = {s}\n", .{arg});
},
'v' => {
verbose = true;
debug.print("verbose = {}\n", .{verbose});
},
'h' => debug.print(
\\usage: example [-a arg] [-hv]
\\
, .{}),
else => unreachable,
}
} else break;
} else |err| {
switch (err) {
getopt.Error.InvalidOption => debug.print("invalid option: {c}\n", .{opts.optopt}),
getopt.Error.MissingArgument => debug.print("option requires an argument: {c}\n", .{opts.optopt}),
}
}debug.print("remaining args: {?s}\n", .{opts.args()});
}
``````
$ zig run example.zig -- -hv -a42 foo bar
usage: example [-a arg] [-hv]
verbose = true
arg = 42
remaining args: { foo, bar }
```