F3.2: std.cli minimal subcommand + flag parser over explicit []string
Extend std/cli.sx with a zero-heap argument parser that the caller drives
over a logical argv ([]string), separate from the F3.1 os_args accessor.
Grammar: <group> <command> [--flag VALUE | --bool]... [--json] [-- rest...]
- (group, command) dispatched against a caller-provided Command table;
no match -> error.UnknownCommand.
- value-taking vs boolean flags fixed by each command's FlagSpec list;
--json is a reserved global boolean surfaced as parsed.json.
- `--` or the first bare operand ends flag parsing; the remainder is
parsed.rest (operand views).
Heap discipline (heap-discipline.md): zero heap, zero copy. group/command/
flag values/rest are all VIEWS into args. Parsed is a by-value stack struct;
flag presence/values live in a fixed [16]FlagValue inline array indexed by
spec position (no per-flag allocation, no context.allocator). The flag-spec
list and command table are caller storage passed as views.
Failure surfacing (no silent skip): unknown command, unknown flag, a
value-flag missing its value, and an absent required flag each raise a
specific CliError variant; a caller-owned Diag records the offending token
(index + view) before each raise, since error tags carry no data.
examples/0717 drives the parser over explicit []string vectors: a valid
group/command/--flag/--bool/--json case (asserting parsed values + that
values are views into argv), subcommand dispatch, `--`/bare-operand
separators, and the five failure variants each asserted via destructure +
Diag. zig build && zig build test && run_examples.sh green (385 passed).
This commit is contained in:
33
examples/expected/0717-modules-cli-parse.stdout
Normal file
33
examples/expected/0717-modules-cli-parse.stdout
Normal file
@@ -0,0 +1,33 @@
|
||||
dispatch-group: ok
|
||||
dispatch-command: ok
|
||||
dispatch-index: ok
|
||||
flag-value: ok
|
||||
flag-value-set: ok
|
||||
bool-set: ok
|
||||
json-set: ok
|
||||
no-rest: ok
|
||||
value-is-view: ok
|
||||
group-is-view: ok
|
||||
dispatch-2nd: ok
|
||||
2nd-bool: ok
|
||||
2nd-json-unset: ok
|
||||
sep-value: ok
|
||||
sep-rest-len: ok
|
||||
sep-rest-0: ok
|
||||
sep-rest-1: ok
|
||||
sep-no-bool: ok
|
||||
bare-rest-len: ok
|
||||
bare-rest-0: ok
|
||||
dash-value: ok
|
||||
err-unknown-cmd: ok
|
||||
err-unknown-group: ok
|
||||
err-too-few: ok
|
||||
err-unknown-flag: ok
|
||||
err-missing-value: ok
|
||||
err-value-eats-flag: ok
|
||||
err-missing-req: ok
|
||||
diag-flag-tag: ok
|
||||
diag-flag-token: ok
|
||||
diag-req-tag: ok
|
||||
diag-req-token: ok
|
||||
=== DONE ===
|
||||
Reference in New Issue
Block a user