# Add JSON Output to a Rust CLI

An agent adds a --json flag to zoxide query that prints each match's path and frecency score, with four integration tests; cargo test and clippy pass.

[Run this on Rush](https://cloud.getrush.ai/run/add-json-output/rust-cli/). Opens the console with this prompt ready to send, in a Project that has your repository.

## What the agent does

The agent reads `src/cmd/query.rs`, the `Query` struct in `src/cmd/cmd.rs`, `man/man1/zoxide-query.1`, the existing tests and `CHANGELOG.md` before writing. `query` already chose between interactive, list and single-result output from one `Stream` of matches. `serde` was a dependency; `serde_json` was only in the lockfile through another crate.

It adds a `json` field with `conflicts_with_all = ["interactive", "list", "score"]` and a `query_json` method that collects every match, skips the `--exclude` path, and writes one array of `{path, score}` with `serde_json::to_writer`. The tests in `tests/query_json.rs` run the binary against a real temporary database, and a manual run printed `[{"path":"/tmp/tmp.YckGnxl5nm/x","score":4.0}]`.

## Start it with one API call

`POST https://api.phoenix.so/o/{org}/sessions`

```json
{
  "agent": "claude",
  "input": "A Linear issue labeled agent says: \"Add JSON Output to a Rust CLI. Add a --json flag to zoxide query so it prints each result with its path and score as a JSON array, documented in the man page.\" Do the work in this repository, add tests for it, run cargo test and cargo clippy, and open a pull request that links back to the issue.",
  "resources": [
    {
      "url": "https://github.com/your-org/your-app"
    }
  ]
}
```

1. **Get an API key.** In the console, open Settings, then API keys, and create a key for your org. Send it in the Authorization header as a Bearer token.
2. **Send the request.** POST the body in request.json to https://api.phoenix.so/o/{org}/sessions, with your repository as the resources URL. The run starts in its own cloud sandbox.
3. **Read what came back.** Follow the run on its live stream or wait for the run.completed webhook, then read the result and its trajectory.

## What comes back

Tried on ajeetdsouza/zoxide at f0b3163: 14 files changed, 126 insertions(+), 3 deletions(-), seven of them shell completions under `contrib/completions/` that the crate's `build.rs` regenerated for the new flag. `cargo test` and plain `cargo clippy` passed on the untouched clone; the baseline clippy ran without `--all-targets`. After the change the new suite ended `test result: ok. 4 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.03s`, and `cargo clippy --all-targets` exited 0.

For the reviewer: `--json` always lists every match, even without `--list`, and `score` is the raw frecency float, not clamped or rounded like the `--score` text. `serde_json` becomes a direct dependency. The `nix-dev`-gated `tests/completions.rs` did not run.

## Run it on every Linear issue labeled agent

An automation with a `linear` trigger on `linear.issue.labeled` and the label `agent` runs the `linear-label-to-pr` recipe's prompt when someone in your Rush org labels a request for machine-readable output. An issue that says whether `--json` without `-l` should print only the top match settles the one choice this run made on its own.

## Why run it on Rush?

A new flag on a clap CLI touches more than the command: zoxide's `build.rs` regenerated seven completion scripts, and the run kept them in the diff and named them for the reviewer. It made `--json` conflict with `-i`, `-l` and `-s` rather than print two formats, and `json_conflicts_with_list` tests that refusal.

## Frequently asked questions

### Why does --json list every match?

The run read "each result" in the request as a list. It says printing only the top match unless `-l` is given is a small change if you prefer that.

### Did the build need anything the repository does not provide?

The sandbox had no Rust toolchain or C linker, and the first `cargo test` failed with `error: linker 'cc' not found`. The agent installed Rust with rustup and `build-essential` with apt, then ran the baseline. None of that is in the diff.

## More Rust CLI jobs

- [Generate a Man Page for a Rust CLI With clap_mangen](https://getrush.ai/run/add-man-page/rust-cli/)
- [Add a Minimum Supported Rust Version Check to a Rust CLI](https://getrush.ai/run/add-msrv-check/rust-cli/)
- [Add a Nix Flake to a Rust CLI](https://getrush.ai/run/add-nix-flake/rust-cli/)

## The same job on other stacks

- [Add JSON Output to a Go Command-Line Tool](https://getrush.ai/run/add-json-output/go-cli/)

## More jobs like this: Turn a Linear label into a PR

- [Add JWT Auth to a Hono API from a Linear Issue](https://getrush.ai/run/add-jwt-auth/hono/)
- [Add JWT Authentication to a Vapor App from a Linear Issue](https://getrush.ai/run/add-jwt-auth/vapor/)

## Keep reading

- [How a Rush run works on your repository, from request to pull request](https://getrush.ai/cloud/)
- [Compare cloud agents, agent sandboxes and AI code review](https://getrush.ai/compare/)
- [Rush pricing: Free, Pro and Max](https://getrush.ai/pricing/)
