Skip to content

fix(cli): return JSON for parse errors - #101

Open
Kewe63 wants to merge 1 commit into
Polymarket:mainfrom
Kewe63:fix/issue-99-json-clap-errors
Open

Kewe63 wants to merge 1 commit into
Polymarket:mainfrom
Kewe63:fix/issue-99-json-clap-errors

Conversation

@Kewe63

@Kewe63 Kewe63 commented Oct 25, 2026 •

Copy link
Copy Markdown

Summary

  • handle Clap parse and validation failures before they terminate the process
  • emit one JSON error object on stdout when JSON output was requested
  • preserve Clap's normal table-mode, help, version, and exit-code behavior

Problem

Cli::parse() exited before the application error formatter ran. As a result, malformed commands passed with --output json produced plain text on stderr instead of the documented machine-readable JSON response.

Approach

The entrypoint now uses Cli::try_parse_from and detects the requested output format from the raw arguments. Parse failures use the existing JSON error shape when appropriate, while help/version requests and table-mode failures retain standard Clap rendering.

Tests

  • cargo +1.91.1 fmt --check
  • cargo +1.91.1 test --locked --test cli_integration — 54 passed
  • cargo +1.91.1 clippy --locked --all-targets -- -D warnings
  • git diff --check
  • RED/GREEN regression check: the JSON parse-error test fails against the old entrypoint and passes with this change

Risk

Low. The change is limited to initial argument parsing and preserves exit code 2 for invalid CLI input. Help and version output continue to use Clap's native behavior.

Fixes #99


Note

Low Risk
Scope is limited to startup argument parsing and error formatting; help, version, table-mode errors, and exit codes stay aligned with Clap.

Overview
Clap parse/validation failures now honor --output json instead of always printing plain-text errors to stderr before the app runs.

The entrypoint switches from Cli::parse() to Cli::try_parse_from, with a small requested_output_format scan of raw args_os (including -ojson, --output=json, and flags after invalid commands). When JSON was requested, parse errors print a single {"error": "..."} object on stdout with Clap’s exit code (typically 2) and empty stderr; help/version and default table mode still use Clap’s normal rendering.

Integration tests cover unknown commands, flag ordering, short -ojson validation errors, unchanged help with --output json, and preserved table-mode stderr behavior.

Reviewed by Cursor Bugbot for commit 9702ab7. Bugbot is set up for automated code reviews on this repo. Configure here.

@cursor cursor Bot 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.

Cursor Bugbot has reviewed your changes using default effort and found 1 potential issue.

Fix All in Cursor

Reviewed by Cursor Bugbot for commit 9702ab7. Configure here.

Comment thread src/main.rs
.is_some_and(|value| value == "json")
|| arg
.strip_prefix("-o")
.is_some_and(|value| !value.is_empty() && value == "json")

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Equals short output flag ignored

Low Severity

requested_output_format recognizes -ojson and --output=json but not clap's valid short equals form -o=json. Parse failures using that flag still go through clap's plain-text stderr path instead of the JSON error object.

Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit 9702ab7. Configure here.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: JSON output mode does not cover CLI parse errors

1 participant