Simargs

Simargs

A simple, opinionated, struct-based argument parser in Zig, taking full advantage of comptime.

Features

  • Supported data type:

  • Optional fields and fields with default value mean they are optional arguments
  • Use comptime as possible as I can
  • Provide print_help() out of the box

Usage

See simargs-demo.zig.

# Run demo
zig build && ./zig-out/bin/simargs-demo -o /tmp/a.out --user-agent Firefox hello world 2>&1
------------------------------Program------------------------------
./zig-out/bin/simargs-demo

------------------------------Arguments------------------------------
   verbose: null
user-agent: demo.main__struct_1677.main__struct_1677__enum_1777.Firefox
   timeout: 30
    output: /tmp/a.out
      help: false

------------------------------Positionals------------------------------
1: hello
2: world

------------------------------print_help------------------------------
 USAGE:
     ./zig-out/bin/demo [OPTIONS] [--] [file]

 OPTIONS:
	-v, --verbose                     Make the operation more talkative
	-A, --user-agent STRING            (valid: Chrome|Firefox|Safari)(default: Firefox)
	    --timeout INTEGER             Max time this request can cost(default: 30)
	-o, --output STRING               Write to file instead of stdout(required)
	-h, --help

Acknowledgment

Blog post explaining how simargs is implemented: What I learn by implementing argparser in Zig.

When implement simargs, I refer following projects to learn how to write idiomatic Zig code. Big Thanks!