Repository navigation
feat(mocactl): --help and usage errors show only the command typed - #867
Conversation
`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]>
ingpaolodettori-dev
left a comment
There was a problem hiding this comment.
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]); |
There was a problem hiding this comment.
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' + |
There was a problem hiding this comment.
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.
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]>
Summary
mocactl sbx credentials --help, or a usage error such asmocactl sbx credentials list --jsn, printed the help for everymocactl sbxcommand: about 230 lines when the user wanted one. Both now print only the entries for the command typed:src/core/usage.ts,commandUsage), so the per-command help and the full--helpcannot disagree. Top-levelmocactlcommands work the same way.sbx templates --helplists every templates command, whilesbx templates create --helpshows onlycreate. The optionalcredentials [list]counts as a subcommand. Words that are not subcommands (a prompt, a sandbox id, anything after--) narrow nothing.templates quotaforms, andadmin subjects grant/revoke.typedWords) runs first just to find the command words. It treats unknown flags as booleans and still consumes string-flag values (so--sandbox IDis 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.--helpkeeps it. The footer adds the "ID defaults to the bound sandbox" note only when the command takes a sandbox ID.Test plan
tsc --noEmitpasses, and Prettier passestest/sbx-cli.test.tsandtest/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 usagetest/cli.test.tsplus everytest/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-cliandsbx-proc: 184 passedAssisted-By: Claude Code