Skip to content

std.cli

flags, options, positionals and help text from one command description

pub CliError :: errors {
unknown_option,
missing_value,
unexpected_value,
missing_argument,
too_many_arguments,
invalid_number,
}
  • unknown_option: an option the command does not have
  • missing_value: an option that takes a value, at the end
  • unexpected_value: a value given to a flag (--verbose=yes)
  • missing_argument: too few positional arguments
  • too_many_arguments: more than it takes
  • invalid_number: int_option of a value that is no integer
pub Command :: struct {
region: *Arena,
name: []const u8,
about: []const u8,
specs: Vec(Spec),
places: Vec(PosSpec),
message: []const u8,
}
pub flag :: fn(self: *Self, long: []const u8, short: u8, about: []const u8)

short 0 = none

pub option :: fn(self: *Self, long: []const u8, short: u8, value_name: []const u8, about: []const u8)

takes a value; short 0 = none; value_name shows in help (FILE, N)

pub positional :: fn(self: *Self, name: []const u8, about: []const u8)

required; positionals match in declaration order

pub optional :: fn(self: *Self, name: []const u8, about: []const u8)

may be omitted; goes after the required ones

pub rest :: fn(self: *Self, name: []const u8, about: []const u8)

any number after the others (files…)

pub parse :: fn(self: *Self, args: []const []const u8) -> Parsed ! CliError

skips args[0]; –x V, –x=V, -o V, -oV, -vq; -- ends options; -h skips checks

pub help :: fn(self: *Self, a: *Arena) -> []const u8

usage and options text, in a

pub Parsed :: struct {
given: Vec(Given),
positionals: Vec([]const u8),
help: bool,
}
pub flag :: fn(self: *Self, long: []const u8) -> bool

flag or option given at all

pub count :: fn(self: *Self, long: []const u8) -> usize

times given (-vvv is 3)

pub option :: fn(self: *Self, long: []const u8) -> ?[]const u8

last value if repeated; null if not given

pub all :: fn(self: *Self, a: *Arena, long: []const u8) -> []const []const u8

all values in order (-I a -I b), in a

pub int_option :: fn(self: *Self, long: []const u8) -> ?i64 ! CliError

null if not given; .invalid_number if not an integer

pub positional :: fn(self: *Self, i: usize) -> ?[]const u8
pub positionals_all :: fn(self: *Self) -> []const []const u8
pub command :: fn(a: *Arena, name: []const u8, about: []const u8) -> Command

name as help shows it; everything lives in a