Argumentos del Proceso
En esta página
process.argv() devuelve el vector completo de argumentos que el SO entregó al
proceso: la ruta del ejecutable, run, el nombre del script y, a continuación,
todo lo que el usuario pasó. El tipo de retorno es [str] — una lista simple de
strings sin ninguna interpretación de flags.
Itera process.argv() para mostrar cada entrada, luego define user_args() que
busca la posición después del archivo .zolo y devuelve solo los argumentos del
usuario. Ejecuta con zolo run 01-process-args.zolo foo bar para ver las cinco
entradas.
// Feature: command-line arguments — `process.argv()`
// Syntax: `process.argv()` returns the USER-FACING argument list:
// `[program, args...]`.
// When to use: small CLI scripts; for structured flag/argument parsing,
// use `@cli` (category 12-decorators) or `Cli.__user_argv()`.
//
// The user-facing model is the SAME whether the program runs under the VM
// (`zolo run script.zolo foo bar`) or as a compiled native binary
// (`./script foo bar`):
// * argv[0] is the PROGRAM — the `.zolo` script under the VM, the compiled
// executable natively. The interpreter chain (`zolo`, `run`) and zolo's
// own flags (`--no-cache`, `--time`, …) are HIDDEN from the script.
// * argv[1..] are the user-passed arguments, verbatim.
//
// parity-normalize: path
// argv[0] is an absolute path that differs between the VM (script) and the
// compiled exe — collapsed to <PATH>. The counts around it stay exact, so a
// wrong argv length would still diverge.
use std::process
let all = process.argv()
print("argv has {all.len()} entries")
// argv[0] — the program (script under the VM, binary natively).
print(all[0])
// The user args are everything after argv[0] — identical logic on both
// backends now that the interpreter chain is hidden.
fn user_args() -> [str] {
let raw = process.argv()
var out: [str] = []
for i in 1..raw.len() {
out.push(raw[i])
}
return out
}
let user = user_args()
print("user args: {user.len()}")
for a in user {
print(" {a}")
}
Requiere la CLI o el host de Zolo; ábrelo en el playground o ejecútalo localmente.
Para scripts que necesitan flags con nombre, defaults y --help automático,
prefiere el builder @cli descrito en las páginas siguientes.
Desafío
Modifica user_args() para aceptar -- como separador alternativo (al estilo
POSIX) y devolver todo lo que venga después.
Consulta también