jq icon indicating copy to clipboard operation
jq copied to clipboard

main.c: remove a FIXME

Open emanuele6 opened this issue 1 year ago • 3 comments
trafficstars

Using an "invalid" variable name is fine, it just means you will only be able to access that argument from $ARGS.named

emanuele6 avatar Apr 18 '24 04:04 emanuele6

Huh didn't know. Help says --arg name value set $name to the string value; so maybe we should check?

wader avatar Apr 18 '24 07:04 wader

I don't see the point in restring that; jq -n ... '$ARGS.named' is a fairly common pattern to construct objects from some shell variables; e.g.

jq -n \
    --arg foo "$(something "$baz")" \
    --arg bar "$thisthing" \
    '$ARGS.named'

We would be preventing users from constructing objects with this-cool-key using this method; it is likely that there are scripts that already rely on this.

Maybe the --help description could be extended? (Wouldn't that take too much space?)

The man page entry for --arg (but only the one for --arg even though it also works with --argjson, --slurpfile, etc) mentions that the arguments are also available in the $ARGS.named object, but does not say anything about $name only being assigned if name is a valid identifer

--arg name value:

    This option passes a value to the jq program as a predefined     variable. If you run jq with --arg foo bar, then $foo is available     in the program and has the value "bar". Note that value will be     treated as a string, so --arg foo 123 will bind $foo to "123".

    Named arguments are also available to the jq program as     $ARGS.named.

emanuele6 avatar Apr 18 '24 13:04 emanuele6

Good points, yeap maybe enough to add some note about it in the more detailed arg docs?

wader avatar Apr 18 '24 14:04 wader

We should reopen and merge this. If I were to do it in my PR, I’d make a new commit, rebased to the front of the series, which would be effectively the same as merging this.

thaliaarchi avatar Oct 31 '24 23:10 thaliaarchi

Turns out gojq errors for this:

gojq: compile error: invalid variable name: #name#

jaq does not error and has all named variables in $ARGS.named.

thaliaarchi avatar Nov 01 '24 07:11 thaliaarchi