Skip to content

Process Arguments

On this page

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.

01-process-args.zolo
// 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}")
}

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

Search Zolo

9 results

enespt-br