cli API reference Changelog View on GitHub

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

pub const InitError = process.Args.Iterator.InitError

src

Functions

pub fn init(raw: process.Args) Args

src

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.

pub fn fromSlice(tokens: []const []const u8) Args

src

Build a cursor over a literal token slice, for tests and embedding. program_name starts empty.

pub fn deinit(self: *Args) void

src

Release the process iterator, if this cursor owns one. A slice cursor has nothing to release.

pub fn next(self: *Args) ?Token

src

Return the next classified token, or null at the end.

pub fn takeValue(self: *Args, opt: Option) ?[]const u8

src

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.

pub fn nextRaw(self: *Args) ?[]const u8

src

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.

pub fn peekRaw(self: *Args) ?[]const u8

src

Look at the next raw argv token without consuming it. Do not call while a short cluster is pending.

pub fn isOption(token: []const u8) bool

src

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 Source

src
pub const Source = union(enum)

Fields

process: process.Args.Iterator
slice: Slice

struct Slice

src

Fields

tokens: []const []const u8
index: usize

The index of the next token to read.

Functions

pub fn init(tokens: []const []const u8) Slice

src

Build a slice source over tokens, starting at the first one.

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

pub fn is(self: *const Option, long: []const u8, short: ?u8) bool

src

pub fn isLong(self: *const Option, long: []const u8) bool

src

pub fn isShort(self: *const Option, char: u8) bool

src

pub fn isSwitch(self: *const Option, long: []const u8, short: ?u8) bool

src

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.

pub fn format(self: *const Option, writer: *Io.Writer) Io.Writer.Error!void

src

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

enum Kind

src

Fields

long long
short short

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

pub fn is(self: *const Option, long: []const u8, short: ?u8) bool

src

pub fn isLong(self: *const Option, long: []const u8) bool

src

pub fn isShort(self: *const Option, char: u8) bool

src

pub fn isSwitch(self: *const Option, long: []const u8, short: ?u8) bool

src

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.

pub fn format(self: *const Option, writer: *Io.Writer) Io.Writer.Error!void

src

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

enum Kind

src

Fields

long long
short short

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

pub fn isOption(token: []const u8) bool

src

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.

pub fn parse(comptime T: type, raw: []const u8) !T

src

Parse raw as T.

more
  • bool: the spellings Go's strconv.ParseBool accepts: 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 returns error.Overflow.
  • f16..f128: the syntax std.fmt.parseFloat accepts.
  • []const u8: the text unchanged, borrowed from raw.
  • 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.