Simple-to-use argument parser with struct-based config
Bob Farrell 01d72b9a01 Add support for wrapping help output
If `wrap_len` is provided, wrap lines over `wrap_len` characters,
retaining indentation.

Fix test runner - tests were not running with `zig build test`.
2024-03-20 10:53:37 +01:00
.github/workflows First step towards integrated verb parsing. 2021-08-27 17:03:13 +02: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
LICENSE Initial commit 2020-03-04 22:34:01 +01:00
README.md Fix optional usage in README example 2024-03-03 17:43:09 +01:00
args.zig Add support for wrapping help output 2024-03-20 10:53:37 +01:00
build.zig Add support for wrapping help output 2024-03-20 10:53:37 +01:00
demo.zig Run zig fmt 2024-01-05 18:05:13 +01:00
demo_verb.zig Changed to fix local variable is never mutated 2023-11-21 10:29:43 +01:00
gyro.zzz added string formatter and gyro packaging 2021-01-29 10:32:59 +01:00
pkg.zpm Exports Options from ParseResult, adds ZPM file. 2021-07-14 09:00:53 +02:00
zig.mod Rename zig-args to args 2021-05-03 10:39:00 +02:00

README.md

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 (use --option=-- if you need -- as a long option argument)
    • verbs (sub-commands), with verb specific options. Non-verb specific (global) options can come before or after the verb on the command line. Non-verb option arguments are processed before determining verb. (see demo_verb.zig)
  • 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 = argsParser.parseForCurrentProcess(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,
    signed_number: ?i64 = null,
    unsigned_number: ?u64 = null,
    mode: enum { default, special, slow, fast } = .default,

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

std.debug.print("executable name: {?s}\n", .{options.executable_name});

std.debug.print("parsed options:\n", .{});
inline for (std.meta.fields(@TypeOf(options.options))) |fld| {
    std.debug.print("\t{s} = {any}\n", .{
        fld.name,
        @field(options.options, fld.name),
    });
}

std.debug.print("parsed positionals:\n", .{});
for (options.positionals) |arg| {
    std.debug.print("\t'{s}'\n", .{arg});
}