Process Arguments
process.argv() returns the full argument vector the OS delivered to the
process: the executable path, run, the script name, and then everything the
user passed. The return type is [str] — a plain list of strings with no
flag interpretation.
Iterates process.argv() to print each entry, then defines user_args() which
finds the position after the .zolo file and returns only the user arguments.
Run with zolo run 01-process-args.zolo foo bar to see the five entries.
// Feature: command-line arguments — `process.argv()`
// Syntax: `process.argv()` returns the full OS list (interpreter,
// `run`, script, then the user args).
// When to use: small CLI scripts; for structured flag/argument parsing,
// use `@cli` (category 12-decorators) or `Cli.__user_argv()`.
use std::Cli
use std::process
let all = process.argv()
print("argv has {all.len()} entries:")
for arg in all {
print(" {arg}")
}
// expected when invoked as `zolo run script.zolo foo bar`:
// argv has 5 entries:
// <path>/zolo.exe
// run
// script.zolo
// foo
// bar
// To get only the user args, skip past the `.zolo` entry.
fn user_args() -> [str] {
let raw = process.argv()
var start = 0
for i in 0..raw.len() {
let s = raw[i]
// `.sub(-5)` takes the last 5 chars; in Zolo, strings have a `.sub` method.
if s.sub(-5) == ".zolo" {
start = i + 1
break
}
}
var out: [str] = []
for i in start..raw.len() {
out.push(raw[i])
}
return out
}
let user = user_args()
print("user args: {user.len()}")
for a in user {
print(" {a}")
}
Requires the Zolo CLI/host — open in the playground or run locally.
For scripts that need named flags, defaults, and automatic --help, prefer the
@cli builder described on the following pages.
Challenge
Modify user_args() to accept -- as an alternative separator (POSIX-style)
and return everything that comes after it.
See also