API reference
cli: command-line argument parsing for Zig.
The parser is a cursor over argv, not a schema. It classifies tokens into options and positionals and leaves every semantic decision, including the help text and the error messages, to the command. The library writes nothing; the command owns its writers.
parse converts one value on request: cli.parse(u16, raw). It covers the
primitive types and calls a type's own parse function for anything else.
It never chooses a flag and never writes text.
module
Args
src
A cursor over command-line arguments: classifies tokens into options and positionals, but never decides what an option means.
more
The grammar is deliberately small:
--name long option, no inline value --name=value long option with an inline value -n short option -abc three short options, in order -ovalue short option with an attached value (the rest) -n value short option whose value is the next token -- ends option parsing; every later token is positional
positional (the conventional stdin marker)
-n=value is not special. The value of n is =value, which is the
usual getopt behavior.
The -- token itself is not returned. The cursor swallows it and flips
to positional mode.
Options are recognized after positionals. A command that wants POSIX
stop-at-first-positional switches to nextRaw after the first one.
Short options are yielded one at a time. Option.attached is the rest of
the same token. If the app calls takeValue, that rest becomes the value
and the cluster ends; if it does not, the cursor yields the next short
option. The cursor never needs to know which options take values.
Args does not allocate an argv slice, print, or exit. It either owns a
process.Args.Iterator (built with init or initAllocator) or
borrows a slice of tokens (built with fromSlice, for tests and
embedding).
Fields
source: Source
program_name: []const u8
The program name, the first argv token. init and initAllocator
capture it before advancing the iterator. A slice cursor leaves it
empty; set the field directly when the name matters. It is invalid
after deinit.
current: []const u8
The raw token that produced the current short-option cluster.
pending: []const u8
The unconsumed short-option bytes in current.
options_done: bool
peeked: ?[]const u8
Constants
Functions
Build a cursor over the process arguments. The cursor captures the
first token as program_name, then owns the iterator; call deinit.
This is the allocation-free constructor. Like
process.Args.Iterator.init, it does not compile on Windows or
WASI; use initAllocator there. On POSIX it allocates nothing.
The returned slices point into the iterator's buffer and remain valid
until deinit.
pub fn initAllocator(raw: process.Args, gpa: Allocator) InitError!Args
src
Build a cursor over the process arguments, with an allocator for the
platforms whose iterator needs a buffer. The cursor captures the first
token as program_name, then owns the iterator; call deinit.
Args reads one argument at a time; it does not build an argv slice.
The allocator is used only where the platform iterator needs a buffer
(Windows and WASI); POSIX allocates nothing.
The returned slices point into the iterator's buffer and remain valid
until deinit.
Build a cursor over a literal token slice, for tests and embedding.
program_name starts empty.
Release the process iterator, if this cursor owns one. A slice cursor has nothing to release.
Return the next classified token, or null at the end.
The value for opt: the inline --name=value, the rest of a short
cluster, or the next raw token. Returns null if there is no next token.
more
The next token is consumed even when it starts with a dash, so
--message -oops gives the message -oops. The app decides whether
that is valid.
Call this at most once per option. A boolean option must not call it;
the cursor then keeps yielding the rest of the cluster. For an
optional value, use peekRaw and isOption first.
Return the next raw argv token without classifying it. This is the
escape hatch for token streams that deliberately bypass option
parsing. Do not call it while a short cluster is pending; use
takeValue or keep calling next.
Look at the next raw argv token without consuming it. Do not call while a short cluster is pending.
True when the token starts option syntax: a dash followed by at least one
byte. - is positional; -- is the separator and counts as option syntax
so optional-value logic does not swallow it.
Types
union
Token
src
pub const Token = union(enum)
Fields
positional: []const u8
A token that is not option syntax. The slice points into the source tokens and lives as long as they do.
option: Option
struct
Option
src
Fields
text: []const u8
The exact argv token that carried this option, for diagnostics.
kind: Kind
name: []const u8
The option name without dashes. For a short option this is one byte.
inline_value: ?[]const u8
The part after = in --name=value, if any. Long options only.
attached: []const u8
The rest of a short cluster after this option. -abc yields a with
attached = "bc", then b with attached = "c".
Functions
Match a switch: a long option with no inline value, or a short option. A short option in a cluster still matches; the attached tail is the rest of the cluster, not a value.
Format the option for a diagnostic: --name, --name=value, or
-n. A short cluster prints only the current letter, and a short
attached value stays out of the spelling. Use {f} with a writer.
Types
struct
Option
src
pub const Option = Args.Option
Fields
text: []const u8
The exact argv token that carried this option, for diagnostics.
kind: Kind
name: []const u8
The option name without dashes. For a short option this is one byte.
inline_value: ?[]const u8
The part after = in --name=value, if any. Long options only.
attached: []const u8
The rest of a short cluster after this option. -abc yields a with
attached = "bc", then b with attached = "c".
Functions
Match a switch: a long option with no inline value, or a short option. A short option in a cluster still matches; the attached tail is the rest of the cluster, not a value.
Format the option for a diagnostic: --name, --name=value, or
-n. A short cluster prints only the current letter, and a short
attached value stays out of the spelling. Use {f} with a writer.
Types
union
Token
src
pub const Token = Args.Token
Fields
positional: []const u8
A token that is not option syntax. The slice points into the source tokens and lives as long as they do.
option: Option
True when the token starts option syntax: a dash followed by at least one
byte. - is positional; -- is the separator and counts as option syntax
so optional-value logic does not swallow it.
Parse raw as T.
more
bool: the spellings Go'sstrconv.ParseBoolaccepts:1,t,T,TRUE,true,True, and the false equivalents.i8..i128,u8..u128,isize,usize: base 10, with an optional leading sign. A number that does not fit returnserror.Overflow.f16..f128: the syntaxstd.fmt.parseFloataccepts.[]const u8: the text unchanged, borrowed fromraw.- an enum: the tag name, matched like
std.meta.stringToEnum. - a struct, union, or opaque type with
pub fn parse(raw: []const u8) !Self: that function runs, and its errors pass through.
Any other type is a compile error.