Nyx by Example

CLI Arguments

Command-line arguments are accessed through get_args(), which returns the full argv array as an Array of String. Index 0 is always the program path, and subsequent indices hold the arguments passed by the user.

Code

// Argumentos de CLI: get_args() retorna un Array de Strings (argv)

fn main() -> int {
    let argv: Array = get_args()
    let total: int = argv.length()

    print("Total de argumentos: " + int_to_string(total))

    var i: int = 0
    while i < total {
        let arg: String = argv[i]
        if i == 0 {
            print("  [" + int_to_string(i) + "] programa: " + arg)
        } else {
            print("  [" + int_to_string(i) + "] arg: " + arg)
        }
        i = i + 1
    }

    if total < 2 {
        print("(sin argumentos — ejecutar con: ./programa arg1 arg2)")
    }

    return 0
}

Output

Total de argumentos: 3
  [0] programa: ./programa
  [1] arg: arg1
  [2] arg: arg2

Explanation

get_args() is a built-in that wraps the C argv array into a Nyx Array. Unlike C, you don't need argc separately — just call .length() on the returned array. The first element (index 0) is always the executable path as the OS reports it.

The while loop with a manual index variable is the standard iteration pattern when you need the index alongside the value. For cases where you only care about the values, Nyx's iterator methods such as .map and .filter are more ergonomic.

The guard at the end detects when no user arguments were provided and prints a usage hint. Building CLI tools in Nyx typically combines get_args() with the std/cli module for flag parsing, but this example shows the raw foundation.

← Previous Next →

Source: examples/by-example/13-cli-args.nx