pyq - the Rust CLI
pyq is the native Rust command-line tool: pyrs-yaml-core directly behind
a jq/yq-style interface, with no Python at runtime. It complements the
Python-based pyrs-yaml CLI; both share the same core, exit-code
and error-message semantics.
Install¶
cargo install --path crates/pyrs-yaml-cli # from a checkout
# or build in-tree:
cargo build -p pyrs-yaml-cli --release # -> target/release/pyq
Query (jq-style)¶
# JSONPath-lite: dot keys, [n], [-n] (python-style), ['key'], [*]
# a missing leading dot is fine, and `.` selects the whole document
$ pyq get '.servers[-1].host' inventory.yaml
web-3
# wildcards expand to ALL matches, streamed like jq: one YAML document
# each (or one JSON value per match with --json)
$ pyq get '.servers[*].port' --json inventory.yaml
8080
8081
# stdin when the file is `-` or omitted; --raw for bare scalars
$ cat services.yaml | pyq get --raw .db.pool.size
20
# JSON output (order-preserving)
$ pyq get '.servers' --json services.yaml
[ { "host": "web-1", "port": 8080 }, ... ]
Filter verbs (jq-style post-processing)¶
Structured flags, not an expression language - applied to the match
stream in this fixed order regardless of flag order on the command line:
select -> sort -> unique -> slice, then join.
pyq get '.servers[*]' --select 'port >= 1000' services.yaml
pyq get '.servers[*]' --sort-by host --desc services.yaml
pyq get '.tags[*]' --unique --skip 2 --take 5 blob.yaml
pyq get '.hosts[*]' --join ',' --raw inventory.yaml # one bare line
| Flag | jq equivalent | Notes |
|---|---|---|
--select 'PATH OP LITERAL' |
select(.PATH OP LITERAL) |
OP == != > >= < <=; literal is YAML; missing path or mixed kinds compare false (no jq total order) |
--sort-by PATH / --desc |
sort_by(.PATH) |
stable; missing key sorts last |
--unique |
unique |
dedup after sorting, like jq |
--first / --last |
.[0] / .[-1] |
mutually exclusive |
--skip N / --take N |
.[N:][…] |
slice the stream |
--join SEP |
join(SEP) |
all-scalar streams only |
Edit (yq-style)¶
# values are YAML expressions (JSON works: YAML is a superset)
pyq set '.db.pool.size' 50 services.yaml # print edited doc
pyq set -i '.db.pool.size' 50 services.yaml # rewrite the file
pyq set --create-missing '.a.b.c' 1 empty.yaml # grow mappings
pyq delete '.legacy_field' -i config.yaml
pyq sort-keys '$' -i config.yaml # sort one mapping level
All edits run through the shared splice engine: untouched lines - comments, blank lines, odd spacing - keep their exact bytes whenever the document's layout is splice-eligible.
Edited documents round-trip through the same serializer as fmt:
comments, anchors and key order survive; the inserted value keeps its
own source style ([1, two] stays flow, "true" stays a quoted string).
Convert¶
pyq fmt k8s.yaml # comment-preserving normalization
pyq fmt --explicit-start cfg.yaml
pyq fmt --indent 4 --width 0 cfg.yaml # block indent 4, no plain-scalar wrap
pyq fmt --sort-keys cfg.yaml # sort every mapping (whole document)
pyq fmt --indent 4 -i cfg.yaml # rewrite the file in place
pyq to-json config.yaml # YAML -> JSON (key order kept)
pyq to-json --jsonc config.yaml # comments carried on the AST re-emitted
pyq to-json --json5 config.yaml # JSON5 spellings ('x', .5, +7, Infinity)
pyq to-toml compose.yaml
pyq from-toml Cargo.toml # TOML -> YAML
pyq from-json package.json # JSON -> YAML
pyq from-ini settings.ini # INI -> YAML (values are strings)
pyq validate k8s.yaml --schema rules.yaml # parse + schema-language rules
pyq frontmatter README.md --body-out body.md # split Markdown front matter
Input format resolves by file extension (.json, .jsonc, .json5,
.toml, .ini); override with
--input yaml|json|jsonc|json5|toml|ini. Since YAML is a JSON superset,
JSON content also parses on the YAML path unchanged. JSONC/JSON5 content
routes through the native dialect engine, so comments and JSON5
spellings ride the AST - pyq to-json --input jsonc --jsonc is a
comment-preserving round-trip.
Diff & merge¶
pyq diff base.yaml head.yaml # semantic diff: exit 0 equal, 1 differs
pyq merge base.yaml overrides.yaml # deep merge, right wins (yq `*+` shape)
pyq merge a.yaml b.yaml --replace-arrays
diff compares resolved values, structure and tags — comments, quoting
and layout are formatting noise and never appear; differing paths print
as - .keep: x / + .c: 4 / ~ .b: 2 -> 3. merge recurses into
mappings, appends sequences (or swaps them wholesale with
--replace-arrays), and emits round-trip YAML.
Exit codes¶
0success;1withpyq: <message>on stderr for missing paths, parse failures or TOML-inexpressible shapes (null values, non-table roots) - the same stable messages the Python API raises.
Shell completion¶
pyq completion bash > /etc/bash_completion.d/pyq # bash
pyq completion zsh > "${functions[@]:0:1}/_pyq" # zsh
pyq completion fish | source # fish
pyq completion powershell > pyq.ps1 # PowerShell
jq / yq Compatibility Shell¶
An argument vector that does not start with one of pyq's own subcommands is read as jq 1.8 or
mikefarah yq v4 and translated into the equivalent subcommand, so scripts written for either tool
run against this binary unchanged. The dialect follows the name the binary was invoked as, so a
jq or yq symlink behaves like the tool it replaces; --as jq|yq states it explicitly.
| Accepted | Meaning |
|---|---|
.a.b, .list[], $.a, [0] |
a path → pyq get (yq's [] is pyq's [*]) |
.a.b = <literal> |
an assignment → pyq set --create-missing |
del(<path>) |
one path → pyq delete |
select(<relpath OP literal>) |
→ pyq get --select, and one trailing \| select pipe |
jq -r, yq -r/--unwrapScalar |
bare scalars (on by default for yq) |
yq -i/--inplace |
rewrite the file in place |
yq -N/--no-doc |
a stream without --- separators (on by default for yq) |
-o json\|yaml, -p json\|yaml\|toml\|ini |
output and input format |
-f PROG, --from-file F |
the expression read from a file, then classified like any other |
Everything the shell cannot answer with the same document the real tool prints is refused by
name, with exit code 2 and empty stdout — never ignored. The set includes jq's --arg/--argjson,
-s, -c, -S, --stream, and any edit expression (pyq writes YAML where jq writes JSON); yq's
-n, -e, -P, --expression, --split-exp, --front-matter, the
--xml-*/--lua-*/--csv-* families and string interpolation; pipe chains other than one
trailing select; sequence slices ([a:b], which both tools accept and this selector does not
address); and every function (keys, length, map, to_entries, env, …).
Two shapes are answered the way the tools answer them: a path the document does not carry prints
null and exits 0, and a select that matches nothing prints nothing and exits 0, where native
pyq get reports both as failures. One cosmetic difference is kept on purpose: an assignment
whose literal is quoted (yq '.a.b = "x"') stays quoted, because pyq preserves the spelling it was
given while yq v4 rewrites it plain; the document re-reads identically.
The accepted table is not a hand-written expectation. crates/pyrs-yaml-cli/tests/
dialect_vs_reference.rs runs the real jq and yq when they are on PATH and compares stdout
byte for byte, and it rejects any reference found inside this workspace, because a probe that finds
the copy of itself proves nothing.
Scope¶
| Capability | pyq | pyrs-yaml CLI (Python) |
|---|---|---|
| Query / set / delete / format / convert | ✅ | ✅ |
Verb post-processing (select/sort/unique/...) |
✅ | — |
| Layout-pinned edits (splice engine) | ✅ | ✅ |
| sort-keys at a path | ✅ | ✅ |
Rename / move / append / insert / frontmatter / validate |
✅ | ✅ |
Multi-document edits (-A on every edit command, to-json -A) |
✅ | ✅ |
Both CLIs edit through the same core plan/splice engine. For streams, pyq -A goes one step further: each document gets an independent splice state, so untouched documents - and every --- separator - keep their exact bytes, and one layout-dirty document falls back alone (to-json -A emits a JSON array). The Python CLI's remaining edge is validation against registered CustomTypes, a Python-layer concept.
See Also¶
- Command-Line Interface — the Python-based
pyrs-yamlcommand - TOML, JSON & INI Formats — the library-side conversion API
- In-Place Editing — the round-trip model
pyqedits through