Simple-to-use argument parser with struct-based config
Find a file
2021-05-03 10:39:00 +02:00
.github/workflows Update to latest master. 2021-01-04 11:37:36 +01:00
.gitattributes adds gitattributes. 2020-08-18 11:59:02 +02:00
.gitignore Update to latest master. 2021-01-04 11:37:36 +01:00
args.zig Useless line 2021-01-23 12:15:25 +01:00
demo.zig Update to latest master. 2021-02-15 11:01:49 +01:00
gyro.zzz added string formatter and gyro packaging 2021-01-29 10:32:59 +01:00
LICENSE Initial commit 2020-03-04 22:34:01 +01:00
README.md use positionals instead of args in README.md 2020-05-25 19:41:36 +02:00
zig.mod Rename zig-args to args 2021-05-03 10:39:00 +02:00

Zig Argument Parser

Simple-to-use argument parser with struct-based config

Features

  • Automatic option generation from a config struct
  • Familiar look & feel:
    • Everything after the first -- is assumed to be a positional argument
    • A single - is interpreted as a positional argument which can be used as the stdin/stdout file placeholder
    • Short options with no argument can be combined into a single argument: -dfe
    • Long options can use either --option=value or --option value syntax
  • Integrated support for primitive types:
    • All integer types (signed & unsigned)
    • Floating point types
    • Booleans (takes optional argument. If no argument given, the bool is set, otherwise, one of yes, true, y, no, false, n is interpreted)
    • Strings
    • Enumerations

Example

const options = try argsParser.parse(struct {
    // This declares long options for double hyphen
    output: ?[]const u8 = null,
    @"with-offset": bool = false,
    @"with-hexdump": bool = false,
    @"intermix-source": bool = false,
    numberOfBytes: ?i32 = null,

    // This declares short-hand options for single hyphen
    pub const shorthands = .{
        .S = "intermix-source",
        .b = "with-hexdump",
        .O = "with-offset",
        .o = "output",
    };
}, &args, argsAllocator);
defer options.deinit();

std.debug.warn("parsed result:\n{}\npositionals:\n", .{options.options});
for (options.positionals) |arg| {
    std.debug.warn("\t{}\n", .{arg});
}