Repository navigation
feat(jq): support --arg and --argjson variables - #210
Conversation
9d33fb7 to
a147011
Compare
|
@strandly-the-agent Please review this PR |
strandly-the-agent
left a comment
There was a problem hiding this comment.
Approve. --arg and --argjson match jq 1.8.1 in every case I checked, and I found no bugs.
I ran the full test suite at a147011: 2212 passed, 0 failed. An adversarial pass of about 50 edge cases found nothing caused by this diff. There are two optional nits below. The CI rollup shows FAILURE only because the first run was cancelled when it was re-triggered. In the current run every finished job has passed, and a few macOS jobs are still queued.
✅ What I verified
-
Tests:
cargo test --workspace --all-targetsata147011gives 2212 passed, 0 failed. All 13 new tests depend on the new flags. -
Compared with jq 1.8.1
src/main.c(tagjq-1.8.1):- First binding wins, and a later
--argjsonvalue is never parsed (jv_object_has, lines 466 and 474). So--argjson a 1 --argjson a invalidgives1. - Bad JSON prints
jq: invalid JSON text passed to --argjsonand exits 2 (line 477). - The missing-parameter messages are byte-identical (lines 463 and 471).
--arg ARGS x '$ARGS'leaves$ARGSas the builtin object, withnamed.ARGS = "x", the same as jq.
- First binding wins, and a later
-
Edge cases that held:
- Values starting with a dash:
--arg a -xand--arg a --foobind correctly. - Options can come after the filter.
- Shadowing works with
as $a,def f($a), andreduce. - Invalid
--argjsoninput exits 2:'1 2','',{bad,[1,]. - Deep nesting (5000 levels) errors cleanly with no stack overflow.
- 1000
--argpairs run instantly. - Global-variable order in jaq matches
Ctx::new(compile.rs:836,filter.rs:51-53).
- Values starting with a dash:
-
Not findings, because main already behaves this way:
--argjson a 1e1000is rejected (jq 1.8 accepts it).100000000000000000000001prints as1e+23.$ARGSkeys come out sorted.
In each case
echo <same> | jq .on stdin behaves identically, since both go through serde_json withoutpreserve_order/arbitrary_precision.
Appendix: non-blocking (2)
- ⚪ No differential case backs the jq 1.8.1 parity claim.
tests/differential/is recorded against jq 1.8.1 (README.md:53) and already hasjq_*.shcases. The new tests hardcode their expected output instead. A smalljq_arg.shwould make the parity claim checkable in CI. Good candidates: two variables, first-wins on a duplicate name, an--argjsonobject, and invalid JSON checked by exit code. - ⚪ Help text alignment. The descriptions on the new lines in
src/commands/jq.rs:16-17start at column 28, while the other options start at column 22. For example:Either align all the lines to the wider column or shorten the new ones. This is purely cosmetic.-j, --join-output No newline after each output --arg NAME VALUE Bind $NAME to the string VALUE
Dropped: --arg=a x is accepted, while jq 1.8 rejects it because it matches options with strcmp. Accepting it does no harm.
Pre-existing and not caused by this PR: in debug builds, $a + 1 with --argjson a 9223372036854775807 panics on i64 overflow inside jaq-json (lib.rs:700). echo 9223372036854775807 | jq '. + 1' panics the same way, so the bug belongs to jaq-json, not this PR.
Evidence: strands-agents/shell/pr/210/cargo-test.log, edge-cases.txt, adversarial-log.txt.
|
Thanks for the review. I checked both optional nits. The 13 integration cases already run in CI, and I also compared eight CLI cases against jq 1.8.1. That caught the duplicate I’m leaving the help spacing as-is too. The wider new entries keep both parameter names visible; realigning all the existing options would be formatting-only churn. The current CI run is still finishing its macOS jobs, and every finished job has passed. |
Description
jqrejected--argand--argjson, which agents use all the time to pass shell values into a filter. So something like this just failed:Now
--argbinds a string and--argjsonbinds parsed JSON. Both can be repeated and work in any filter, and$ARGS.namedhas them too. Invalid JSON errors out with exit 2 like real jq, and when a name is repeated the first one wins, same as jq 1.8. No dependency changes. jaq already supports global variables, so this doesn't touch the jaq version (#187).I compared the outputs against jq 1.8.1 and they match. The one difference is that
$ARGSkeys come out sorted, but that's already how this jq prints every object. I added tests for the new flags and the error cases, and the existing jq tests still pass. I left out--args/--jsonargs, so$ARGS.positionalis always empty for now.Related Issues
Part of #207
Documentation PR
N/A
Type of Change
New feature
Testing
How have you tested the change? Verify that the changes do not break functionality or introduce new warnings.
cargo test --workspace --all-targets,pytest tests/python,npm test)cargo fmtandcargo clippyChecklist
By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.