Add --slurpfile

This commit is contained in:
Nicolas Williams committed 2015-05-29 14:24:33 -05:00
1 parent 8a79f1d0a3
commit 1de56bc5df
2 files changed
+22 -10

No files matched your search

+11 -6
View File
@@ -209,13 +209,18 @@ sections:
predefined variable. If you run jq with `--argjson foo 123`, then predefined variable. If you run jq with `--argjson foo 123`, then
`$foo` is available in the program and has the value `123`. `$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 This option reads all the JSON texts in the named file and binds
value to the jq program as a predefined variable. If you run jq an array of the parsed JSON values to the given global variable.
with `--argfile foo bar`, then `$foo` is available in the If you run jq with `--argfile foo bar`, then `$foo` is available
program and has the value resulting from parsing the content of in the program and has an array whose elements correspond to the
the file named `bar`. 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 - title: Basic filters
entries: entries:
+11 -4
View File
@@ -305,9 +305,15 @@ int main(int argc, char* argv[]) {
i += 2; // skip the next two arguments i += 2; // skip the next two arguments
if (!short_opts) continue; 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) { 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(); die();
} }
jv arg = jv_object(); jv arg = jv_object();
@@ -315,14 +321,15 @@ int main(int argc, char* argv[]) {
jv data = jv_load_file(argv[i+2], 0); jv data = jv_load_file(argv[i+2], 0);
if (!jv_is_valid(data)) { if (!jv_is_valid(data)) {
data = jv_invalid_get_msg(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)); argv[i+1], argv[i+2], jv_string_value(data));
jv_free(data); jv_free(data);
jv_free(arg); jv_free(arg);
ret = 2; ret = 2;
goto out; 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); data = jv_array_get(data, 0);
arg = jv_object_set(arg, jv_string("value"), data); arg = jv_object_set(arg, jv_string("value"), data);
program_arguments = jv_array_append(program_arguments, arg); program_arguments = jv_array_append(program_arguments, arg);