Issue #2640: Command[f, g]: main picks a function by the first word (ADR-cli-v1 D9, D10)

Open and answer this issue on GitHub.

ready · opened 2026-11-21 · updated 2026-11-21 · 0 comments

Part of #449 (subcommands of main, option A). After #2638 (struct spread into options) and #2639 (struct options of main are flattened): the body command.run(..command.args; ..command.options) needs both spreads, and the picked function’s struct options are read as main’s are.

Decision: ADR-cli-v1 D9 and D10; Part I 5.5.

Probe

/// Runs one step.
fn run_step(step: String) {
    println(f"run {step}")
}

/// Picks the next focus.
fn focus_next(file: String; depth: int = 1) {
    println(f"focus {file} {depth}")
}

fn main(command: Command[run_step, focus_next]) {
    command.run(..command.args; ..command.options)
}

Today: a parse error at .. (the spreads), and Command is no type.

Expected

prog focus-next areas.md --depth 2 prints focus areas.md 2; prog run-step build prints run build; prog nothing is a wrong word (exit 2, the error, the usage). Command[f, g, …] is a prelude type naming functions of the program, allowed only as the type of main’s one subject (elsewhere NK1273). The value has run (the picked function), args (a tuple of its subject values) and options (a struct of its option values). The spread call is checked once per named function, as that written-out call. Refused while the program is built (NK1358, reserved): let a = command.args; command.run(..a), focus_next(..command.args), a spread of command.options from a mut command.

To build

  1. Type and check: Command[...] in the prelude and the checker (check_main.nika for its place; the type checker for run/args/options, whose types are per picked function). The spread call checks against each function of the list. Tests cli_command_outside_main.nika + .stderr (NK1273), cli_command_not_the_only_subject.nika + .stderr (NK1273), cli_command_args_copied.nika + .stderr, cli_command_args_into_named_function.nika + .stderr, cli_command_mut.nika + .stderr (all NK1358), cli_command_names_no_function.nika + .stderr.
  2. Runtime in cli.nika: an Interface per command function plus a reader that takes the first positional word, maps - to _, and reads the rest into that function’s Interface (D1-D2, with the flattening of the struct-option issue). A first word naming no function is a Stop::Wrong.
  3. Lowering in emit_entry.nika: one Interface per named function; main’s body is lowered with command.run(..) as a match over the picked function, each arm calling that function with its read words. Test cli_command_picks_a_function.nika + .stdout (the probe, in the form of cli_words_are_sorted_into_the_parameters.nika: both commands, a dashed name, a wrong word, exit statuses).
  4. Part III 30: NK1358 to the built list.

Done when

The tests pass, the cli_* tests stay green; step 4’s commit carries Fixes #N; gaps (e.g. a type whose fields depend on a runtime choice, if the checker has no form for it) are Found: lines with Needs: supervisor decision, not a workaround.

Files: the prelude, check_main.nika, the type checker’s .nika for field access, emit_entry.nika, cli.nika, Part III 30, tests in tests/compiler/tests/. No new Rust.

Facts: nikaia test --filter cli_command from tests/compiler.

Ruled out: an enum the program declares for the commands (option B of #449, not chosen); a match on command in the program (the spread is the one form, D10); how a test calls a main that takes a Command (not decided, ADR-cli-v1 §6: a test calls the function itself).

Back to Open Issues