Skip to content

feat(mocactl): --help and usage errors show only the command typed - #867

Merged
esnible merged 1 commit into
rossoctl:mainfrom
esnible:feat/mocactl-command-help
Nov 17, 2026
Merged

esnible merged 1 commit into
rossoctl:mainfrom
esnible:feat/mocactl-command-help

Conversation

@esnible

@esnible esnible commented Nov 17, 2026 •

Copy link
Copy Markdown
Member

Summary

mocactl sbx credentials --help, or a usage error such as mocactl sbx credentials list --jsn, printed the help for every mocactl sbx command: about 230 lines when the user wanted one. Both now print only the entries for the command typed:

$ mocactl sbx credentials list --jsn
Unknown option '--jsn'. ...
usage:
  mocactl sbx credentials [list] [--json]       the credentials the host swaps into your sandboxes' HTTPS requests
Run `mocactl sbx --help` for the list of all commands.
  • One source. Each command's help is cut from the existing usage text (src/core/usage.ts, commandUsage), so the per-command help and the full --help cannot disagree. Top-level mocactl commands work the same way.
  • Subcommands narrow it. sbx templates --help lists every templates command, while sbx templates create --help shows only create. The optional credentials [list] counts as a subcommand. Words that are not subcommands (a prompt, a sandbox id, anything after --) narrow nothing.
  • Shared descriptions stay together. Forms that share one description stay together: the two templates quota forms, and admin subjects grant/revoke.
  • Usage errors. About 80 error sites now show the failed command's help. An unknown flag makes the normal parse fail before the command is known, so a lenient parse (typedWords) runs first just to find the command words. It treats unknown flags as booleans and still consumes string-flag values (so --sandbox ID is not taken for a word). Each CLI's flag list moved into one shared constant (SBX_OPTIONS, OPTIONS) so both parses read the same flags.
  • Footer. An error leaves out the list of exit codes, since the user just got one; per-command --help keeps it. The footer adds the "ID defaults to the bound sandbox" note only when the command takes a sandbox ID.
  • Unchanged. With no command or an unknown one, the full usage is shown as before.

Test plan

  • tsc --noEmit passes, and Prettier passes
  • New tests in test/sbx-cli.test.ts and test/cli.test.ts: per-command help, subcommand narrowing (templates create, proc logs, credentials list), shared-description forms, ignored words (rm ID, run -- CMD), every sbx command having its own help, usage errors after both a failed and a successful parse, and an unknown command still getting the full usage
  • test/cli.test.ts plus every test/sbx-* file: 953 passed (with --testTimeout=60000, as this machine is loaded), before the last change; after it (dropping exit codes from errors), cli, sbx-cli and sbx-proc: 184 passed

Assisted-By: Claude Code

`mocactl sbx credentials --help` (or a usage error such as
`mocactl sbx credentials list --jsn`) printed the help for every sbx
command. Both now print only the entries for the command and subcommand
typed, cut from the same usage text as the full --help (src/core/usage.ts),
so the two cannot disagree. The same applies to top-level mocactl commands.

- A subcommand narrows it: `templates create`, `proc logs`, and the
  optional `credentials [list]`. Words that are not subcommands (a
  prompt, an id, anything after `--`) do not.
- Forms that share one description stay together (`templates quota`).
- A usage error finds its command with a lenient parse (an unknown flag
  is taken as a boolean), and leaves out the list of exit codes.
- No command, or an unknown one, still shows the whole usage.

Assisted-By: Claude (Anthropic AI) <[email protected]>
Signed-off-by: Ed Snible <[email protected]>
@esnible
esnible requested a review from pdettori as a code owner November 17, 2026 19:17
@pdettori pdettori added the ready-for-ai-review Request automated AI code review from clawgenti label Nov 17, 2026

@ingpaolodettori-dev ingpaolodettori-dev left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Review at c4b5e18

Adds src/core/usage.ts so mocactl COMMAND [SUB] --help and usage errors print only that command's entries, cut from the existing USAGE/SBX_USAGE text, with shared flag tables and a lenient parse that finds the command words even when an unknown flag makes the strict parse fail. I checked the grouping heuristic by hand against every head line of both usage texts. It handles the cases the PR describes, with one mis-grouping at top level (inline). No secrets, dependency or workflow changes.

Author: esnible (MEMBER — maintainer)
Areas reviewed: TypeScript CLI (mocactl), tests, docs accuracy
Agent/IDE config (.claude/.vscode): none
Commits: 1 commit, all signed-off: yes
CI status: all checks passing at submit time

suggestion (outside the diff): two docs now state the old behaviour. docs/reference/cli-mocactl.md:15 says "--help is not per command: mocactl run --help prints the same global usage", and docs/reference/change-policy.md:31 says "--help is global". Please update them; line 19 there ("An unknown flag ... prints the usage") could also say it is now the command's own usage.

Drafted by an AI reviewer and approved by a human before posting.

if (!line.startsWith(' ')) break;
if (line.startsWith(head)) {
const words = headWords(line.slice(head.length));
const shares = cur && !cur.described && cur.heads.some((h) => h[0] === words[0]);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

suggestion: shares compares only the first head word. In USAGE, mocactl workloads get ID [--json] has no description and is followed by workloads delete ID [--json] delete a workload and its context, so they merge into one entry. mocactl workloads get --help (and any workloads get usage error) then prints delete's line too, and its description reads as if it described get. The simplest fix is to give workloads get its own description in USAGE (e.g. the README's "status, context type, size and frozen revision"). The other option is to tighten the sharing rule while keeping templates quota and admin subjects grant/revoke together.

: undefined,
opts.forError
? undefined
: 'exit codes: 0 ok, 1 failed, 2 usage error, 3 not logged in, 4 control plane unreachable,\n' +

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

nit: This exit-code list is a hand-written copy whose wording differs from SBX_USAGE's exit: line ("2 usage" / "3 log in first" there vs "2 usage error" / "3 not logged in" here). That goes against the "one source, cannot disagree" aim. Consider cutting it from SBX_USAGE, as usageFor does with flags for every command.

@esnible
esnible merged commit 6642560 into rossoctl:main Nov 17, 2026
18 checks passed
@esnible
esnible deleted the feat/mocactl-command-help branch November 17, 2026 20:05
webchang added a commit to webchang/moca that referenced this pull request Nov 17, 2026
rossoctl#867 cut COMMAND --help from the usage text. Keep both: bare --help
opens with what an sbx sandbox is, COMMAND --help shows that command's
entries, and usageEntries now skips the group headings rossoctl#599 added
(a line ending in ':' before a head, and the blank line before it),
so they neither end the entries nor join one.

Assisted-By: Claude Code
Signed-off-by: Rong Chang <[email protected]>
@pdettori pdettori added this to the v0.11.0 milestone Nov 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ready-for-ai-review Request automated AI code review from clawgenti

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants