Skip to content
Myr

Parse short options in order given

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

Fixes: * Short options cannot be repeated in the same group: e.g., `-nn` fails, but `--null-input --null-input` works * Short options are processed in an arbitrary fixed order, not the order given by the user: e.g., `-hV`, `-Vh`, `-h -V`, and `--help --version` are processed as `-h`, but `-V -h` and `--version --help` are processed as `-V` * `-L` cannot be a part of a short option group (as the last one): e.g., `-nLlib` and `-nL lib` fail

authored by Thalia Archibald · committed by Emanuele Torre · parent 562d5c5 · Browse files · 2 files+193 added−225 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+193 added−225 removed

  • tests:+22 added−0 removed
  • other:+171 added−225 removed

2 files+193 added−225 removed

modified:src/main.c+171 added-225 removed
127 unmodified lines
128static int isoptish(const char* text) {129 return text[0] == '-' && (text[1] == '-' || isalpha((unsigned char)text[1]));130}131132static int isoption(const char* text, char shortopt, const char* longopt, size_t *short_opts) {133 if (text[0] != '-' || text[1] == '-')134 *short_opts = 0;135 if (text[0] != '-') return 0;136137 // check long option138 if (text[1] == '-' && !strcmp(text+2, longopt)) return 1;139 else if (text[1] == '-') return 0;140141 // must be short option; check it and...142 if (!shortopt) return 0;143 if (strchr(text, shortopt) != NULL) {144 (*short_opts)++; // ...count it (for option stacking)145 return 1;132static int isoption(const char** text, char shortopt, const char* longopt, int is_short) {133 if (is_short) {134 if (shortopt && **text == shortopt) {135 (*text)++;136 if (!**text) *text = NULL;137 return 1;138 }139 } else {140 if (longopt != NULL && !strcmp(*text, longopt)) {141 *text = NULL;142 return 1;143 }144 }145 return 0;146}147
193 unmodified lines
341 int further_args_are_strings = 0;342 int further_args_are_json = 0;343 int args_done = 0;344 int jq_flags = 0;347 size_t short_opts = 0;345 jv lib_search_paths = jv_null();349 for (int i=1; i<argc; i++, short_opts = 0) {346 for (int i=1; i<argc; i++) {347 if (args_done || !isoptish(argv[i])) {348 if (!program) {349 program = argv[i];350 } else if (further_args_are_strings) {
11 unmodified lines
362 }363 } else if (!strcmp(argv[i], "--")) {364 args_done = 1;365 } else {369 if (argv[i][1] == 'L') {370 if (jv_get_kind(lib_search_paths) == JV_KIND_NULL)371 lib_search_paths = jv_array();372 if (argv[i][2] != 0) { // -Lname (faster check than strlen)373 lib_search_paths = jv_array_append(lib_search_paths, jq_realpath(jv_string(argv[i]+2)));374 } else if (i >= argc - 1) {375 fprintf(stderr, "-L takes a parameter: (e.g. -L /search/path or -L/search/path)\n");376 die();377 } else {378 lib_search_paths = jv_array_append(lib_search_paths, jq_realpath(jv_string(argv[i+1])));379 i++;380 }381 continue;366 const char* text = argv[i];367 int is_short;368 // First '-' already checked by isoptish369 if (text[1] == '-') {370 text += 2;371 is_short = 0;372 } else {373 text++;374 is_short = 1;375 }376 int raw; // Temporary for --rawfile377384 if (isoption(argv[i], 's', "slurp", &short_opts)) {385 options |= SLURP;386 if (!short_opts) continue;387 }388 if (isoption(argv[i], 'r', "raw-output", &short_opts)) {389 options |= RAW_OUTPUT;390 if (!short_opts) continue;391 }392 if (isoption(argv[i], 0, "raw-output0", &short_opts)) {393 options |= RAW_OUTPUT | RAW_NO_LF | RAW_OUTPUT0;394 if (!short_opts) continue;395 }396 if (isoption(argv[i], 'j', "join-output", &short_opts)) {397 options |= RAW_OUTPUT | RAW_NO_LF;398 if (!short_opts) continue;399 }400 if (isoption(argv[i], 'c', "compact-output", &short_opts)) {401 dumpopts &= ~(JV_PRINT_TAB | JV_PRINT_INDENT_FLAGS(7));402 if (!short_opts) continue;403 }404 if (isoption(argv[i], 'C', "color-output", &short_opts)) {405 options |= COLOR_OUTPUT;406 if (!short_opts) continue;407 }408 if (isoption(argv[i], 'M', "monochrome-output", &short_opts)) {409 options |= NO_COLOR_OUTPUT;410 if (!short_opts) continue;411 }412 if (isoption(argv[i], 'a', "ascii-output", &short_opts)) {413 options |= ASCII_OUTPUT;414 if (!short_opts) continue;415 }416 if (isoption(argv[i], 0, "unbuffered", &short_opts)) {417 options |= UNBUFFERED_OUTPUT;418 continue;419 }420 if (isoption(argv[i], 'S', "sort-keys", &short_opts)) {421 options |= SORTED_OUTPUT;422 if (!short_opts) continue;423 }424 if (isoption(argv[i], 'R', "raw-input", &short_opts)) {425 options |= RAW_INPUT;426 if (!short_opts) continue;427 }428 if (isoption(argv[i], 'n', "null-input", &short_opts)) {429 options |= PROVIDE_NULL;430 if (!short_opts) continue;431 }432 if (isoption(argv[i], 'f', "from-file", &short_opts)) {433 options |= FROM_FILE;434 if (!short_opts) continue;435 }436 if (isoption(argv[i], 'b', "binary", &short_opts)) {378 // Parse a long option in one iteration or iterate over short options379 while (text != NULL) {380 if (isoption(&text, 's', "slurp", is_short)) {381 options |= SLURP;382 } else if (isoption(&text, 'r', "raw-output", is_short)) {383 options |= RAW_OUTPUT;384 } else if (isoption(&text, 0, "raw-output0", is_short)) {385 options |= RAW_OUTPUT | RAW_NO_LF | RAW_OUTPUT0;386 } else if (isoption(&text, 'j', "join-output", is_short)) {387 options |= RAW_OUTPUT | RAW_NO_LF;388 } else if (isoption(&text, 'c', "compact-output", is_short)) {389 dumpopts &= ~(JV_PRINT_TAB | JV_PRINT_INDENT_FLAGS(7));390 } else if (isoption(&text, 'C', "color-output", is_short)) {391 options |= COLOR_OUTPUT;392 } else if (isoption(&text, 'M', "monochrome-output", is_short)) {393 options |= NO_COLOR_OUTPUT;394 } else if (isoption(&text, 'a', "ascii-output", is_short)) {395 options |= ASCII_OUTPUT;396 } else if (isoption(&text, 0, "unbuffered", is_short)) {397 options |= UNBUFFERED_OUTPUT;398 } else if (isoption(&text, 'S', "sort-keys", is_short)) {399 options |= SORTED_OUTPUT;400 } else if (isoption(&text, 'R', "raw-input", is_short)) {401 options |= RAW_INPUT;402 } else if (isoption(&text, 'n', "null-input", is_short)) {403 options |= PROVIDE_NULL;404 } else if (isoption(&text, 'f', "from-file", is_short)) {405 options |= FROM_FILE;406 } else if (isoption(&text, 'L', NULL, is_short)) {407 if (jv_get_kind(lib_search_paths) == JV_KIND_NULL)408 lib_search_paths = jv_array();409 if (text != NULL) { // -Lname410 lib_search_paths = jv_array_append(lib_search_paths, jq_realpath(jv_string(text)));411 text = NULL;412 } else if (i >= argc - 1) {413 fprintf(stderr, "-L takes a parameter: (e.g. -L /search/path or -L/search/path)\n");414 die();415 } else {416 lib_search_paths = jv_array_append(lib_search_paths, jq_realpath(jv_string(argv[i+1])));417 i++;418 }419 } else if (isoption(&text, 'b', "binary", is_short)) {420#ifdef WIN32438 fflush(stdout);439 fflush(stderr);440 _setmode(fileno(stdin), _O_BINARY);441 _setmode(fileno(stdout), _O_BINARY);442 _setmode(fileno(stderr), _O_BINARY);421 fflush(stdout);422 fflush(stderr);423 _setmode(fileno(stdin), _O_BINARY);424 _setmode(fileno(stdout), _O_BINARY);425 _setmode(fileno(stderr), _O_BINARY);426#endif444 if (!short_opts) continue;445 }446 if (isoption(argv[i], 0, "tab", &short_opts)) {447 dumpopts &= ~JV_PRINT_INDENT_FLAGS(7);448 dumpopts |= JV_PRINT_TAB | JV_PRINT_PRETTY;449 continue;450 }451 if (isoption(argv[i], 0, "indent", &short_opts)) {452 if (i >= argc - 1) {453 fprintf(stderr, "%s: --indent takes one parameter\n", progname);454 die();455 }456 dumpopts &= ~(JV_PRINT_TAB | JV_PRINT_INDENT_FLAGS(7));457 int indent = atoi(argv[i+1]);458 if (indent < -1 || indent > 7) {459 fprintf(stderr, "%s: --indent takes a number between -1 and 7\n", progname);460 die();461 }462 dumpopts |= JV_PRINT_INDENT_FLAGS(indent);463 i++;464 continue;465 }466 if (isoption(argv[i], 0, "seq", &short_opts)) {467 options |= SEQ;468 continue;469 }470 if (isoption(argv[i], 0, "stream", &short_opts)) {471 parser_flags |= JV_PARSE_STREAMING;472 continue;473 }474 if (isoption(argv[i], 0, "stream-errors", &short_opts)) {475 parser_flags |= JV_PARSE_STREAMING | JV_PARSE_STREAM_ERRORS;476 continue;477 }478 if (isoption(argv[i], 'e', "exit-status", &short_opts)) {479 options |= EXIT_STATUS;480 if (!short_opts) continue;481 }482 // FIXME: For --arg* we should check that the varname is acceptable483 if (isoption(argv[i], 0, "args", &short_opts)) {484 further_args_are_strings = 1;485 further_args_are_json = 0;486 continue;487 }488 if (isoption(argv[i], 0, "jsonargs", &short_opts)) {489 further_args_are_strings = 0;490 further_args_are_json = 1;491 continue;492 }493 if (isoption(argv[i], 0, "arg", &short_opts)) {494 if (i >= argc - 2) {495 fprintf(stderr, "%s: --arg takes two parameters (e.g. --arg varname value)\n", progname);496 die();497 }498 if (!jv_object_has(jv_copy(program_arguments), jv_string(argv[i+1])))499 program_arguments = jv_object_set(program_arguments, jv_string(argv[i+1]), jv_string(argv[i+2]));500 i += 2; // skip the next two arguments501 continue;502 }503 if (isoption(argv[i], 0, "argjson", &short_opts)) {504 if (i >= argc - 2) {505 fprintf(stderr, "%s: --argjson takes two parameters (e.g. --argjson varname text)\n", progname);506 die();507 }508 if (!jv_object_has(jv_copy(program_arguments), jv_string(argv[i+1]))) {509 jv v = jv_parse(argv[i+2]);510 if (!jv_is_valid(v)) {511 fprintf(stderr, "%s: invalid JSON text passed to --argjson\n", progname);427 } else if (isoption(&text, 0, "tab", is_short)) {428 dumpopts &= ~JV_PRINT_INDENT_FLAGS(7);429 dumpopts |= JV_PRINT_TAB | JV_PRINT_PRETTY;430 } else if (isoption(&text, 0, "indent", is_short)) {431 if (i >= argc - 1) {432 fprintf(stderr, "%s: --indent takes one parameter\n", progname);433 die();434 }514 program_arguments = jv_object_set(program_arguments, jv_string(argv[i+1]), v);515 }516 i += 2; // skip the next two arguments517 continue;518 }519 if (isoption(argv[i], 0, "rawfile", &short_opts) ||520 isoption(argv[i], 0, "slurpfile", &short_opts)) {521 int raw = isoption(argv[i], 0, "rawfile", &short_opts);522 const char *which;523 if (raw)524 which = "rawfile";525 else526 which = "slurpfile";527 if (i >= argc - 2) {528 fprintf(stderr, "%s: --%s takes two parameters (e.g. --%s varname filename)\n", progname, which, which);435 dumpopts &= ~(JV_PRINT_TAB | JV_PRINT_INDENT_FLAGS(7));436 int indent = atoi(argv[i+1]);437 if (indent < -1 || indent > 7) {438 fprintf(stderr, "%s: --indent takes a number between -1 and 7\n", progname);439 die();440 }441 dumpopts |= JV_PRINT_INDENT_FLAGS(indent);442 i++;443 } else if (isoption(&text, 0, "seq", is_short)) {444 options |= SEQ;445 } else if (isoption(&text, 0, "stream", is_short)) {446 parser_flags |= JV_PARSE_STREAMING;447 } else if (isoption(&text, 0, "stream-errors", is_short)) {448 parser_flags |= JV_PARSE_STREAMING | JV_PARSE_STREAM_ERRORS;449 } else if (isoption(&text, 'e', "exit-status", is_short)) {450 options |= EXIT_STATUS;451 } else if (isoption(&text, 0, "args", is_short)) {452 further_args_are_strings = 1;453 further_args_are_json = 0;454 } else if (isoption(&text, 0, "jsonargs", is_short)) {455 further_args_are_strings = 0;456 further_args_are_json = 1;457 } else if (isoption(&text, 0, "arg", is_short)) {458 // FIXME: For --arg* we should check that the varname is acceptable459 if (i >= argc - 2) {460 fprintf(stderr, "%s: --arg takes two parameters (e.g. --arg varname value)\n", progname);461 die();462 }463 if (!jv_object_has(jv_copy(program_arguments), jv_string(argv[i+1])))464 program_arguments = jv_object_set(program_arguments, jv_string(argv[i+1]), jv_string(argv[i+2]));465 i += 2; // skip the next two arguments466 } else if (isoption(&text, 0, "argjson", is_short)) {467 if (i >= argc - 2) {468 fprintf(stderr, "%s: --argjson takes two parameters (e.g. --argjson varname text)\n", progname);469 die();470 }471 if (!jv_object_has(jv_copy(program_arguments), jv_string(argv[i+1]))) {472 jv v = jv_parse(argv[i+2]);473 if (!jv_is_valid(v)) {474 fprintf(stderr, "%s: invalid JSON text passed to --argjson\n", progname);475 die();476 }477 program_arguments = jv_object_set(program_arguments, jv_string(argv[i+1]), v);478 }479 i += 2; // skip the next two arguments480 } else if ((raw = isoption(&text, 0, "rawfile", is_short)) ||481 isoption(&text, 0, "slurpfile", is_short)) {482 const char *which = raw ? "rawfile" : "slurpfile";483 if (i >= argc - 2) {484 fprintf(stderr, "%s: --%s takes two parameters (e.g. --%s varname filename)\n", progname, which, which);485 die();486 }487 if (!jv_object_has(jv_copy(program_arguments), jv_string(argv[i+1]))) {488 jv data = jv_load_file(argv[i+2], raw);489 if (!jv_is_valid(data)) {490 data = jv_invalid_get_msg(data);491 fprintf(stderr, "%s: Bad JSON in --%s %s %s: %s\n", progname, which,492 argv[i+1], argv[i+2], jv_string_value(data));493 jv_free(data);494 ret = JQ_ERROR_SYSTEM;495 goto out;496 }497 program_arguments = jv_object_set(program_arguments, jv_string(argv[i+1]), data);498 }499 i += 2; // skip the next two arguments500 } else if (isoption(&text, 0, "debug-dump-disasm", is_short)) {501 options |= DUMP_DISASM;502 } else if (isoption(&text, 0, "debug-trace=all", is_short)) {503 jq_flags |= JQ_DEBUG_TRACE_ALL;504 } else if (isoption(&text, 0, "debug-trace", is_short)) {505 jq_flags |= JQ_DEBUG_TRACE;506 } else if (isoption(&text, 'h', "help", is_short)) {507 usage(0, 0);508 } else if (isoption(&text, 'V', "version", is_short)) {509 printf("jq-%s\n", JQ_VERSION);510 ret = JQ_OK;511 goto out;512 } else if (isoption(&text, 0, "build-configuration", is_short)) {513 printf("%s\n", JQ_CONFIG);514 ret = JQ_OK;515 goto out;516 } else if (isoption(&text, 0, "run-tests", is_short)) {517 i++;518 // XXX Pass program_arguments, even a whole jq_state *, through;519 // could be useful for testing520 ret = jq_testsuite(lib_search_paths,521 (options & DUMP_DISASM) || (jq_flags & JQ_DEBUG_TRACE),522 argc - i, argv + i);523 goto out;524 } else {525 if (is_short) {526 fprintf(stderr, "%s: Unknown option -%c\n", progname, text[0]);527 } else {528 fprintf(stderr, "%s: Unknown option --%s\n", progname, text);529 }530 die();531 }531 if (!jv_object_has(jv_copy(program_arguments), jv_string(argv[i+1]))) {532 jv data = jv_load_file(argv[i+2], raw);533 if (!jv_is_valid(data)) {534 data = jv_invalid_get_msg(data);535 fprintf(stderr, "%s: Bad JSON in --%s %s %s: %s\n", progname, which,536 argv[i+1], argv[i+2], jv_string_value(data));537 jv_free(data);538 ret = JQ_ERROR_SYSTEM;539 goto out;540 }541 program_arguments = jv_object_set(program_arguments, jv_string(argv[i+1]), data);542 }543 i += 2; // skip the next two arguments544 continue;532 }546 if (isoption(argv[i], 0, "debug-dump-disasm", &short_opts)) {547 options |= DUMP_DISASM;548 continue;549 }550 if (isoption(argv[i], 0, "debug-trace=all", &short_opts)) {551 jq_flags |= JQ_DEBUG_TRACE_ALL;552 if (!short_opts) continue;553 }554 if (isoption(argv[i], 0, "debug-trace", &short_opts)) {555 jq_flags |= JQ_DEBUG_TRACE;556 continue;557 }558 if (isoption(argv[i], 'h', "help", &short_opts)) {559 usage(0, 0);560 if (!short_opts) continue;561 }562 if (isoption(argv[i], 'V', "version", &short_opts)) {563 printf("jq-%s\n", JQ_VERSION);564 ret = JQ_OK;565 goto out;566 }567 if (isoption(argv[i], 0, "build-configuration", &short_opts)) {568 printf("%s\n", JQ_CONFIG);569 ret = JQ_OK;570 goto out;571 }572 if (isoption(argv[i], 0, "run-tests", &short_opts)) {573 i++;574 // XXX Pass program_arguments, even a whole jq_state *, through;575 // could be useful for testing576 ret = jq_testsuite(lib_search_paths,577 (options & DUMP_DISASM) || (jq_flags & JQ_DEBUG_TRACE),578 argc - i, argv + i);579 goto out;580 }581582 // check for unknown options... if this argument was a short option583 if (strlen(argv[i]) != short_opts + 1) {584 fprintf(stderr, "%s: Unknown option %s\n", progname, argv[i]);585 die();586 }533 }534 }535536#ifdef USE_ISATTY
182 unmodified lines
modified:tests/shtest+22 added
208 unmodified lines
209else210 cmp $d/out $d/expected211fi212213# Regression tests for #3194214echo 42 > $d/expected215$JQ -nn 42 > $d/out 2>&1216cmp $d/out $d/expected217218$JQ -nL. 42 > $d/out 2>&1219cmp $d/out $d/expected220$JQ -nL . 42 > $d/out 2>&1221cmp $d/out $d/expected222223$JQ -h > $d/expected 2>&1224$JQ -hV > $d/out 2>&1225cmp $d/out $d/expected226$JQ -h -V > $d/out 2>&1227cmp $d/out $d/expected228229$JQ -V > $d/expected 2>&1230$JQ -Vh > $d/out 2>&1231cmp $d/out $d/expected232$JQ -V -h > $d/out 2>&1233cmp $d/out $d/expected234235## Test streaming parser236237## If we add an option to stream to the `import ... as $symbol;` directive238## then we can move these tests into tests/all.test.
487 unmodified lines