This library is in flux. APIs may change without notice. Generated from source with koruc 0.1.7 on 11/4/2026.
Cli
import std/cliKoru Standard Library: std/cli — a command surface for programs.
cli.kz · 3 tors
Koru Standard Library: std/cli — a command surface for programs. · 49 more lines
Koru Standard Library: std/cli — a command surface for programs.
import std/cli
std/cli:flag(verbose) {
"type": "boolean", // boolean | string | int | float
"short": "v",
"description": "chatter"
}
std/cli:command(run) {
"description": "run the thing",
"alias": "r",
"subcommands": ["check", "fire"]
}
std/cli()
| run { subcommand, file: string, verbose } |> handle(subcommand, file, verbose)
| build |> build_all()
| usage { message } |> complain(message)
| help { text } |> std/io:print.ln(text)
The declares are [comptime|norun] Source records — the compiler.kz
flag.declare/command.declare idiom, name as the positional arg, metadata
in the block body. `std/cli()` resolves to this module's `default` tor —
a [keyword|comptime|transform] that harvests the declares from the
program AST and the command arms from its own continuations, and emits
an argv parser + generated --help + dispatch. The std/json:parse emit
pattern end to end.
THE DESTRUCTURE IS THE SCHEMA (the json:parse license): a field whose
name matches a declared flag (`verbose`) binds the parsed flag value;
the reserved name `subcommand` binds the first token after the command,
constrained against the declare's "subcommands" list when one exists;
every other field is a positional in written order, typed by its spec —
`file: string` hands a []const u8, `count: i64` parses the token,
`?string` admits a missing trailing argument, `[]string` binds the rest.
Bare fields bind strings. A command arm with no matching
`std/cli:command` declare still dispatches — declares carry prose
(help text, alias, subcommand validation), arms carry shape.
`| usage { message, help }` and `| help { text }` are optional arms.
Absent: --help prints the generated text and exits 0; a usage fault
prints the message + generated text and exits 2. Bare invocation (no
command token) shows help and exits 0.
Program name/version in the usage line are harvested from a
std/build:app declaration when one exists (identity lives there, not
here) and fall back to the entry-file stem.
// DECLARATIONS — norun Source records, harvested by the default transform.
// Every field a declare accepts feeds help, dispatch, or a refusal.
~[comptime|norun] pub tor flag { name: string, source: Source }~[comptime|norun] pub tor command { name: string, source: Source }// CLI.DEFAULT — the dispatch site transform
~[keyword|comptime|transform] pub tor default {
invocation: *const Invocation,
item: std/compiler:*const Item,
program: std/compiler:*const Program,
reporter: std/compiler:*ErrorReporter,
allocator: std.mem.Allocator
}
| * *
| ?usage { message: string, help: string }
| ?help string