Skip to content
Myr

Require a main program (fix #2785)

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

authored by Nicolas Williams · committed by Nico Williams · parent 044b385 · Browse files · 7 files+61 added−3 removed
This commit

State

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

Diff stats

7 files+61 added−3 removed

  • tests:+33 added−0 removed
  • other:+28 added−3 removed

7 files+61 added−3 removed

modified:docs/content/manual/manual.yml+13 added-1 removed
79 unmodified lines
80 are passed through the provided filter one at a time. The81 output(s) of the filter are written to standard output, as a82 sequence of newline-separated JSON data.8384 The simplest and most common filter (or jq program) is `.`,85 which is the identity operator, copying the inputs of the jq86 processor to the output stream. Because the default behavior of87 the jq processor is to read JSON texts from the input stream,88 and to pretty-print outputs, the `.` program's main use is to89 validate and pretty-print the inputs. The jq programming90 language is quite rich and allows for much more than just91 validation and pretty-printing.9293 Note: it is important to mind the shell's quoting rules. As a94 general rule it's best to always quote (with single-quote86 characters) the jq program, as too many characters with special95 characters on Unix shells) the jq program, as too many characters with special96 meaning to jq are also shell meta-characters. For example, `jq97 "foo"` will fail on most Unix shells because that will be the same98 as `jq foo`, which will generally fail because `foo is not99 defined`. When using the Windows command shell (cmd.exe) it's
8 unmodified lines
108 * Unix shells: `jq '.["foo"]'`109 * Powershell: `jq '.[\"foo\"]'`110 * Windows command shell: `jq ".[\"foo\"]"`111112 Note: jq allows user-defined functions, but every jq program113 must have a top-level expression.114115 You can affect how jq reads and writes its input and output116 using some command-line options:117118 * `--null-input` / `-n`:
3533 unmodified lines
modified:src/linker.c+6 added
405 unmodified lines
406 nerrors = jq_parse(src, &program);407 if (nerrors)408 return nerrors;409410 if (!block_has_main(program)) {411 jq_report_error(jq, jv_string("jq: error: Top-level program not given (try \".\")"));412 block_free(program);413 return 1;414 }415416 char* home = getenv("HOME");417 if (home) { // silently ignore no $HOME418 /* Import ~/.jq as a library named "" found in $HOME */419 block import = gen_import_meta(gen_import("", NULL, 0),
24 unmodified lines
added:tests/no-main-program.jq+1 added
1def a: .;
modified:tests/shtest+30 added
514 unmodified lines
515 printf '\033[1;30mnull\033[0m\r\n' > $d/expect516 cmp $d/color $d/expect517fi518519# #2785520if $VALGRIND $Q $JQ -n -f "$JQTESTDIR/no-main-program.jq" > $d/out 2>&1; then521 echo "jq -f $JQTESTDIR/no-main-program.jq succeeded"522 exit 1523else524 EC=$?525 if [ $EC -eq 1 ]; then526 echo "jq -f $JQTESTDIR/no-main-program.jq failed with memory errors"527 exit 1528 fi529 if [ $EC -ne 3 ]; then530 echo "jq -f $JQTESTDIR/no-main-program.jq failed with wrong exit code ($EC)"531 exit 1532 fi533fi534cat > $d/expected <<EOF535jq: error: Top-level program not given (try ".")536537jq: 1 compile error538EOF539if ! diff $d/expected $d/out; then540 echo "jq -f $JQTESTDIR/no-main-program.jq failed but its error message is not the expected one"541 exit 1542fi543544if ! $VALGRIND $Q $JQ -n -f "$JQTESTDIR/yes-main-program.jq" > $d/out 2>&1; then545 echo "jq -f $JQTESTDIR/yes-main-program.jq failed"546 exit 1547fi548549exit 0
added:tests/yes-main-program.jq+2 added
1def a: .;20
modified:jq.1.prebuilt+7 added-1 removed
33 unmodified lines
34.SH "INVOKING JQ"35jq filters run on a stream of JSON data\. The input to jq is parsed as a sequence of whitespace\-separated JSON values which are passed through the provided filter one at a time\. The output(s) of the filter are written to standard output, as a sequence of newline\-separated JSON data\.36.37.P38Note: it is important to mind the shell\'s quoting rules\. As a general rule it\'s best to always quote (with single\-quote characters) the jq program, as too many characters with special meaning to jq are also shell meta\-characters\. For example, \fBjq "foo"\fR will fail on most Unix shells because that will be the same as \fBjq foo\fR, which will generally fail because \fBfoo is not defined\fR\. When using the Windows command shell (cmd\.exe) it\'s best to use double quotes around your jq program when given on the command\-line (instead of the \fB\-f program\-file\fR option), but then double\-quotes in the jq program need backslash escaping\. When using the Powershell (\fBpowershell\.exe\fR) or the Powershell Core (\fBpwsh\fR/\fBpwsh\.exe\fR), use single\-quote characters around the jq program and backslash\-escaped double\-quotes (\fB\e"\fR) inside the jq program\.38The simplest and most common filter (or jq program) is \fB\.\fR, which is the identity operator, copying the inputs of the jq processor to the output stream\. Because the default behavior of the jq processor is to read JSON texts from the input stream, and to pretty\-print outputs, the \fB\.\fR program\'s main use is to validate and pretty\-print the inputs\. The jq programming language is quite rich and allows for much more than just validation and pretty\-printing\.39.40.P41Note: it is important to mind the shell\'s quoting rules\. As a general rule it\'s best to always quote (with single\-quote characters on Unix shells) the jq program, as too many characters with special meaning to jq are also shell meta\-characters\. For example, \fBjq "foo"\fR will fail on most Unix shells because that will be the same as \fBjq foo\fR, which will generally fail because \fBfoo is not defined\fR\. When using the Windows command shell (cmd\.exe) it\'s best to use double quotes around your jq program when given on the command\-line (instead of the \fB\-f program\-file\fR option), but then double\-quotes in the jq program need backslash escaping\. When using the Powershell (\fBpowershell\.exe\fR) or the Powershell Core (\fBpwsh\fR/\fBpwsh\.exe\fR), use single\-quote characters around the jq program and backslash\-escaped double\-quotes (\fB\e"\fR) inside the jq program\.42.43.IP "\(bu" 444Unix shells: \fBjq \'\.["foo"]\'\fR45.46.IP "\(bu" 4
4 unmodified lines
51.52.IP "" 053.54.P55Note: jq allows user\-defined functions, but every jq program must have a top\-level expression\.56.57.P58You can affect how jq reads and writes its input and output using some command\-line options:59.60.TP61\fB\-\-null\-input\fR / \fB\-n\fR:
4035 unmodified lines
modified:Makefile.am+2 added-1 removed
220 unmodified lines
221 tests/onig.supp tests/local.supp \222 tests/setup tests/torture/input0.json \223 tests/optional.test tests/man.test tests/manonig.test \224 tests/jq.test tests/onig.test tests/base64.test \225 tests/utf8-truncate.jq tests/jq-f-test.sh225 tests/utf8-truncate.jq tests/jq-f-test.sh \226 tests/no-main-program.jq tests/yes-main-program.jq227228AM_DISTCHECK_CONFIGURE_FLAGS=--with-oniguruma=builtin229230# README.md is expected in GitHub projects, good stuff in it, so we'll
14 unmodified lines