feat: add --fields flag to filter JSON output#53
feat: add --fields flag to filter JSON output#53qtzx06 wants to merge 4 commits intoPolymarket:mainfrom
Conversation
…ts list the SDK supports these query params but the CLI wasn't wiring them through, forcing users to over-fetch and filter client-side. closes Polymarket#38
agents and scripts consuming CLI output pay per token. a market object has 80+ fields but a typical query only needs 2-3 of them. --fields lets callers specify exactly which keys to keep: polymarket markets list -o json --fields question,volumeNum,slug this filters at the output layer so it works with every command that produces JSON, without touching any individual command logic.
- swap OnceLock for RwLock so --fields can change between interactive shell commands instead of being stuck on first value - move field extraction into run() so the shell path also picks it up (previously only main() extracted fields) - only route through serde_json::to_value when --fields is active, preserving original key order for unfiltered output
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes and found 1 potential issue.
Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.
| if let Ok(mut guard) = JSON_FIELDS.write() { | ||
| *guard = fields; | ||
| } | ||
| } |
There was a problem hiding this comment.
Silent failure in set_json_fields hides broken --fields
Low Severity
set_json_fields silently swallows a RwLock write failure with if let Ok(...), meaning the --fields flag is quietly ignored and the previous filter value persists. Meanwhile, print_json calls .unwrap() on the read lock, which would panic on the same poisoned-lock condition. The writer silently no-ops while the reader crashes — these two call sites need consistent error handling. If the write silently fails, users get unfiltered output with no indication that --fields was ignored.


Problem
The CLI's JSON output includes every field from the API response. A single market object has 80+ fields. When agents or scripts consume this output, they're processing (and paying for) data they don't need.
This matters because LLM-based agents pay per token. Feeding an agent 80 fields per market when it only needs
questionandvolumeNumwastes context window and increases cost for every call.Solution
Adds a global
--fieldsflag that filters JSON output to only the requested keys:The filtering happens at the
print_jsonlayer, so it works with every command that produces JSON output — no per-command changes needed.How it works
--fieldsaccepts a comma-separated list of JSON field names--fieldsis not set, output is unchanged (fully backwards compatible)Implementation
Two files changed, 74 lines added:
src/main.rs— adds--fieldsglobal arg, stores the field list at startup viaOnceLocksrc/output/mod.rs—print_jsonreads the stored fields and filters before printing; includesfilter_fieldshelper with 4 unit testsTest plan
cargo test— 135 tests pass (82 unit + 49 integration + 4 new)cargo clippy -- -D warnings— cleancargo fmt --check— clean--fieldsappears in--helpoutputNote
Medium Risk
Medium risk because it changes CLI argument parsing and request-building for
markets list/events list, which can alter query results and JSON output shape for downstream scripts.Overview
Adds a global
--fieldsflag that filters JSON output only to a specified subset of keys, implemented centrally inoutput::print_jsonvia a per-invocation field list set frommain.Extends
markets listandevents listwith additional filtering flags (volume/liquidity min/max and start/end date ranges; plus market tag id filtering) and wires them into the respective*Request::builder()calls.Written by Cursor Bugbot for commit 8ecbdcf. This will update automatically on new commits. Configure here.