Skip to content

Commit 4ebd21e

Browse files
emanuele6nicowilliams
authored andcommitted
Allow passing the inline jq script before --
jq previously only allowed passing the inline script before -- (as if they were options) even though one would expect the inline script to be a positional argument. Since jq previously also refused to run with a usage error if the script was passed after -- (It was not assuming . as script as it does when no arguments are passed), and positional arguments are allowed before -- and even before other options, it should not be a breaking change to change that weird behaviour, and allow the script to appear after --. It also simplifies the option parsing code a bunch. Fixes jqlang#2918
1 parent 7f54782 commit 4ebd21e

File tree

4 files changed

+17
-25
lines changed

4 files changed

+17
-25
lines changed

docs/content/manual/manual.yml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -313,9 +313,8 @@ sections:
313313
314314
* `--`:
315315
316-
Terminates argument processing. Remaining arguments are
317-
positional, either strings, JSON texts, or input filenames,
318-
according to whether `--args` or `--jsonargs` were given.
316+
Terminates argument processing. Remaining arguments are not
317+
interpreted as options.
319318
320319
* `--run-tests [filename]`:
321320

jq.1.prebuilt

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/main.c

Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -353,8 +353,10 @@ int main(int argc, char* argv[]) {
353353
size_t short_opts = 0;
354354
jv lib_search_paths = jv_null();
355355
for (int i=1; i<argc; i++, short_opts = 0) {
356-
if (args_done) {
357-
if (further_args_are_strings) {
356+
if (args_done || !isoptish(argv[i])) {
357+
if (!program) {
358+
program = argv[i];
359+
} else if (further_args_are_strings) {
358360
ARGS = jv_array_append(ARGS, jv_string(argv[i]));
359361
} else if (further_args_are_json) {
360362
jv v = jv_parse(argv[i]);
@@ -368,26 +370,7 @@ int main(int argc, char* argv[]) {
368370
nfiles++;
369371
}
370372
} else if (!strcmp(argv[i], "--")) {
371-
if (!program) usage(2, 1);
372373
args_done = 1;
373-
} else if (!isoptish(argv[i])) {
374-
if (program) {
375-
if (further_args_are_strings) {
376-
ARGS = jv_array_append(ARGS, jv_string(argv[i]));
377-
} else if (further_args_are_json) {
378-
jv v = jv_parse(argv[i]);
379-
if (!jv_is_valid(v)) {
380-
fprintf(stderr, "%s: invalid JSON text passed to --jsonargs\n", progname);
381-
die();
382-
}
383-
ARGS = jv_array_append(ARGS, v);
384-
} else {
385-
jq_util_input_add_input(input_state, argv[i]);
386-
nfiles++;
387-
}
388-
} else {
389-
program = argv[i];
390-
}
391374
} else {
392375
if (argv[i][1] == 'L') {
393376
if (jv_get_kind(lib_search_paths) == JV_KIND_NULL)

tests/shtest

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -579,4 +579,14 @@ if ( ! $msys && ! $mingw ) && locale -a > /dev/null; then
579579
fi
580580
fi
581581

582+
# Allow passing the inline jq script before -- #2919
583+
if ! r=$($JQ --args -rn -- '$ARGS.positional[0]' bar) || [ "$r" != bar ]; then
584+
echo "passing the inline script after -- didn't work"
585+
exit 1
586+
fi
587+
if ! r=$($JQ --args -rn 1 -- '$ARGS.positional[0]' bar) || [ "$r" != 1 ]; then
588+
echo "passing the inline script before -- didn't work"
589+
exit 1
590+
fi
591+
582592
exit 0

0 commit comments

Comments
 (0)