Add library/modules/std/cli.sx: a pure-sx command-line argument accessor backed by the macOS C runtime (_NSGetArgv/_NSGetArgc), no compiler change. os_argc() -> s64 os_args(buf: []string) -> []string Zero heap, zero per-arg allocation: os_args fills a caller-provided buffer (stack array) with string VIEWS over the process's own argv block, which lives for the whole process. The returned slice header is a by-value stack return; nothing touches context.allocator. Documents the `sx run` reality: under `sx run <prog.sx> ...` the process argv is the interpreter's argv (sx, run, prog.sx, ...), not a program's logical args. This accessor reports the real process argv truthfully; mapping to logical args is a later consumer concern (distribution P3.1). Non-macOS platforms bail loudly (message + _exit) rather than returning a silent empty. examples/0716-modules-cli-argv.sx asserts only deterministic structural invariants (argc >= 1, argv[0] non-empty, os_argc() == filled length).
32 lines
1.1 KiB
Plaintext
32 lines
1.1 KiB
Plaintext
// Real OS-argv accessor from `modules/std/cli.sx` (#foreign _NSGetArgv).
|
|
//
|
|
// Only DETERMINISTIC structural invariants are asserted — the actual arg
|
|
// contents depend on how the test is invoked (under `sx run` the process
|
|
// argv is the interpreter's: ["sx", "run", "<this file>"]), so we never
|
|
// pin exact strings:
|
|
// - argc >= 1 (every process has argv[0])
|
|
// - argv[0] is non-empty (the executable path)
|
|
// - os_argc() agrees with the filled slice length (no truncation)
|
|
//
|
|
// `buf` is a stack `[64]string`; `os_args` fills it with zero-copy views
|
|
// over the C runtime's argv block — no heap, no per-arg allocation.
|
|
|
|
#import "modules/std.sx";
|
|
#import "modules/std/cli.sx";
|
|
|
|
main :: () {
|
|
buf : [64]string = ---;
|
|
args := os_args(buf[0..64]);
|
|
|
|
if args.len >= 1 { print("argc>=1: ok\n"); }
|
|
else { print("argc>=1: FAIL ({})\n", args.len); }
|
|
|
|
if args.len >= 1 {
|
|
if args[0].len > 0 { print("arg0-nonempty: ok\n"); }
|
|
else { print("arg0-nonempty: FAIL\n"); }
|
|
}
|
|
|
|
if os_argc() == args.len { print("argc-consistent: ok\n"); }
|
|
else { print("argc-consistent: FAIL (os_argc={} len={})\n", os_argc(), args.len); }
|
|
}
|