fix(cli): validate --output uniformly across all command groups#14
Open
Davidson3556 wants to merge 1 commit into
Open
fix(cli): validate --output uniformly across all command groups#14Davidson3556 wants to merge 1 commit into
Davidson3556 wants to merge 1 commit into
Conversation
Only `test` and `project` validated the global `--output` flag. The
`auth`, `usage`, `agent`, and `init` command groups resolved it with
`globals.output ?? 'text'`, so an unrecognised value (e.g. a typo like
`--output josn`) was silently coerced to text mode instead of being
rejected. A coding agent that asked for `--output json` then received a
human-readable text payload and failed to parse it as JSON, with no
signal as to why.
Extract the validation into a shared `resolveOutputMode` helper in
`lib/output.js` and route every command group's `resolveCommonOptions`
through it. Invalid values now throw a typed VALIDATION_ERROR (exit 5)
with an actionable message everywhere. This also unifies the error
wording, which previously differed between `test`
("Flag `--output` is invalid: must be one of: json, text.") and
`project` ("--output must be one of: json, text").
Author
|
@zeshi-du kindly review |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #13
Describe the changes you have made in this PR -
The global
--outputflag was validated inconsistently across command groups.testandprojectrejected an unrecognised value with a typedVALIDATION_ERROR(exit 5), butauth,usage,agent, andinitresolved it withglobals.output ?? 'text'— silently coercing anything that isn'tjson/textto text mode and exiting0.For the CLI's primary consumer (a coding agent), that's a footgun: a request for
--output jsonwith a typo (--output josn) returned a human-readable text payload, so the agent's downstreamJSON.parsefailed with no signal as to why. The two commands that did validate also used different wording.Changes:
resolveOutputMode(raw)helper insrc/lib/output.ts. It returns'text'for an omitted flag and throwslocalValidationError('output', 'must be one of: json, text', ['json', 'text'])for any other value.resolveCommonOptions(auth,usage,agent,init,project,test) through it — closing the validation gap on the four unvalidated groups and unifying the error wording on the canonicaltest-style message.resolveOutputModeand an end-to-end subprocess regression for two previously-unvalidated groups (agent list,auth status).Note: when
--outputitself is the invalid value, the error envelope is rendered in text mode (the requested mode is unusable) — unchanged from the existingtest/projectbehaviour.Demo/Screenshot for feature changes and bug fixes -
Before (main) — invalid value silently coerced to text, exit 0:
After (this branch) — rejected with an actionable message; valid values unaffected:
Tests:

Code Understanding and AI Usage
Did you use AI assistance (ChatGPT, Claude, Copilot, etc.) to write any part of this code?
If you used AI assistance:
Explain your implementation approach:
Problem: the global
--outputflag is the contract every machine consumer depends on, but four of the six command groups silently fell back to text on an unrecognised value instead of failing. An agent that mistyped--output jsongot text back and broke on parse, with no error to point at the cause.Alternatives considered:
.choices(['json','text'])on the root option. Rejected: it would change the error path (Commander's own error rendering / exit codes) rather than the project's typedVALIDATION_ERRORenvelope, and the flag is read in each command viaoptsWithGlobals()— the existingtest/projectcode already validates insideresolveCommonOptions, so matching that is lower-risk and consistent.resolveOutputModehelper and route all six command groups through it. Fixes the gap and unifies wording in one place.Key function:
resolveOutputMode(raw)—undefined→'text'(flag omitted);'json'/'text'→ returned verbatim; anything else throws the typedVALIDATION_ERROR(exit 5) used everywhere else in the CLI. It lives inoutput.ts(no import cycle:output.ts→errors.ts→facade.ts, none of which importoutput.ts).Edge cases handled/tested: omitted flag defaults to text;
json/textpass through;josn/yaml/JSON/Text/empty string all throw exit 5; the error itself renders in text mode because the requested mode is unusable.Checklist before requesting a review
Note: Please check Allow edits from maintainers if you would like us to assist in the PR.