From 1de56bc5df52cf187ea5c27098f360d4b277d89d Mon Sep 17 00:00:00 2001 From: Nicolas Williams Date: Fri, 29 May 2015 14:19:23 -0500 Subject: [PATCH] Add --slurpfile --- docs/content/3.manual/manual.yml | 17 +++++++++++------ main.c | 15 +++++++++++---- 2 files changed, 22 insertions(+), 10 deletions(-) diff --git a/docs/content/3.manual/manual.yml b/docs/content/3.manual/manual.yml index 7fada7ed..03495c06 100644 --- a/docs/content/3.manual/manual.yml +++ b/docs/content/3.manual/manual.yml @@ -209,13 +209,18 @@ sections: predefined variable. If you run jq with `--argjson foo 123`, then `$foo` is available in the program and has the value `123`. - * `--argfile name filename`: + * `--slurpfile variable-name filename`: - This option passes the first value from the named file as a - value to the jq program as a predefined variable. If you run jq - with `--argfile foo bar`, then `$foo` is available in the - program and has the value resulting from parsing the content of - the file named `bar`. + This option reads all the JSON texts in the named file and binds + an array of the parsed JSON values to the given global variable. + If you run jq with `--argfile foo bar`, then `$foo` is available + in the program and has an array whose elements correspond to the + texts in the file named `bar`. + + * `--argfile variable-name filename`: + + Like `--slurpfile`, but uses just the first value in the given + file. - title: Basic filters entries: diff --git a/main.c b/main.c index 6b4d5b92..813ff34d 100644 --- a/main.c +++ b/main.c @@ -305,9 +305,15 @@ int main(int argc, char* argv[]) { i += 2; // skip the next two arguments if (!short_opts) continue; } - if (isoption(argv[i], 0, "argfile", &short_opts)) { + if (isoption(argv[i], 0, "argfile", &short_opts) || + isoption(argv[i], 0, "slurpfile", &short_opts)) { + const char *which; + if (isoption(argv[i], 0, "argfile", &short_opts)) + which = "argfile"; + else + which = "slurpfile"; if (i >= argc - 2) { - fprintf(stderr, "%s: --argfile takes two parameters (e.g. -a varname filename)\n", progname); + fprintf(stderr, "%s: --%s takes two parameters (e.g. -a varname filename)\n", progname, which); die(); } jv arg = jv_object(); @@ -315,14 +321,15 @@ int main(int argc, char* argv[]) { jv data = jv_load_file(argv[i+2], 0); if (!jv_is_valid(data)) { data = jv_invalid_get_msg(data); - fprintf(stderr, "%s: Bad JSON in --argfile %s %s: %s\n", progname, + fprintf(stderr, "%s: Bad JSON in --%s %s %s: %s\n", progname, which, argv[i+1], argv[i+2], jv_string_value(data)); jv_free(data); jv_free(arg); ret = 2; goto out; } - if (jv_get_kind(data) == JV_KIND_ARRAY && jv_array_length(jv_copy(data)) == 1) + if (isoption(argv[i], 0, "argfile", &short_opts) && + jv_get_kind(data) == JV_KIND_ARRAY && jv_array_length(jv_copy(data)) == 1) data = jv_array_get(data, 0); arg = jv_object_set(arg, jv_string("value"), data); program_arguments = jv_array_append(program_arguments, arg);