Introspection: Difference between revisions
Content deleted Content added
Frink |
→{{header|jq}}: update, with reference also to gojq |
||
Line 960:
=={{header|jq}}==
'''Works with jq, the C implementation of jq'''
'''Works with gojq, the Go implementation of jq'''
jq --arg version $(jq --version) '$version'▼
jq's support for introspection is limited. For example, jq's version
number is available from the command-line but not as a built-in; and
it is not in general possible to check whether a variable has been
defined without causing an error. However, jq does make it possible
for a program to check whether a variable has been defined globally,
so we'll go with that.
Note that jq's `builtins` function provides details about jq's built-in
functions, and similarly the `modulemeta` function provides some support for
introspection within modules, but otherwise there is no support for
determining the functions that have been user-defined.
Assuming JQ has been defined appropriately as a shell. variable, one could
invoke jq with a global "bloop" variable as follows:
<pre>
$JQ -n --arg version "$($JQ --version)" --argjson bloop -3 -f introspection.jq
</pre>
The following program has been tested with recent versions of both jq
and gojq, but may fail with earlier versions. Note also that
jq's `length` has the semantics of `abs` when applied to numbers,
so if your jq does not support `abs` by that name, you could use `length`.
<syntaxhighlight lang="jq">
def checkVersion:
$version
| capture("^[^- ]*(?<jq>[- ])(?<major>[0-9]*)[.](?<minor>[0-9]*)") // {jq: 0}
| if .jq == 0 then "unrecognized version identification: \($version)" | error
elif .jq == "-" # jq
and (.major < "1" or (.major == "1" and .minor < "5"))
then "version \($version) is too old" | error
elif .jq == " " # gojq
and (.major == "0" and (.minor | tonumber) < 12)
then "version \($version) is too old" | error
else .
end;
checkVersion
# Check that abs/0 is defined
| (builtins | index("abs/0")) as $ix
| if $ix == null then "abs/0 not available" | error else . end
# Is bloop a global variable?
| if ($ARGS.named | has("bloop")) then $ARGS.named["bloop"] | abs
else "There is no globally defined variable name $bloop." | error
end
</syntaxhighlight>
Examples:
{{output}}
<pre>
$ gojq -n --arg version "$(gojq --version)" --argjson bloop -3 -f introspection.jq
3
jq: error (at <unknown>): There is no globally defined variable name bloop.
</pre>
=={{header|Jsish}}==
|