fix usage in help to use jq not the invoked command path (#3299)

This commit is contained in:
itchyny authored and GitHub committed 2025-03-28 08:43:06 +09:00
1 parent 8819eb8fb4
commit 5f7b1aed16
1 file changed
+17 -26
+17 -26
View File
@@ -30,16 +30,12 @@ extern void jv_tsd_dtoa_ctx_init();
#define USE_ISATTY #define USE_ISATTY
#endif #endif
#include "compile.h"
#include "jv.h" #include "jv.h"
#include "jq.h" #include "jq.h"
#include "jv_alloc.h"
#include "util.h" #include "util.h"
int jq_testsuite(jv lib_dirs, int verbose, int argc, char* argv[]); int jq_testsuite(jv lib_dirs, int verbose, int argc, char* argv[]);
static const char* progname;
/* /*
* For a longer help message we could use a better option parsing * For a longer help message we could use a better option parsing
* strategy, one that lets stack options. * strategy, one that lets stack options.
@@ -52,9 +48,9 @@ static void usage(int code, int keep_it_short) {
int ret = fprintf(f, int ret = fprintf(f,
"jq - commandline JSON processor [version %s]\n" "jq - commandline JSON processor [version %s]\n"
"\nUsage:\t%s [options] <jq filter> [file...]\n" "\nUsage:\tjq [options] <jq filter> [file...]\n"
"\t%s [options] --args <jq filter> [strings...]\n" "\tjq [options] --args <jq filter> [strings...]\n"
"\t%s [options] --jsonargs <jq filter> [JSON_TEXTS...]\n\n" "\tjq [options] --jsonargs <jq filter> [JSON_TEXTS...]\n\n"
"jq is a tool for processing JSON inputs, applying the given filter to\n" "jq is a tool for processing JSON inputs, applying the given filter to\n"
"its JSON text inputs and producing the filter's results as JSON on\n" "its JSON text inputs and producing the filter's results as JSON on\n"
"standard output.\n\n" "standard output.\n\n"
@@ -62,12 +58,9 @@ static void usage(int code, int keep_it_short) {
"unmodified except for formatting. For more advanced filters see\n" "unmodified except for formatting. For more advanced filters see\n"
"the jq(1) manpage (\"man jq\") and/or https://jqlang.org/.\n\n" "the jq(1) manpage (\"man jq\") and/or https://jqlang.org/.\n\n"
"Example:\n\n\t$ echo '{\"foo\": 0}' | jq .\n" "Example:\n\n\t$ echo '{\"foo\": 0}' | jq .\n"
"\t{\n\t \"foo\": 0\n\t}\n\n", "\t{\n\t \"foo\": 0\n\t}\n\n", JQ_VERSION);
JQ_VERSION, progname, progname, progname);
if (keep_it_short) { if (keep_it_short) {
fprintf(f, fprintf(f, "For listing the command options, use jq --help.\n");
"For listing the command options, use %s --help.\n",
progname);
} else { } else {
(void) fprintf(f, (void) fprintf(f,
"Command options:\n" "Command options:\n"
@@ -118,7 +111,7 @@ static void usage(int code, int keep_it_short) {
} }
static void die() { static void die() {
fprintf(stderr, "Use %s --help for help with command-line options,\n", progname); fprintf(stderr, "Use jq --help for help with command-line options,\n");
fprintf(stderr, "or see the jq manpage, or online docs at https://jqlang.org\n"); fprintf(stderr, "or see the jq manpage, or online docs at https://jqlang.org\n");
exit(2); exit(2);
} }
@@ -321,8 +314,6 @@ int main(int argc, char* argv[]) {
jv ARGS = jv_array(); /* positional arguments */ jv ARGS = jv_array(); /* positional arguments */
jv program_arguments = jv_object(); /* named arguments */ jv program_arguments = jv_object(); /* named arguments */
if (argc) progname = argv[0];
jq = jq_init(); jq = jq_init();
if (jq == NULL) { if (jq == NULL) {
perror("jq_init"); perror("jq_init");
@@ -349,7 +340,7 @@ int main(int argc, char* argv[]) {
} else if (further_args_are_json) { } else if (further_args_are_json) {
jv v = jv_parse(argv[i]); jv v = jv_parse(argv[i]);
if (!jv_is_valid(v)) { if (!jv_is_valid(v)) {
fprintf(stderr, "%s: invalid JSON text passed to --jsonargs\n", progname); fprintf(stderr, "jq: invalid JSON text passed to --jsonargs\n");
die(); die();
} }
ARGS = jv_array_append(ARGS, v); ARGS = jv_array_append(ARGS, v);
@@ -426,13 +417,13 @@ int main(int argc, char* argv[]) {
dumpopts |= JV_PRINT_TAB | JV_PRINT_PRETTY; dumpopts |= JV_PRINT_TAB | JV_PRINT_PRETTY;
} else if (isoption(&text, 0, "indent", is_short)) { } else if (isoption(&text, 0, "indent", is_short)) {
if (i >= argc - 1) { if (i >= argc - 1) {
fprintf(stderr, "%s: --indent takes one parameter\n", progname); fprintf(stderr, "jq: --indent takes one parameter\n");
die(); die();
} }
dumpopts &= ~(JV_PRINT_TAB | JV_PRINT_INDENT_FLAGS(7)); dumpopts &= ~(JV_PRINT_TAB | JV_PRINT_INDENT_FLAGS(7));
int indent = atoi(argv[i+1]); int indent = atoi(argv[i+1]);
if (indent < -1 || indent > 7) { if (indent < -1 || indent > 7) {
fprintf(stderr, "%s: --indent takes a number between -1 and 7\n", progname); fprintf(stderr, "jq: --indent takes a number between -1 and 7\n");
die(); die();
} }
dumpopts |= JV_PRINT_INDENT_FLAGS(indent); dumpopts |= JV_PRINT_INDENT_FLAGS(indent);
@@ -453,7 +444,7 @@ int main(int argc, char* argv[]) {
further_args_are_json = 1; further_args_are_json = 1;
} else if (isoption(&text, 0, "arg", is_short)) { } else if (isoption(&text, 0, "arg", is_short)) {
if (i >= argc - 2) { if (i >= argc - 2) {
fprintf(stderr, "%s: --arg takes two parameters (e.g. --arg varname value)\n", progname); fprintf(stderr, "jq: --arg takes two parameters (e.g. --arg varname value)\n");
die(); die();
} }
if (!jv_object_has(jv_copy(program_arguments), jv_string(argv[i+1]))) if (!jv_object_has(jv_copy(program_arguments), jv_string(argv[i+1])))
@@ -461,13 +452,13 @@ int main(int argc, char* argv[]) {
i += 2; // skip the next two arguments i += 2; // skip the next two arguments
} else if (isoption(&text, 0, "argjson", is_short)) { } else if (isoption(&text, 0, "argjson", is_short)) {
if (i >= argc - 2) { if (i >= argc - 2) {
fprintf(stderr, "%s: --argjson takes two parameters (e.g. --argjson varname text)\n", progname); fprintf(stderr, "jq: --argjson takes two parameters (e.g. --argjson varname text)\n");
die(); die();
} }
if (!jv_object_has(jv_copy(program_arguments), jv_string(argv[i+1]))) { if (!jv_object_has(jv_copy(program_arguments), jv_string(argv[i+1]))) {
jv v = jv_parse(argv[i+2]); jv v = jv_parse(argv[i+2]);
if (!jv_is_valid(v)) { if (!jv_is_valid(v)) {
fprintf(stderr, "%s: invalid JSON text passed to --argjson\n", progname); fprintf(stderr, "jq: invalid JSON text passed to --argjson\n");
die(); die();
} }
program_arguments = jv_object_set(program_arguments, jv_string(argv[i+1]), v); program_arguments = jv_object_set(program_arguments, jv_string(argv[i+1]), v);
@@ -477,14 +468,14 @@ int main(int argc, char* argv[]) {
isoption(&text, 0, "slurpfile", is_short)) { isoption(&text, 0, "slurpfile", is_short)) {
const char *which = raw ? "rawfile" : "slurpfile"; const char *which = raw ? "rawfile" : "slurpfile";
if (i >= argc - 2) { if (i >= argc - 2) {
fprintf(stderr, "%s: --%s takes two parameters (e.g. --%s varname filename)\n", progname, which, which); fprintf(stderr, "jq: --%s takes two parameters (e.g. --%s varname filename)\n", which, which);
die(); die();
} }
if (!jv_object_has(jv_copy(program_arguments), jv_string(argv[i+1]))) { if (!jv_object_has(jv_copy(program_arguments), jv_string(argv[i+1]))) {
jv data = jv_load_file(argv[i+2], raw); jv data = jv_load_file(argv[i+2], raw);
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 --%s %s %s: %s\n", progname, which, fprintf(stderr, "jq: Bad JSON in --%s %s %s: %s\n", 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);
ret = JQ_ERROR_SYSTEM; ret = JQ_ERROR_SYSTEM;
@@ -519,9 +510,9 @@ int main(int argc, char* argv[]) {
goto out; goto out;
} else { } else {
if (is_short) { if (is_short) {
fprintf(stderr, "%s: Unknown option -%c\n", progname, text[0]); fprintf(stderr, "jq: Unknown option -%c\n", text[0]);
} else { } else {
fprintf(stderr, "%s: Unknown option --%s\n", progname, text); fprintf(stderr, "jq: Unknown option --%s\n", text);
} }
die(); die();
} }
@@ -599,7 +590,7 @@ int main(int argc, char* argv[]) {
jv data = jv_load_file(program, 1); jv data = jv_load_file(program, 1);
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: %s\n", progname, jv_string_value(data)); fprintf(stderr, "jq: %s\n", jv_string_value(data));
jv_free(data); jv_free(data);
ret = JQ_ERROR_SYSTEM; ret = JQ_ERROR_SYSTEM;
goto out; goto out;