Skip to content

Commit

Permalink
Allow passing the inline jq script before --
Browse files Browse the repository at this point in the history
It previously was only allowed to pass inline scripts to jq before --
(as if they were options), and not after --; 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.
  • Loading branch information
emanuele6 committed Oct 3, 2023
1 parent 6b5a18f commit ea138b9
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 26 deletions.
5 changes: 2 additions & 3 deletions docs/content/manual/manual.yml
Original file line number Diff line number Diff line change
Expand Up @@ -313,9 +313,8 @@ sections:
* `--`:
Terminates argument processing. Remaining arguments are
positional, either strings, JSON texts, or input filenames,
according to whether `--args` or `--jsonargs` were given.
Terminates argument processing. Remaining arguments are not
intreted as options.
* `--run-tests [filename]`:
Expand Down
4 changes: 2 additions & 2 deletions jq.1.prebuilt

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 4 additions & 21 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -353,8 +353,10 @@ int main(int argc, char* argv[]) {
size_t short_opts = 0;
jv lib_search_paths = jv_null();
for (int i=1; i<argc; i++, short_opts = 0) {
if (args_done) {
if (further_args_are_strings) {
if (args_done || !isoptish(argv[i])) {
if (!program) {
program = argv[i];
} else if (further_args_are_strings) {
ARGS = jv_array_append(ARGS, jv_string(argv[i]));
} else if (further_args_are_json) {
jv v = jv_parse(argv[i]);
Expand All @@ -368,26 +370,7 @@ int main(int argc, char* argv[]) {
nfiles++;
}
} else if (!strcmp(argv[i], "--")) {
if (!program) usage(2, 1);
args_done = 1;
} else if (!isoptish(argv[i])) {
if (program) {
if (further_args_are_strings) {
ARGS = jv_array_append(ARGS, jv_string(argv[i]));
} else if (further_args_are_json) {
jv v = jv_parse(argv[i]);
if (!jv_is_valid(v)) {
fprintf(stderr, "%s: invalid JSON text passed to --jsonargs\n", progname);
die();
}
ARGS = jv_array_append(ARGS, v);
} else {
jq_util_input_add_input(input_state, argv[i]);
nfiles++;
}
} else {
program = argv[i];
}
} else {
if (argv[i][1] == 'L') {
if (jv_get_kind(lib_search_paths) == JV_KIND_NULL)
Expand Down
10 changes: 10 additions & 0 deletions tests/shtest
Original file line number Diff line number Diff line change
Expand Up @@ -579,4 +579,14 @@ if ( ! $msys && ! $mingw ) && locale -a > /dev/null; then
fi
fi

# Allow passing the inline jq script before -- #2919
if ! r=$($JQ --args -rn -- '$ARGS.positional[0]' bar) || [ "$r" != bar ]; then
echo "passing the inline script after -- didn't work"
exit 1
fi
if ! r=$($JQ --args -rn 1 -- '$ARGS.positional[0]' bar) || [ "$r" != 1 ]; then
echo "passing the inline script before -- didn't work"
exit 1
fi

exit 0

0 comments on commit ea138b9

Please sign in to comment.