Skip to content

feat(commander): __schema meta-command + sidecar discovery helpers (spec §4) - #3

Open
dull-bird wants to merge 2 commits into
cli-schema:mainfrom
dull-bird:feat/commander-discovery
Open

dull-bird wants to merge 2 commits into
cli-schema:mainfrom
dull-bird:feat/commander-discovery

Conversation

@dull-bird

Copy link
Copy Markdown

What

@cli-schema/commander builds a valid CLI Schema document from a live command tree, but until now the CLI itself couldn't be discovered — spec §4 defines two mechanisms and the package implemented neither at runtime:

  • §4.1 meta-command — mytool __schema prints the document to stdout, exit 0
  • §4.2 sidecar — <binary>.cli-schema.json shipped next to the binary

This PR adds both:

  • serveSchemaMetaCommand(root, options) — serves __schema by intercepting argv before Commander parsing, and guarantees __schema appears in the emitted document's reservedMetaCommands (merged with caller-declared entries).
  • writeSchemaSidecar(schema, binaryPath) — writes <binary>.cli-schema.json, intended for install/release scripts.

Why argv interception instead of a hidden subcommand

The README currently documents program.command('__schema', { hidden: true }) as the wiring recipe. That works for multi-command programs but has three sharp edges, all avoided by intercepting argv[2] === '__schema' before program.parse():

  1. Routing changes for default-action programs. A single-command program (root .action() only) starts treating bare arguments as unknown-command errors once any subcommand exists — registering __schema changes the CLI's invocation shape. (Same issue surfaced in the Python/Typer integration — see Proposal: official Python Typer/Click exporter SDK cli-schema#6.)
  2. No --help / tree footprint by construction — the meta-command never enters the command tree, rather than entering it and being hidden after the fact. Spec §4.1: implementations SHOULD NOT treat __schema as a subcommand visible in help.
  3. reservedMetaCommands can't drift — the helper injects __schema into the emitted document automatically; the manual recipe relies on the caller remembering to declare it.

The hidden-subcommand pattern is still mentioned in the README as a helper-free alternative, with the caveats noted.

Usage

serveSchemaMetaCommand(program, { name: 'mytool', version: '1.4.2' })
program.parse()

argv, write, and exit are injectable for tests; defaults are process.argv, process.stdout.write, process.exit.

Verification

  • npm run build, npm test (47 pass / 0 fail across workspaces), npm run lint — all clean
  • New tests in packages/commander/test/discovery.test.ts cover: spec-valid output (validated against the meta-schema via @cli-schema/spec), exit code 0, no-op on normal invocations, untouched command tree / absent from --help, default-action program shape preserved, reservedMetaCommands merging, and sidecar file contents.

…pec §4)

Adds serveSchemaMetaCommand, which serves the reserved __schema
meta-command by intercepting argv before Commander parsing instead of
registering a hidden subcommand:

- no --help footprint and no command-tree mutation;
- single-command (default-action) programs keep their invocation shape,
  which subcommand registration would change;
- the emitted document always lists __schema in reservedMetaCommands,
  merged with caller-declared entries.

Also adds writeSchemaSidecar for the <binary>.cli-schema.json sidecar
mechanism, and documents both in the package README.

@Mpdreamz Mpdreamz left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice work — clean design, great test coverage, and the argv-interception rationale is well-articulated. A few small thoughts, none blocking:

  • Return type clarity: serveSchemaMetaCommand returns boolean, but with the real process.exit the return true is never reached. Might be worth a brief JSDoc note that the return value is only meaningful when a custom exit is injected (callers in the wild will just do serveSchemaMetaCommand(…); program.parse() linearly anyway).

  • Sidecar directory assumption: writeSchemaSidecar will throw if the parent directory doesn't exist — totally reasonable, just might be nice to mention in the JSDoc that the caller is responsible for ensuring the path is writable.

  • Trailing args after __schema: the current behaviour (ignore them, serve the schema regardless) seems right to me — spec §4.1 doesn't mandate erroring on extras. A one-line comment in the code noting "trailing arguments are intentionally ignored" would make that decision visible to future readers.

  • reservedMetaCommands ordering: the Set-based merge naturally gives caller entries first, then __schema — works well. Just flagging that the test asserts on that ordering, so if it ever needs to be sorted for spec reasons the test will catch it. 👍

Really like the injectable write/exit/argv pattern — makes the tests a joy to read.


I reviewed this with the help of AI.

- serveSchemaMetaCommand: JSDoc note that the boolean return is only
  observable when a custom `exit` is injected (real process.exit never
  returns).
- serveSchemaMetaCommand: inline comment that trailing args after
  `__schema` are intentionally ignored.
- writeSchemaSidecar: JSDoc note that the caller must ensure the parent
  directory exists and is writable.
@dull-bird

dull-bird commented Jul 26, 2026 •

Copy link
Copy Markdown
Author

@Mpdreamz thanks for the thorough review — I've pushed 390b096 addressing the three doc nits:

  • serveSchemaMetaCommand JSDoc now notes the boolean return is only observable when a custom exit is injected.
  • Added an inline comment that trailing args after __schema are intentionally ignored.
  • writeSchemaSidecar JSDoc now states the caller must ensure the parent directory exists and is writable.

I left the reservedMetaCommands ordering as-is since the Set-based merge already produces the caller-first-then-__schema shape you flagged, and the test pins it down.

One small ask: the two CI runs on this PR are in action_required state — they need a maintainer to click "Approve and run" on the PR page or under Actions. Once those are green this should be good to merge. 🙏

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants