Skip to content
Myr

Merge pull request #133 from nicowilliams/option_argfile-rebase0

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

Add --argfile variant of --arg (issue #117)

authored by Stephen Dolan · committed by Stephen Dolan · parents 983a53a, 03598bb · Browse files · 2 files+46 added−4 removed
This commit

State

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

Diff stats

2 files+46 added−4 removed

  • other:+46 added−4 removed

2 files+46 added−4 removed

modified:docs/content/3.manual/manual.yml+8 added
125 unmodified lines
126 This option passes a value to the jq program as a predefined127 variable. If you run jq with `--arg foo bar`, then `$foo` is128 available in the program and has the value `"bar"`.129130 * `--argfile name filename`:131132 This option passes the first value from the named file as a133 value to the jq program as a predefined variable. If you run jq134 with `--argfile foo bar`, then `$foo` is available in the135 program and has the value resulting from parsing the content of136 the file named `bar`.137138 - title: Basic filters139 entries:140 - title: "`.`"141 body: |
1130 unmodified lines
modified:main.c+38 added-4 removed
90 unmodified lines
91 jv_free(result);92 jq_teardown(&jq);93}9495static jv slurp_file(const char* filename) {95static jv slurp_file(const char* filename, int raw) {96 FILE* file = fopen(filename, "r");97 struct jv_parser parser;98 jv data;99 if (!file) {100 return jv_invalid_with_msg(jv_string_fmt("Could not open %s: %s",101 filename,102 strerror(errno)));103 }104 if (raw) {102 jv data = jv_string("");105 data = jv_string("");106 } else {107 data = jv_array();108 jv_parser_init(&parser);109 }110 while (!feof(file) && !ferror(file)) {111 char buf[4096];112 size_t n = fread(buf, 1, sizeof(buf), file);113 if (raw) {106 data = jv_string_concat(data, jv_string_sized(buf, (int)n));114 data = jv_string_concat(data, jv_string_sized(buf, (int)n));115 } else {116 jv_parser_set_buf(&parser, buf, strlen(buf), !feof(file));117 jv value;118 while (jv_is_valid((value = jv_parser_next(&parser))))119 data = jv_array_append(data, value);120 }121 }122 int badread = ferror(file);123 fclose(file);124 if (badread) {
85 unmodified lines
210 arg = jv_object_set(arg, jv_string("name"), jv_string(argv[i+1]));211 arg = jv_object_set(arg, jv_string("value"), jv_string(argv[i+2]));212 program_arguments = jv_array_append(program_arguments, arg);213 i += 2; // skip the next two arguments214 } else if (isoption(argv[i], 0, "argfile")) {215 if (i >= argc - 2) {216 fprintf(stderr, "%s: --argfile takes two parameters (e.g. -a varname filename)\n", progname);217 die();218 }219 jv arg = jv_object();220 arg = jv_object_set(arg, jv_string("name"), jv_string(argv[i+1]));221 jv data = slurp_file(argv[i+2], 0);222 if (!jv_is_valid(data)) {223 data = jv_invalid_get_msg(data);224 fprintf(stderr, "%s: Bad JSON in --argfile %s %s: %s\n", progname,225 argv[i+1], argv[i+2], jv_string_value(data));226 jv_free(data);227 return 1;228 }229 if (jv_array_length(data) == 1)230 data = jv_array_get(data, 0);231 arg = jv_object_set(arg, jv_string("value"), data);232 program_arguments = jv_array_append(program_arguments, arg);233 i += 2; // skip the next two arguments234 } else if (isoption(argv[i], 0, "debug-dump-disasm")) {235 options |= DUMP_DISASM;236 } else if (isoption(argv[i], 0, "debug-trace")) {237 jq_flags |= JQ_DEBUG_TRACE;
15 unmodified lines
253 die();254 }255 256 if (options & FROM_FILE) {223 jv data = slurp_file(program);257 jv data = slurp_file(program, 1);258 if (!jv_is_valid(data)) {259 data = jv_invalid_get_msg(data);260 fprintf(stderr, "%s: %s\n", progname, jv_string_value(data));261 jv_free(data);
72 unmodified lines