fix: deliver USAGE exit + JSON envelope for bad flags; clean switch output#116
Open
tcerqueira wants to merge 3 commits into
Open
fix: deliver USAGE exit + JSON envelope for bad flags; clean switch output#116tcerqueira wants to merge 3 commits into
tcerqueira wants to merge 3 commits into
Conversation
Run the root command's `parse()` with `.noExit()` and funnel thrown
`ValidationError`s (unknown/invalid/conflicting flags, missing values)
through the CLI error contract so they exit with `ExitCode.USAGE` (2)
and, under `--json`, emit the structured `{error:{code,message,...}}`
envelope on stderr instead of dumping help to stdout.
Previously a bad flag printed full help to stdout and a raw, ANSI-colored
Cliffy error to stderr even under `--json`, corrupting machine-readable
output. `.reset()` repoints the builder to the root command before
`.noExit()`, since mounting/defining subcommands leaves it selecting a
child. `--help`/`--version` still exit 0.
Adds a token-free subprocess test for the exit-code contract and tightens
the existing invalid-flag test to assert USAGE (2) plus a clean stdout.
`switch` printed its confirmation to stdout via console.log and emitted
nothing under `--json`, corrupting piped/machine-readable output. Route
the human-readable message to stderr and, under `--json`, write a single
`{ org, app }` result to stdout via `writeJsonResult` (app is null when
the sandbox variant doesn't resolve an application).
…dler `handleCliError` picked the error output format with a `--json`/`-j` substring-free includes check, missing `--json=...` and combined short flags like `-jy`/`-yj`. A parse-time ValidationError in those invocations printed the human-readable ANSI error instead of the structured VALIDATION_ERROR envelope. Broaden detection to a parse-free heuristic matching `--json`, `--json=...`, `-j`, and combined short flags containing `j`. Adds a `-jy` envelope test case.
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 two CLI-contract rough edges from #112.
1. Bad flags now honor the agent/CI contract
Previously, an unknown/invalid flag dumped the full help text to stdout and a raw, ANSI-colored Cliffy error to stderr — even under
--json— corrupting machine-readable output (the exit code already happened to be 2 because@cliffy/command'sValidationError.exitCodedefaults to 2, but everything around it violated the contract).Now the root
parse()runs with.noExit()and thrownValidationErrors (unknown/invalid/conflicting flags, missing values, and app-thrownValidationErrors re-thrown byactionHandler) are funneled through the CLI error contract:ExitCode.USAGE(2),--json: a single{ "error": { "code": "VALIDATION_ERROR", "message": ... } }envelope on stderr, clean stdout, ANSI disabled,.reset()repoints Cliffy's builder to the root command before.noExit()(mounting/defining subcommands leaves the builder selecting a child, so the setting would otherwise land on the wrong command).--helpand--versionstill exit 0.2.
switchno longer pollutes stdoutswitchprinted its confirmation to stdout viaconsole.logand emitted nothing under--json. It now sends the human message to stderr and, under--json, writes a single{ org, app }result to stdout (app isnullfor the sandbox variant, which doesn't resolve an app).Tests
tests/cli_contract.test.ts: bad flag → 2 (deploy + standalone sandbox roots),--jsonbad flag → 2 with envelope on stderr and clean stdout,--help/--version→ 0.Verified offline: bad flags (plain +
--json),--help,--version, unknown subcommand-as-path, bad option values, and bothswitchoutput modes.