← All examples

Args

Command-line arguments, as C hands them over: MAIN may take 'argc' and 'argv', the program's name first, each word NUL-ended, a null pointer after the last.

args.zg
# Command-line arguments, as C hands them over (owner, 2026-09-29: "low-level shit, decided"): MAIN may
# take 'argc' and 'argv', the program's name first, each word NUL-ended, a null pointer after the last.
# Reading them is C's world, so it is no_check; a friendly list will be written in Zag on top.
# examples/args.args holds the words the test passes: apples, 3, hello world.
MAIN ['argc': int32, 'argv': ptr[ptr[uint8]]] {
    std::print.stored["argc = ", 'argc', newln];
    let mut 'i': int32 = 1;
    while 'i' < 'argc' {
        no_check let 'word' := ptr.read[ptr.at['argv', by 'i']];
        let mut 'k': int32 = 0;
        let mut 'bytes': list[uint8] = [];
        loop {
            no_check let 'b' := ptr.read[ptr.at['word', by 'k']];
            if 'b' equal_to 0 { break; }
            list.append[lend.mut 'bytes', value 'b'];
            change 'k' = 'k' plus 1;
        }
        let 'text' := to.str.utf8['bytes'] otherwise "(not UTF-8)";      # bytes into text (owner, 2026-09-30)
        std::print.stored["argument ", 'i', ": ", 'text', newln];
        change 'i' = 'i' plus 1;
    }
    no_check let 'end' := ptr.read[ptr.at['argv', by 'argc']];
    std::print.stored["argv[argc] is null: ", ptr.is_null['end'], newln];
}
Output

Run with apples3hello world

argc = 4
argument 1: apples
argument 2: 3
argument 3: hello world
argv[argc] is null: true