Argumentos do Processo
Nesta página
process.argv() retorna o vetor completo de argumentos que o SO entregou ao
processo: o caminho do executável, run, o nome do script e, em seguida, tudo
que o usuário passou. O retorno é [str] — uma lista simples de strings sem
nenhuma interpretação de flags.
Itera process.argv() para mostrar cada entrada, depois define user_args() que
busca o ponto após o arquivo .zolo e devolve apenas os argumentos do usuário.
Execute com zolo run 01-process-args.zolo foo bar para ver as 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}")
}
Requer a CLI/host do Zolo — abra no playground ou rode localmente.
Para scripts que precisam de flags nomeadas, defaults e --help automático,
prefira o builder @cli descrito nas páginas seguintes.
Desafio
Modifique user_args() para aceitar um argumento -- como separador alternativo
(ao estilo POSIX) e retornar tudo que vier depois dele.
Veja também