Skip to content

Reokodoku/sap

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

15 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

sap

sap is a simple argument parser library for zig that uses a tuple of flags to create a struct containing the value of the arguments.

How to add the library

  1. Run in the terminal:
zig fetch --save git+https://github.com/Reokodoku/sap
  1. Add in your build.zig:
const sap = b.dependency("sap", .{});
exe.root_module.addImport("sap", sap.module("sap"));

Examples

Minimal example:

const sap = @import("sap");

var arg_parser = sap.Parser(.{
    sap.flag([]const u8, "hello", 'h', "world"),
}).init(allocator);
defer arg_parser.deinit();

const args = try arg_parser.parseArgs();

std.debug.print("Executable name: {s}\n", .{args.executable_name});

var positionals_iter = args.positionals.iterator();
std.debug.print("Positionals:\n", .{});
while (positionals_iter.next()) |str|
    std.debug.print("  {s}\n", .{str});

std.debug.print("`hello`|`h` flag value: {s}\n", .{args.hello});

You can find more examples in the examples/ folder.

For more information, see the source code or documentation (zig build docs).

Features

  • short arguments
  • long arguments
  • pass values after an equal (--foo=bar) or a space (--foo bar)
  • flags can be specified multiple times
  • flags that call a function
  • supported types:
    • booleans
    • strings
    • ints (signed and unsigned)
    • floats
    • enums
    • and all optional variants of the above (?bool, ?[]const u8, ...)

Zig version

sap targets the master branch of zig. In the build.zig.zon file, there is the minimum_zig_version field which specifies the latest version of zig in which sap compiles. When the zig master branch breaks the compilation, a commit will be merged to:

  • fix the compilation errors
  • update the minimum_zig_version field with the new zig version

About

A simple argument parser library for zig

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages