Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 27, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 17 additions & 26 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,12 @@ extern void jv_tsd_dtoa_ctx_init();
#define USE_ISATTY
#endif

#include "compile.h"
#include "jv.h"
#include "jq.h"
#include "jv_alloc.h"
#include "util.h"

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
* strategy, one that lets stack options.
Expand All @@ -52,22 +48,19 @@ static void usage(int code, int keep_it_short) {

int ret = fprintf(f,
"jq - commandline JSON processor [version %s]\n"
"\nUsage:\t%s [options] <jq filter> [file...]\n"
"\t%s [options] --args <jq filter> [strings...]\n"
"\t%s [options] --jsonargs <jq filter> [JSON_TEXTS...]\n\n"
"\nUsage:\tjq [options] <jq filter> [file...]\n"
"\tjq [options] --args <jq filter> [strings...]\n"
"\tjq [options] --jsonargs <jq filter> [JSON_TEXTS...]\n\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"
"standard output.\n\n"
"The simplest filter is ., which copies jq's input to its output\n"
"unmodified except for formatting. For more advanced filters see\n"
"the jq(1) manpage (\"man jq\") and/or https://jqlang.org/.\n\n"
"Example:\n\n\t$ echo '{\"foo\": 0}' | jq .\n"
"\t{\n\t \"foo\": 0\n\t}\n\n",
JQ_VERSION, progname, progname, progname);
"\t{\n\t \"foo\": 0\n\t}\n\n", JQ_VERSION);
if (keep_it_short) {
fprintf(f,
"For listing the command options, use %s --help.\n",
progname);
fprintf(f, "For listing the command options, use jq --help.\n");
} else {
(void) fprintf(f,
"Command options:\n"
Expand Down Expand Up @@ -118,7 +111,7 @@ static void usage(int code, int keep_it_short) {
}

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");
exit(2);
}
Expand Down Expand Up @@ -321,8 +314,6 @@ int main(int argc, char* argv[]) {
jv ARGS = jv_array(); /* positional arguments */
jv program_arguments = jv_object(); /* named arguments */

if (argc) progname = argv[0];

jq = jq_init();
if (jq == NULL) {
perror("jq_init");
Expand All @@ -349,7 +340,7 @@ int main(int argc, char* argv[]) {
} else if (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);
fprintf(stderr, "jq: invalid JSON text passed to --jsonargs\n");
die();
}
ARGS = jv_array_append(ARGS, v);
Expand Down Expand Up @@ -426,13 +417,13 @@ int main(int argc, char* argv[]) {
dumpopts |= JV_PRINT_TAB | JV_PRINT_PRETTY;
} else if (isoption(&text, 0, "indent", is_short)) {
if (i >= argc - 1) {
fprintf(stderr, "%s: --indent takes one parameter\n", progname);
fprintf(stderr, "jq: --indent takes one parameter\n");
die();
}
dumpopts &= ~(JV_PRINT_TAB | JV_PRINT_INDENT_FLAGS(7));
int indent = atoi(argv[i+1]);
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();
}
dumpopts |= JV_PRINT_INDENT_FLAGS(indent);
Expand All @@ -453,21 +444,21 @@ int main(int argc, char* argv[]) {
further_args_are_json = 1;
} else if (isoption(&text, 0, "arg", is_short)) {
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();
}
if (!jv_object_has(jv_copy(program_arguments), jv_string(argv[i+1])))
program_arguments = jv_object_set(program_arguments, jv_string(argv[i+1]), jv_string(argv[i+2]));
i += 2; // skip the next two arguments
} else if (isoption(&text, 0, "argjson", is_short)) {
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();
}
if (!jv_object_has(jv_copy(program_arguments), jv_string(argv[i+1]))) {
jv v = jv_parse(argv[i+2]);
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();
}
program_arguments = jv_object_set(program_arguments, jv_string(argv[i+1]), v);
Expand All @@ -477,14 +468,14 @@ int main(int argc, char* argv[]) {
isoption(&text, 0, "slurpfile", is_short)) {
const char *which = raw ? "rawfile" : "slurpfile";
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();
}
if (!jv_object_has(jv_copy(program_arguments), jv_string(argv[i+1]))) {
jv data = jv_load_file(argv[i+2], raw);
if (!jv_is_valid(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));
jv_free(data);
ret = JQ_ERROR_SYSTEM;
Expand Down Expand Up @@ -519,9 +510,9 @@ int main(int argc, char* argv[]) {
goto out;
} else {
if (is_short) {
fprintf(stderr, "%s: Unknown option -%c\n", progname, text[0]);
fprintf(stderr, "jq: Unknown option -%c\n", text[0]);
} else {
fprintf(stderr, "%s: Unknown option --%s\n", progname, text);
fprintf(stderr, "jq: Unknown option --%s\n", text);
}
die();
}
Expand Down Expand Up @@ -599,7 +590,7 @@ int main(int argc, char* argv[]) {
jv data = jv_load_file(program, 1);
if (!jv_is_valid(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);
ret = JQ_ERROR_SYSTEM;
goto out;
Expand Down
Loading