Module: sys.optparse

Source: ./sys/optparse.reef


Overview

sys.optparse - hierarchical CLI option parser (clap-style)

Cells returned by bool_flag / int_flag / string_flag / arg / rest are the same objects parse fills. See docs/superpowers/specs/2026-08-25-sys-optparse-design.md.


Types

Status (enum)

Variants:

  • OkParsed
  • OkHelp
  • OkVersion
  • Err(ParseError)

ParseError (enum)

Variants:

  • UnknownFlag(string)
  • InvalidFlag(string)
  • MissingValue(string)
  • InvalidValue(string, string)
  • MissingFlag(string)
  • UnknownCommand(string)
  • MissingCommand
  • GlobalMisplaced(string)
  • MissingArg(string)
  • UnexpectedArg(string)

TokenKind (enum)

Variants:

  • TokEndOpts
  • TokLong
  • TokShort
  • TokInvalid
  • TokPositional

Token

Fields:

Name Type
kind TokenKind
raw string
name string
short_ch char
has_eq bool
eq_value string

FlagApply (enum)

Variants:

  • Applied(int)
  • Failed(ParseError)

IntParse

Fields:

Name Type
ok bool
val int

ParseState

Fields:

Name Type
argv [string]
help_seen bool
version_seen bool
parse_root Command

ChildFind

Fields:

Name Type
ok bool
child Command

DeclSlot (enum)

Variants:

  • DeclBool(int)
  • DeclInt(int)
  • DeclString(int)

BoolFlag

Fields:

Name Type
long_name string
short_name char
help string
val bool
default_val bool
was_set bool
required bool

IntFlag

Fields:

Name Type
long_name string
short_name char
help string
val int
default_val int
was_set bool
required bool

StringFlag

Fields:

Name Type
long_name string
short_name char
help string
val string
default_val string
was_set bool
required bool

Arg

Fields:

Name Type
arg_name string
val string
was_set bool

Rest

Fields:

Name Type
vals [string]

Command

Fields:

Name Type
cmd_name string
about_text string
version_str string
children [Command]
bools [BoolFlag]
ints [IntFlag]
strings [StringFlag]
pos_args [Arg]
rest_cell Rest
has_rest bool
args_closed bool
invoked_leaf Command
path_str string
parse_root_name string
decls [DeclSlot]

Functions

fn command(name: string): Command

fn program(): string

fn argc(): int

fn argv_get(index: int): string

fn format(e: ParseError): string

fn classify(tok: string): Token

fn parse_int_operand(s: string): IntParse

fn apply_bool_cell(cmd: Command, idx: int, t: Token, i: int): FlagApply

fn apply_int_cell(cmd: Command, idx: int, t: Token, argv: [string], i: int): FlagApply

fn apply_string_cell(cmd: Command, idx: int, t: Token, argv: [string], i: int): FlagApply

fn apply_long(cmd: Command, t: Token, argv: [string], i: int): FlagApply

fn apply_short(cmd: Command, t: Token, argv: [string], i: int): FlagApply

fn apply_positional(cmd: Command, raw: string, i: int): FlagApply

fn finish_leaf(cmd: Command): Status

fn flag_option_syntax(short: char, long_name: string, hint: string): string

fn widen_usage_col(w: int, syntax: string): int

fn usage_col_width(cmd: Command): int

fn commands_name_width(cmd: Command): int

fn command_path(ancestors: [Command], leaf: Command): string

fn finish_help(cmd: Command, state: ParseState, ancestors: [Command]): Status

fn finish_version(state: ParseState): Status

fn version_builtin_ok(state: ParseState, ancestors: [Command]): bool

fn is_version_token(tok: string): bool

fn command_has_long(cmd: Command, name: string): bool

fn command_has_short(cmd: Command, ch: char): bool

fn command_has_token_flag(cmd: Command, t: Token): bool

fn rewrite_unknown(tok: string, ancestors: [Command]): ParseError

fn maybe_misplaced(e: ParseError, ancestors: [Command]): ParseError

fn find_child(cmd: Command, name: string): ChildFind

fn walk(cmd: Command, state: ParseState, i: int, ancestors: [Command]): Status

fn parse_select(cmd: Command, state: ParseState, start: int, ancestors: [Command]): Status

fn parse_leaf(cmd: Command, state: ParseState, start: int, ancestors: [Command]): Status


Procedures

proc abort_decl(msg: string)

proc check_command_name(name: string)

proc check_flag_decl(cmd: Command, name: string, short: char)

proc reset_command(c: Command)

proc print_option_line(syntax: string, help: string, width: int)

proc print_decl_option(cmd: Command, slot: DeclSlot, w: int)

proc mark_invoked(cmd: Command, state: ParseState, ancestors: [Command])


Generated by reefc doc