std/argparse
CLI argument parsing with typed flags, positional args, and auto-generated help text.
from "std/argparse" import { ArgParser, ParsedArgs }Quick start
from "std/argparse" import { ArgParser, ParsedArgs }
fn main(): i32 {
var parser = ArgParser.new("greet", "A greeting tool")
parser.addRequired("name", "n", "Name to greet")
parser.addBool("loud", "l", "Shout the greeting")
parser.addOptionalPositional("title", "Optional title prefix")
let args = parser.parse()
let name = args.getString("name") ?? ""
if args.getBool("loud") {
print($"HELLO, {name}!")
} else {
print($"Hello, {name}")
}
return 0
}$ greet --name Alice --loud
HELLO, Alice!
$ greet --help
greet - A greeting tool
usage: greet [options] [title]
arguments:
<title> Optional title prefix
options:
-n, --name <value> Name to greet (required)
-l, --loud Shout the greeting
-h, --help Show this help messageDefine flags, call parse(), access typed values. --help is generated automatically. Missing required flags print an error with usage.
Trailing arguments
enableTrailingArgs stops flag parsing at the first positional, so everything after it is collected verbatim. That is what a runtime or wrapper wants: flags before the command are yours, everything after belongs to the child.
var parser = ArgParser.new("node-milo", "JS runtime")
parser.addBool("version", "v", "Print version")
parser.addOptionalPositional("script", "Script to run")
parser.enableTrailingArgs()
let args = parser.parse()
// node-milo script.js --foo -> --foo lands in args.positional, not parsed as a flagAPI reference
ArgEntry
pub struct ArgEntryFields: name: string, value: string, present: bool.
ArgParser
pub struct ArgParserDeclarative command-line argument parser with auto-generated --help. Create with ArgParser.new(), add flags with addString/addBool/addRequired, then call .parse() to get a ParsedArgs.
Fields:
name: stringdescription: stringusage: stringepilog: string: Free text printed after the options block — the place for controls, an example invocation, or anything the per-flag help lines cannot say.flags: Vec<FlagDef>positionals: Vec<PositionalDef>trailingArgs: boolignoreUnknown: bool
ArgParser.addBool
fn ArgParser.addBool(self: &mut ArgParser, long: string, short: string, help: string): voidAdd a boolean flag (present = true, absent = false).
ArgParser.addI64
fn ArgParser.addI64(self: &mut ArgParser, long: string, short: string, help: string, defaultVal: i64): voidAdd an integer flag with a default value. Validated as numeric at parse time.
ArgParser.addOptionalPositional
fn ArgParser.addOptionalPositional(self: &mut ArgParser, name: string, help: string): voidAdd an optional positional argument.
ArgParser.addPositional
fn ArgParser.addPositional(self: &mut ArgParser, name: string, help: string): voidAdd a required positional argument.
ArgParser.addRequired
fn ArgParser.addRequired(self: &mut ArgParser, long: string, short: string, help: string): voidAdd a required string flag. parse() exits with error if missing.
ArgParser.addString
fn ArgParser.addString(self: &mut ArgParser, long: string, short: string, help: string, defaultVal: string): voidAdd a string flag with long name, short alias, help text, and default. Example: parser.addString("output", "o", "Output file", "out.txt")
Pass "" as defaultVal for no default.
ArgParser.enableIgnoreUnknown
fn ArgParser.enableIgnoreUnknown(self: &mut ArgParser): voidSilently skip unrecognized flags instead of exiting with an error.
ArgParser.enableTrailingArgs
fn ArgParser.enableTrailingArgs(self: &mut ArgParser): voidStop flag parsing after the first positional arg. Remaining args collected as positionals without interpretation.
For a runtime or wrapper: flags before the command are yours, everything after belongs to the child. A -- separator always ends flag parsing, whatever this setting.
ArgParser.helpText
fn ArgParser.helpText(self: &ArgParser): stringGenerate formatted help text for all registered flags.
ArgParser.new
fn ArgParser.new(name: string, description: string): ArgParserCreate a parser with a program name and description.
ArgParser.parse
fn ArgParser.parse(self: &ArgParser): ParsedArgsParse command-line arguments and return ParsedArgs. Automatically handles --help. Exits on invalid input.
Returns no Result: a missing required flag prints usage and exits, so the value is always usable. parseFrom takes the argv vector instead, which is what a test wants.
ArgParser.parseFrom
fn ArgParser.parseFrom(self: &ArgParser, argv: Vec<string>): ParsedArgsParse from a provided argument list instead of process args. argv[0] is treated as the program name (skipped during parsing). Like Python's parse_args(args=[...]).
ArgParser.setEpilog
fn ArgParser.setEpilog(self: &mut ArgParser, text: string): voidText printed at the end of --help, after the options. Example: parser.setEpilog("controls:\n UP / DOWN pitch")
FlagDef
pub struct FlagDefFields: longName: string, shortName: string, help: string, defaultVal: string, isBool: bool, isI64: bool, required: bool.
ParsedArgs
pub struct ParsedArgsParsed command-line arguments. Access values with .getString() (Option), .getI64(), .getU16(), .getBool(). Check presence with .has(). Positional args in .positional field.
Fields: prog: string, entries: Vec<ArgEntry>, positional: Vec<string>.
ParsedArgs.getBool
fn ParsedArgs.getBool(self: &ParsedArgs, name: &string): boolCheck if a boolean flag was set.
ParsedArgs.getI64
fn ParsedArgs.getI64(self: &ParsedArgs, name: &string): i64Get an integer value of a flag. Exits if the value is not numeric.
ParsedArgs.getString
fn ParsedArgs.getString(self: &ParsedArgs, name: &string): Option<string>Value of the flag or positional name, or None if it has no value: either name was never declared, or it was declared with no default and not supplied. --name "" is supplied, so it is Some("").
A declared default is the value when the flag is absent, so it comes back as Some. An empty default is how addString spells "no default" (helpText only prints a default when it is non-empty), which is why the check is on the value's length and not on a separate flag.
ParsedArgs.getU16
fn ParsedArgs.getU16(self: &ParsedArgs, name: &string): u16Get a u16 value of a flag. Exits if out of range 0..65535.
ParsedArgs.has
fn ParsedArgs.has(self: &ParsedArgs, name: &string): boolCheck if a flag was provided on the command line.
PositionalDef
pub struct PositionalDefFields: name: string, help: string, required: bool.