cli: shell completion scripts (CLI slice 3) #125

Closed
opened 2026-10-05 21:07:23 +00:00 by orion · 15 comments
Collaborator

Goal

Shell completion for any program declared with std/cli, generated from the same declaration as the help text (CLI.md section 2, point 2: "later, generated from the same declaration"). This issue is the std/cli half of slice 3: a pure function that turns a Command tree into the completion script of a shell. Exposing it as torb completions <shell> is the compiler half and a follow-up (see Seed).

Decisions (by the loop, from CLI.md and docs/standard-library/cli.md)

  • Shells: bash, zsh, fish and PowerShell, as the original criterion says.
  • Where it lives: std/cli, because the declaration (Command with public commands, flags, options, positional) is there; Command.completion(shell: Shell): String is pure like parse and help, so a test needs no process and no shell.
  • How a user gets the script: the program prints it (torb completions bash > ...); the generator never writes a file itself, so the package stays pure except for run.
  • What is completed: subcommand names at every depth, --flag and --option names of the command the line is in, plus -h and --help. Values of options are not completed (OptionSpec has a type name but no value list); the shell falls back to file names. Completing values is a later slice and needs an Argument hook.
  • Test: golden output. A fixed small declaration (nested subcommand, a flag, an option, a repeatable, a passthrough arguments) is rendered for each shell and compared with a checked-in expected text; one test per shell. No test starts a real shell.

Requirements

  • R1. The package std/cli shall provide a Shell type with the cases Bash, Zsh, Fish and PowerShell, and Command.completion(shell: Shell): String that answers the completion script of the command tree.
  • R2. The script shall be derived only from the declaration (name, commands, flags, options, positional), so a new flag or subcommand appears without a second list to edit.
  • R3. The script shall complete the subcommands of the command the cursor is in, and the --name of every flag and option of that command plus -h and --help, at every depth: after torb test the offered names are those of test.
  • R4. Where an option takes a value, the script shall offer no value candidates and leave the position to the shell's file completion.
  • R5. The scripts shall use each shell's documented mechanism: bash complete -F, zsh #compdef with a completion function, fish complete -c, PowerShell Register-ArgumentCompleter -Native.
  • R6. Command.completion shall be pure and write nothing.
  • R7. docs/design/CLI.md and docs/standard-library/cli.md shall record these decisions, and the std/cli declaration blocks shall list Shell and completion (the docs check keeps them in sync).

Done when

  • std/cli/tests/cli.test.trb (or a new file beside it) has one golden test per shell (bash, zsh, fish, PowerShell) that compares the generated text with the expected text for a fixed declaration with a nested subcommand, a flag, an option, a repeatable and a passthrough arguments spec.
  • A test shows a nested path: the test subcommand's script part lists test flags and options and not those of its parent.
  • Adding a flag to the fixed declaration changes the generated script (a test builds the declaration twice and compares), showing R2.
  • docs/standard-library/cli.md and docs/design/CLI.md describe Shell and Command.completion and mark slice 3 done for std/cli; the torb:declaration blocks match the source (docs check green).
  • sh tools/gates.sh a green in forge CI; tools/check-commits.sh clean; one or two Conventional Commits (feat(std/cli): ..., docs(cli): ...).
  • After merge, a follow-up issue cli: torb completions <shell> is filed (just in time, depends on this issue and a seed refresh). Filed as #807.

Seed

This issue does not touch the seed: it adds new API to std/cli only, with no syntax change, no new native, no driver flag and no std name the compiler looks up by string. The compiler imports std/cli through the seed, so torb completions <shell> (calling completion from compiler/src/cli/declaration.trb) can only land after a seed refresh that contains this change; that refresh stays Torben's. That is why the subcommand is a separate follow-up and not a second commit here.

Source

  • CLI.md slice 3
  • docs/standard-library/cli.md (declarations of Command, OptionSpec, ArgumentsSpec)

Dependencies

Depends on #123 (closed). Slice 2 (#124) is not needed: the generator works on any Command.

Moved from the planning lists of the docs; the design record stays the specification.

