flags/core

sourceflags/core.sx

InitError

pub enum InitError = { OutOfMemory }

Errors reported while creating a flag set.

Variants

  • OutOfMemory

ParseError

pub union ParseError = { HelpRequested() | UnknownFlag(ref u8) | MissingValue(ref u8) | InvalidValue(ref u8, ref u8) }

Errors reported while parsing arguments.

Variants

  • HelpRequested()
  • UnknownFlag(ref u8)
  • MissingValue(ref u8)
  • InvalidValue(ref u8, ref u8)

FlagValue

union FlagValue = { Bool(mut ref bool) | Str(mut ref ref u8) | Int(mut ref i64) | Float(mut ref f64) }

The value storage for one registered flag.

Variants

  • Bool(mut ref bool)
  • Str(mut ref ref u8)
  • Int(mut ref i64)
  • Float(mut ref f64)

Flag

type Flag = { next: mut ref Flag, name: mut ref u8, alias: mut ref u8, help: mut ref u8, placeholder: mut ref u8, default_text: mut ref u8, value: mut FlagValue }

One registered flag.

Fields

NameType
nextmut ref Flag
namemut ref u8
aliasmut ref u8
helpmut ref u8
placeholdermut ref u8
default_textmut ref u8
valuemut FlagValue

FlagSet

pub type FlagSet = { name: mut ref u8, description: mut ref u8, head: mut ref Flag, tail: mut ref Flag, positional: mut ref list[ref u8], allow_double_dash: mut bool }

A collection of flags and the arguments left over after parsing.

Fields

NameType
namemut ref u8
descriptionmut ref u8
headmut ref Flag
tailmut ref Flag
positionalmut ref list[ref u8]
allow_double_dashmut bool

new

pub fn new(name: ref u8, description: ref u8) result[FlagSet, InitError] = trust { ... }

Create an empty flag set. name is shown in the usage text.

allow_double_dash

pub fn allow_double_dash(fs: mut FlagSet, enabled: bool) void

Accept --name spellings as well as the default -name spelling. Also makes a bare -- terminate flag parsing.

add

private
fn add(fs: mut FlagSet, name: ref u8, alias: ref u8, help: ref u8, placeholder: ref u8, default_text: ref u8, value: FlagValue) void = trust { ... }

bool_var

pub fn bool_var(fs: mut FlagSet, name: ref u8, alias: ref u8, target: ref bool, help: ref u8) void = trust { ... }

Register a boolean flag. The flag is false until it appears on the command line; an explicit value may be given with : or =.

string_var

pub fn string_var(fs: mut FlagSet, name: ref u8, alias: ref u8, target: ref ref u8, help: ref u8) void = trust { ... }

Register a string flag. Values are borrowed pointers, not copies.

int_var

pub fn int_var(fs: mut FlagSet, name: ref u8, alias: ref u8, target: ref i64, help: ref u8) void = trust { ... }

Register a signed integer flag.

float_var

pub fn float_var(fs: mut FlagSet, name: ref u8, alias: ref u8, target: ref f64, help: ref u8) void = trust { ... }

Register a floating-point flag.

parse

pub fn parse(fs: FlagSet, argv: list[ref u8]) result[void, ParseError] = trust { ... }

Parse command-line arguments. The first entry is treated as the program name and skipped, matching @args() and os.get_args().

Only the first positional -- is special, and only when double-dash support is enabled. Parsing stops at the first error; values already seen remain set.

args

pub fn args(fs: FlagSet) list[ref u8] = trust { ... }

Positional arguments collected by the last call to parse.

usage

pub fn usage(fs: FlagSet) void = trust { ... }

Print the generated usage text to stdout.

usage_err

pub fn usage_err(fs: FlagSet) void = trust { ... }

Print the generated usage text to stderr.

destroy

pub fn destroy(fs: FlagSet) void = trust { ... }

Release the flag set, its registered flags, and the positional argument list. Target values and borrowed strings are not freed.

is_help

pub fn is_help(e: ParseError) bool

True when parse stopped because -h or -help was requested.

message

pub fn message(e: ParseError) ref u8 = trust { ... }

A human-readable message for a parse error. The returned string is heap-allocated.

clear_positional

private
fn clear_positional(fs: FlagSet) void = trust { ... }

emit

private
fn emit(to_stderr: bool, line: ref u8) void = trust { ... }

write_usage

private
fn write_usage(fs: FlagSet, to_stderr: bool) void = trust { ... }

span_equals

private
fn span_equals(span: ref u8, len: i64, other: ref u8) bool = trust { ... }

find_name

private
fn find_name(fs: FlagSet, name: ref u8, len: i64) ref Flag = trust { ... }

find_separator

private
fn find_separator(s: ref u8) i64 = trust { ... }

value_of

private
fn value_of(argv: list[ref u8], body: ref u8, sep: i64, i: mut ref i64, argc: i64) ref u8 = trust { ... }

parse_bool

private
fn parse_bool(value: ref u8) option[bool]

looks_like_int

private
fn looks_like_int(value: ref u8) bool = trust { ... }

parse_int

private
fn parse_int(name: ref u8, value: ref u8) result[i64, ParseError] = trust { ... }

parse_float

private
fn parse_float(name: ref u8, value: ref u8) result[f64, ParseError]

assert_ok

private
fn assert_ok(r: result[void, ParseError]) void