Zopt - low-ceremony schemaless CLI argument parser

zopt - low-ceremony command line argument parser


I’ve been working on some small Zig CLI tools, partly to scratch my own itch, partly to learn the language and do some “hobby coding”. In doing so, I wanted to have a convenient way of parsing CLI options/flags/positionals that was conceptually pretty close to the simplicity of just iterating over argv with a bunch of if statements, but hidden behind a nicer abstraction.

I built zopt for this reason. It’s a schema on read style arg parser that you use to query argv for option values, flags, or positionals. It’s a slightly different kettle of fish to things like clap or argparse. It’s probably closer to something like lexopts.

Perhaps the gnarliest part is having to infer which arguments are positionals without a schema - the way I did this was by introducing some unfortunate temporal coupling: you have to first query all of the available flags/options and then call positionals, which returns you anything not matched by your earlier queries under the assumption that they are positional arguments.

You may or may not find this useful if you are writing simple-ish CLI tools too. If you’d want to use it you should feel free, I’ll try not to break the API too much. Any feedback, contributions, or critique much appreciated as well :slight_smile:

Supported Zig versions

0.16.0

AI / LLM usage disclosure

none

14 Likes