Skip to content
Myr

Clarify -f argument parsing (#3199)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Thalia Archibald · committed by GitHub · parent eb9bdca · Browse files · 3 files+11 added−10 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

3 files+11 added−10 removed

  • other:+11 added−10 removed

3 files+11 added−10 removed

modified:docs/content/manual/dev/manual.yml+6 added-5 removed
219 unmodified lines
220 warned about), discarding all subsequent input until the next221 RS. This mode also parses the output of jq without the `--seq`222 option.223224 * `-f filename` / `--from-file filename`:224 * `-f` / `--from-file`:225226 Read filter from the file rather than from a command line, like227 awk's -f option.226 Read the filter from a file rather than from a command line,227 like awk's -f option. This changes the filter argument to be228 interpreted as a filename, instead of the source of a program.229230 * `-L directory` / `--library-path directory`:231232 Prepend `directory` to the search list for modules. If this
3400 unmodified lines
3633 #!/bin/sh --3634 # total - Output the sum of the given arguments (or stdin)3635 # usage: total [numbers...]3636 # \3636 exec jq --args -MRnf "$0" -- "$@"3637 exec jq --args -MRnf -- "$0" "$@"36383639 $ARGS.positional |3640 reduce (3641 if . == []
11 unmodified lines
3653 But it is not ignored by `sh`, since in `sh` a backslash at the3654 end of the line does not continue the comment.3655 With this trick, when the script is invoked as `total 1 2`,3656 `/bin/sh -- /path/to/total 1 2` will be run, and `sh` will then3656 run `exec jq --args -MRnf /path/to/total -- 1 2` replacing itself3657 run `exec jq --args -MRnf -- /path/to/total 1 2` replacing itself3658 with a `jq` interpreter invoked with the specified options (`-M`,3659 `-R`, `-n`, `--args`), that evaluates the current file (`$0`),3660 with the arguments (`$@`) that were passed to `sh`.3661
163 unmodified lines
modified:src/main.c+1 added-1 removed
92 unmodified lines
93 " --stream parse the input value in streaming fashion;\n"94 " --stream-errors implies --stream and report parse error as\n"95 " an array;\n"96 " --seq parse input/output as application/json-seq;\n"97 " -f, --from-file file load filter from the file;\n"97 " -f, --from-file load the filter from a file;\n"98 " -L, --library-path dir search modules from the directory;\n"99 " --arg name value set $name to the string value;\n"100 " --argjson name value set $name to the JSON value;\n"101 " --slurpfile name file set $name to an array of JSON values read\n"
615 unmodified lines
modified:jq.1.prebuilt+4 added-4 removed
161 unmodified lines
162.IP163Use the \fBapplication/json\-seq\fR MIME type scheme for separating JSON texts in jq\'s input and output\. This means that an ASCII RS (record separator) character is printed before each value on output and an ASCII LF (line feed) is printed after every output\. Input JSON texts that fail to parse are ignored (but warned about), discarding all subsequent input until the next RS\. This mode also parses the output of jq without the \fB\-\-seq\fR option\.164.165.TP166\fB\-f filename\fR / \fB\-\-from\-file filename\fR:166\fB\-f\fR / \fB\-\-from\-file\fR:167.168.IP169Read filter from the file rather than from a command line, like awk\'s \-f option\.169Read the filter from a file rather than from a command line, like awk\'s \-f option\. This changes the filter argument to be interpreted as a filename, instead of the source of a program\.170.171.TP172\fB\-L directory\fR / \fB\-\-library\-path directory\fR:173.
3890 unmodified lines
4064#!/bin/sh \-\-4065# total \- Output the sum of the given arguments (or stdin)4066# usage: total [numbers\.\.\.]4067# \e4068exec jq \-\-args \-MRnf "$0" \-\- "$@"4068exec jq \-\-args \-MRnf \-\- "$0" "$@"40694070$ARGS\.positional |4071reduce (4072 if \. == []
11 unmodified lines
4084.4085.IP "" 04086.4087.P4088The \fBexec\fR line is considered a comment by jq, so it is ignored\. But it is not ignored by \fBsh\fR, since in \fBsh\fR a backslash at the end of the line does not continue the comment\. With this trick, when the script is invoked as \fBtotal 1 2\fR, \fB/bin/sh \-\- /path/to/total 1 2\fR will be run, and \fBsh\fR will then run \fBexec jq \-\-args \-MRnf /path/to/total \-\- 1 2\fR replacing itself with a \fBjq\fR interpreter invoked with the specified options (\fB\-M\fR, \fB\-R\fR, \fB\-n\fR, \fB\-\-args\fR), that evaluates the current file (\fB$0\fR), with the arguments (\fB$@\fR) that were passed to \fBsh\fR\.4088The \fBexec\fR line is considered a comment by jq, so it is ignored\. But it is not ignored by \fBsh\fR, since in \fBsh\fR a backslash at the end of the line does not continue the comment\. With this trick, when the script is invoked as \fBtotal 1 2\fR, \fB/bin/sh \-\- /path/to/total 1 2\fR will be run, and \fBsh\fR will then run \fBexec jq \-\-args \-MRnf \-\- /path/to/total 1 2\fR replacing itself with a \fBjq\fR interpreter invoked with the specified options (\fB\-M\fR, \fB\-R\fR, \fB\-n\fR, \fB\-\-args\fR), that evaluates the current file (\fB$0\fR), with the arguments (\fB$@\fR) that were passed to \fBsh\fR\.4089.4090.SH "MODULES"4091jq has a library/module system\. Modules are files whose names end in \fB\.jq\fR\.4092.
171 unmodified lines