## Goal Shell completion for any program declared with `std/cli`, generated from the same declaration as the help text (CLI.md section 2, point 2: "later, generated from the same declaration"). This issue is the std/cli half of slice 3: a pure function that turns a `Command` tree into the completion script of a shell. Exposing it as `torb completions <shell>` is the compiler half and a follow-up (see Seed). ## Decisions (by the loop, from CLI.md and docs/standard-library/cli.md) - **Shells:** bash, zsh, fish and PowerShell, as the original criterion says. - **Where it lives:** `std/cli`, because the declaration (`Command` with public `commands`, `flags`, `options`, `positional`) is there; `Command.completion(shell: Shell): String` is pure like `parse` and `help`, so a test needs no process and no shell. - **How a user gets the script:** the program prints it (`torb completions bash > ...`); the generator never writes a file itself, so the package stays pure except for `run`. - **What is completed:** subcommand names at every depth, `--flag` and `--option` names of the command the line is in, plus `-h` and `--help`. Values of options are not completed (`OptionSpec` has a type name but no value list); the shell falls back to file names. Completing values is a later slice and needs an `Argument` hook. - **Test:** golden output. A fixed small declaration (nested subcommand, a flag, an option, a repeatable, a passthrough `arguments`) is rendered for each shell and compared with a checked-in expected text; one test per shell. No test starts a real shell. ## Requirements - R1. The package std/cli shall provide a `Shell` type with the cases Bash, Zsh, Fish and PowerShell, and `Command.completion(shell: Shell): String` that answers the completion script of the command tree. - R2. The script shall be derived only from the declaration (`name`, `commands`, `flags`, `options`, `positional`), so a new flag or subcommand appears without a second list to edit. - R3. The script shall complete the subcommands of the command the cursor is in, and the `--name` of every flag and option of that command plus `-h` and `--help`, at every depth: after `torb test` the offered names are those of `test`. - R4. Where an option takes a value, the script shall offer no value candidates and leave the position to the shell's file completion. - R5. The scripts shall use each shell's documented mechanism: bash `complete -F`, zsh `#compdef` with a completion function, fish `complete -c`, PowerShell `Register-ArgumentCompleter -Native`. - R6. `Command.completion` shall be pure and write nothing. - R7. docs/design/CLI.md and docs/standard-library/cli.md shall record these decisions, and the std/cli declaration blocks shall list `Shell` and `completion` (the docs check keeps them in sync). ## Done when - [x] `std/cli/tests/cli.test.trb` (or a new file beside it) has one golden test per shell (bash, zsh, fish, PowerShell) that compares the generated text with the expected text for a fixed declaration with a nested subcommand, a flag, an option, a repeatable and a passthrough arguments spec. - [x] A test shows a nested path: the `test` subcommand's script part lists `test` flags and options and not those of its parent. - [x] Adding a flag to the fixed declaration changes the generated script (a test builds the declaration twice and compares), showing R2. - [x] `docs/standard-library/cli.md` and `docs/design/CLI.md` describe `Shell` and `Command.completion` and mark slice 3 done for std/cli; the `torb:declaration` blocks match the source (docs check green). - [x] `sh tools/gates.sh a` green in forge CI; `tools/check-commits.sh` clean; one or two Conventional Commits (`feat(std/cli): ...`, `docs(cli): ...`). - [x] After merge, a follow-up issue `cli: torb completions <shell>` is filed (just in time, depends on this issue and a seed refresh). Filed as #807. ## Seed This issue does not touch the seed: it adds new API to `std/cli` only, with no syntax change, no new native, no driver flag and no std name the compiler looks up by string. The compiler imports `std/cli` through the seed, so `torb completions <shell>` (calling `completion` from `compiler/src/cli/declaration.trb`) can only land after a seed refresh that contains this change; that refresh stays Torben's. That is why the subcommand is a separate follow-up and not a second commit here. ## Source - [CLI.md slice 3](https://git.torb.dev/torbscript/language/src/commit/393ef2608e4a60b8ff76d8b6b408951b822d887d/docs/design/CLI.md#L53) - docs/standard-library/cli.md (declarations of `Command`, `OptionSpec`, `ArgumentsSpec`) ## Dependencies Depends on #123 (closed). Slice 2 (#124) is not needed: the generator works on any `Command`. _Moved from the planning lists of the docs; the design record stays the specification._
Author
Collaborator

[loop:torbscript] intake: in scope (author orion, label loop), source CLI.md slice 3, not a duplicate. Depends on #123. next: waiting (dependency #123) until 2026-10-10T04:00:00Z

[loop:torbscript] intake: in scope (author orion, label loop), source CLI.md slice 3, not a duplicate. Depends on #123. next: waiting (dependency #123) until 2026-10-10T04:00:00Z
Author
Collaborator

[loop:torbscript] re-sweep: state check, body unchanged. The dependency #123 closed on 2026-10-23, so the last note (waiting until 2026-10-10) is stale. The acceptance criteria are a single line, and CLI.md says only "later, generated from the same declaration" about this slice, so the scope (which shells, where the completion script is written, how it is tested) is not settled. This is a definition-of-ready gap: refine has to write goal, criteria and the seed statement before ready.

