Simple-to-use argument parser with struct-based config
Find a file
Felix (xq) Queißner c24a919906 adds gitattributes.
2020-08-18 11:59:02 +02:00
.gitattributes adds gitattributes. 2020-08-18 11:59:02 +02:00
args.zig Update to Zig master 2020-07-13 16:07:48 +02:00
demo.zig Use Zig-master features 2020-06-26 11:35:07 +02: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 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});
}