Skip to content

Commit

Permalink
Implement --experimental-fileargs cli option
Browse files Browse the repository at this point in the history
  • Loading branch information
myaaaaaaaaa committed Aug 27, 2024
1 parent 6761779 commit d62042e
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 9 deletions.
35 changes: 26 additions & 9 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -340,8 +340,13 @@ int main(int argc, char* argv[]) {

input_state = jq_util_input_init(NULL, NULL); // XXX add err_cb

int further_args_are_strings = 0;
int further_args_are_json = 0;
enum {
FURTHER_ARGS_ARE_STRINGS = 1001,
FURTHER_ARGS_ARE_JSON = 1002,
FURTHER_ARGS_ARE_FILES = 1003,
};
int further_args = 0;

int args_done = 0;
int jq_flags = 0;
size_t short_opts = 0;
Expand All @@ -350,15 +355,25 @@ int main(int argc, char* argv[]) {
if (args_done || !isoptish(argv[i])) {
if (!program) {
program = argv[i];
} else if (further_args_are_strings) {
} else if (further_args == FURTHER_ARGS_ARE_STRINGS) {
ARGS = jv_array_append(ARGS, jv_string(argv[i]));
} else if (further_args_are_json) {
} else if (further_args == 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 if (further_args == FURTHER_ARGS_ARE_FILES) {
jv data = jv_load_file(argv[i], 0);
if (!jv_is_valid(data)) {
data = jv_invalid_get_msg(data);
fprintf(stderr, "%s: Bad JSON in %s: %s\n", progname, argv[i], jv_string_value(data));
jv_free(data);
ret = JQ_ERROR_SYSTEM;
goto out;
}
program_arguments = jv_object_set(program_arguments, jv_string(argv[i]), data);
} else {
jq_util_input_add_input(input_state, argv[i]);
nfiles++;
Expand Down Expand Up @@ -479,17 +494,19 @@ int main(int argc, char* argv[]) {
options |= EXIT_STATUS;
if (!short_opts) continue;
}
// FIXME: For --arg* we should check that the varname is acceptable
if (isoption(argv[i], 0, "args", &short_opts)) {
further_args_are_strings = 1;
further_args_are_json = 0;
further_args = FURTHER_ARGS_ARE_STRINGS;
continue;
}
if (isoption(argv[i], 0, "jsonargs", &short_opts)) {
further_args_are_strings = 0;
further_args_are_json = 1;
further_args = FURTHER_ARGS_ARE_JSON;
continue;
}
if (isoption(argv[i], 0, "experimental-fileargs", &short_opts)) {
further_args = FURTHER_ARGS_ARE_FILES;
continue;
}
// FIXME: For --arg* we should check that the varname is acceptable
if (isoption(argv[i], 0, "arg", &short_opts)) {
if (i >= argc - 2) {
fprintf(stderr, "%s: --arg takes two parameters (e.g. --arg varname value)\n", progname);
Expand Down
9 changes: 9 additions & 0 deletions tests/shtest
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,15 @@ f.json
EOF
cmp $d/out $d/expected

# fileargs
(cd $d; JQ_ENABLE_SNAPSHOT=1 $VALGRIND $Q $JQ -n 'range(3) | _experimental_snapshot(tostring+"-ff.json"; .*2) | empty')
(cd $d; JQ_ENABLE_SNAPSHOT=1 $VALGRIND $Q $JQ -n --experimental-fileargs '$ARGS.named | add(keys[]),add(.[][])' ?-ff.json >out)
cat > $d/expected <<'EOF'
"0-ff.json1-ff.json2-ff.json"
6
EOF
cmp $d/out $d/expected

# debug, stderr
$VALGRIND $Q $JQ -n '"test", {} | debug, stderr' >/dev/null
$JQ -n -c -j '"hello\nworld", null, [false, 0], {"foo":["bar"]}, "\n" | stderr' >$d/out 2>$d/err
Expand Down

0 comments on commit d62042e

Please sign in to comment.