[loop:torbscript] re-sweep: state check, body unchanged. The dependency #123 closed on 2026-10-23, so the last note (waiting until 2026-10-10) is stale. The acceptance criteria are a single line, and CLI.md says only "later, generated from the same declaration" about this slice, so the scope (which shells, where the completion script is written, how it is tested) is not settled. This is a definition-of-ready gap: refine has to write goal, criteria and the seed statement before `ready`.
Author
Collaborator

[loop:torbscript] claim refine by session:loop-torbscript-20261114-0814 until 2026-11-14T09:14:00Z

[loop:torbscript] claim refine by session:loop-torbscript-20261114-0814 until 2026-11-14T09:14:00Z
Author
Collaborator

[loop:torbscript] decided by the loop: scope of slice 3. Shells bash, zsh, fish and PowerShell; one pure Command.completion(shell) in std/cli generated from the declaration; golden-output tests per shell; option values not completed (no value list in OptionSpec); the torb completions <shell> subcommand is a just-in-time follow-up after a seed refresh. No owner decision needed: technical design inside CLI.md. Body rewritten with goal, R1-R7, done criteria and seed statement; label ready set. Size: one PR, one or two commits, so no split. next: implement

[loop:torbscript] release refine by session:loop-torbscript-20261114-0814

[loop:torbscript] decided by the loop: scope of slice 3. Shells bash, zsh, fish and PowerShell; one pure `Command.completion(shell)` in std/cli generated from the declaration; golden-output tests per shell; option values not completed (no value list in `OptionSpec`); the `torb completions <shell>` subcommand is a just-in-time follow-up after a seed refresh. No owner decision needed: technical design inside CLI.md. Body rewritten with goal, R1-R7, done criteria and seed statement; label `ready` set. Size: one PR, one or two commits, so no split. next: implement [loop:torbscript] release refine by session:loop-torbscript-20261114-0814
Author
Collaborator

[loop:torbscript] claim implement by session:loop-torbscript-20261114-1056 until 2026-11-14T12:56:00Z

[loop:torbscript] claim implement by session:loop-torbscript-20261114-1056 until 2026-11-14T12:56:00Z
Author
Collaborator

[loop:torbscript] implement: opened #785 (2 commits: feat(std/cli), docs(cli); R1-R7 and the tests of the done list). Written without a local compiler (CI only by rule), so the golden texts and the hand-edited skills copy are unverified until forge CI runs; a fix round may be needed. next: waiting (review of PR #785)

[loop:torbscript] release implement by session:agent:orion:subagent:875c3410-95a9-4cf5-8391-985a9de04aab

[loop:torbscript] implement: opened https://git.torb.dev/torbscript/language/pulls/785 (2 commits: feat(std/cli), docs(cli); R1-R7 and the tests of the done list). Written without a local compiler (CI only by rule), so the golden texts and the hand-edited skills copy are unverified until forge CI runs; a fix round may be needed. next: waiting (review of PR #785) [loop:torbscript] release implement by session:agent:orion:subagent:875c3410-95a9-4cf5-8391-985a9de04aab
Author
Collaborator

[loop:torbscript] claim implement by session:loop-torbscript-20261114-1616 until 2026-11-14T19:16:00Z

[loop:torbscript] claim implement by session:loop-torbscript-20261114-1616 until 2026-11-14T19:16:00Z
Author
Collaborator

[loop:torbscript] implement: PR #785 had a failing commit-message check because of a style commit. Folded it into the feat commit; PR #785 is now feat + docs, new head c3c38fea9f, tree unchanged, commit lint passes locally. CI re-runs.

next: waiting (CI on PR #785, then review)
[loop:torbscript] release implement by session:loop-torbscript-20261114-1616

[loop:torbscript] implement: PR #785 had a failing commit-message check because of a `style` commit. Folded it into the feat commit; PR #785 is now feat + docs, new head c3c38fea9f429cefdb662e0b3e722adbc1340844, tree unchanged, commit lint passes locally. CI re-runs. next: waiting (CI on PR #785, then review) [loop:torbscript] release implement by session:loop-torbscript-20261114-1616
Author
Collaborator

[loop:torbscript] claim implement by session:loop-torbscript-20261114-1746 until 2026-11-14T19:46:00Z

[loop:torbscript] claim implement by session:loop-torbscript-20261114-1746 until 2026-11-14T19:46:00Z
Author
Collaborator

[loop:torbscript] implement: rebased PR #785 onto main (64f7ca6a), conflicts in docs/design/CLI.md; CI re-runs.
next: waiting (CI on PR #785, then argus review)

[loop:torbscript] implement: rebased PR #785 onto main (64f7ca6a), conflicts in docs/design/CLI.md; CI re-runs. next: waiting (CI on PR #785, then argus review)
Author
Collaborator

[loop:torbscript] release implement by session:loop-torbscript-20261114-1746

[loop:torbscript] release implement by session:loop-torbscript-20261114-1746
Author
Collaborator

Status by orion (PR follow-up after merge): PR #785 (feat(std/cli): shell completion scripts) merged by argus at 2026-11-14T23:02Z as f823aa3f40. Post-merge CI on main (ci / gates, ci / commit messages, pdisc) was still pending at this run.

Remaining, in order:

  1. Confirm main CI is green on f823aa3 (ci / gates, pdisc). If red, fix in a new PR.
  2. Release: the repo builds images on vX.Y.Z tags (release.yml, images.yml), and no version tag exists yet (latest tags: nightly-20261113, seeds). The change is in the nightly line and images edge after main CI. Cut the next release via a version-bump PR (project.trb, compiler/project.trb, editors/vscode/package.json, compiler/src/package/version.trb; version per docs/design/RELEASE.md), reviewer requested.
  3. torb completions <shell> (compiler half, seed refresh) is a separate follow-up issue, not yet filed. This task stays open until it is filed or done.
Status by orion (PR follow-up after merge): PR #785 (`feat(std/cli): shell completion scripts`) merged by argus at 2026-11-14T23:02Z as f823aa3f408246cb1cc99a5196a232325aa61c67. Post-merge CI on main (`ci / gates`, `ci / commit messages`, `pdisc`) was still pending at this run. Remaining, in order: 1. Confirm main CI is green on f823aa3 (`ci / gates`, `pdisc`). If red, fix in a new PR. 2. Release: the repo builds images on vX.Y.Z tags (release.yml, images.yml), and no version tag exists yet (latest tags: nightly-20261113, seeds). The change is in the nightly line and images `edge` after main CI. Cut the next release via a version-bump PR (project.trb, compiler/project.trb, editors/vscode/package.json, compiler/src/package/version.trb; version per docs/design/RELEASE.md), reviewer requested. 3. `torb completions <shell>` (compiler half, seed refresh) is a separate follow-up issue, not yet filed. This task stays open until it is filed or done.
Author
Collaborator

[loop:torbscript] claim refine by session:loop-torbscript-125-followup until 2026-11-15T04:50Z

[loop:torbscript] claim refine by session:loop-torbscript-125-followup until 2026-11-15T04:50Z
Author
Collaborator

Closing: the std/cli half of CLI slice 3 landed in PR #785 (#785, merged as f823aa3f; feature commit 16d67f30, docs commit 3c23fdec). Shell, Command.completion, std/cli/tests/completion.test.trb, and the docs/standard-library/cli.md and docs/design/CLI.md sections are on main; ci / gates was green on f823aa3f, and pdisc is green on the later main commit 35209c5d (the red pdisc on f823aa3f was not caused by a content change of this issue: it passed again two commits later).

The follow-up cli: torb completions <shell> is filed as #807 (#807). It needs a seed refresh that contains PR #785, which stays Torben's, so it is not ready yet.

A release (version tag) is cut outside the loop; this issue does not depend on it.

Closing: the std/cli half of CLI slice 3 landed in PR #785 (https://git.torb.dev/torbscript/language/pulls/785, merged as f823aa3f; feature commit 16d67f30, docs commit 3c23fdec). `Shell`, `Command.completion`, `std/cli/tests/completion.test.trb`, and the docs/standard-library/cli.md and docs/design/CLI.md sections are on `main`; `ci / gates` was green on f823aa3f, and `pdisc` is green on the later main commit 35209c5d (the red `pdisc` on f823aa3f was not caused by a content change of this issue: it passed again two commits later). The follow-up `cli: torb completions <shell>` is filed as #807 (https://git.torb.dev/torbscript/language/issues/807). It needs a seed refresh that contains PR #785, which stays Torben's, so it is not `ready` yet. A release (version tag) is cut outside the loop; this issue does not depend on it.
Author
Collaborator

[loop:torbscript] release refine: verified the merged work on main, filed the follow-up #807 (#807, without ready: needs a seed refresh with PR #785, Torben's), ticked the criteria and closed #125; a release is cut outside the loop. Released the claim of session:loop-torbscript-125-followup. Worker usage: about 60k tokens (sonnet). next: #125 done; #807 waiting (seed refresh)

[loop:torbscript] release refine: verified the merged work on main, filed the follow-up #807 (https://git.torb.dev/torbscript/language/issues/807, without `ready`: needs a seed refresh with PR #785, Torben's), ticked the criteria and closed #125; a release is cut outside the loop. Released the claim of session:loop-torbscript-125-followup. Worker usage: about 60k tokens (sonnet). next: #125 done; #807 waiting (seed refresh)
orion closed this issue 2026-11-15 01:47:49 +00:00
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
torbscript/language#125
No description provided.