Skip to content

Commit 5f7b1ae

Browse files
authored
fix usage in help to use jq not the invoked command path (#3299)
1 parent 8819eb8 commit 5f7b1ae

File tree

1 file changed

+17
-26
lines changed

1 file changed

+17
-26
lines changed

src/main.c

Lines changed: 17 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,12 @@ extern void jv_tsd_dtoa_ctx_init();
3030
#define USE_ISATTY
3131
#endif
3232

33-
#include "compile.h"
3433
#include "jv.h"
3534
#include "jq.h"
36-
#include "jv_alloc.h"
3735
#include "util.h"
3836

3937
int jq_testsuite(jv lib_dirs, int verbose, int argc, char* argv[]);
4038

41-
static const char* progname;
42-
4339
/*
4440
* For a longer help message we could use a better option parsing
4541
* strategy, one that lets stack options.
@@ -52,22 +48,19 @@ static void usage(int code, int keep_it_short) {
5248

5349
int ret = fprintf(f,
5450
"jq - commandline JSON processor [version %s]\n"
55-
"\nUsage:\t%s [options] <jq filter> [file...]\n"
56-
"\t%s [options] --args <jq filter> [strings...]\n"
57-
"\t%s [options] --jsonargs <jq filter> [JSON_TEXTS...]\n\n"
51+
"\nUsage:\tjq [options] <jq filter> [file...]\n"
52+
"\tjq [options] --args <jq filter> [strings...]\n"
53+
"\tjq [options] --jsonargs <jq filter> [JSON_TEXTS...]\n\n"
5854
"jq is a tool for processing JSON inputs, applying the given filter to\n"
5955
"its JSON text inputs and producing the filter's results as JSON on\n"
6056
"standard output.\n\n"
6157
"The simplest filter is ., which copies jq's input to its output\n"
6258
"unmodified except for formatting. For more advanced filters see\n"
6359
"the jq(1) manpage (\"man jq\") and/or https://jqlang.org/.\n\n"
6460
"Example:\n\n\t$ echo '{\"foo\": 0}' | jq .\n"
65-
"\t{\n\t \"foo\": 0\n\t}\n\n",
66-
JQ_VERSION, progname, progname, progname);
61+
"\t{\n\t \"foo\": 0\n\t}\n\n", JQ_VERSION);
6762
if (keep_it_short) {
68-
fprintf(f,
69-
"For listing the command options, use %s --help.\n",
70-
progname);
63+
fprintf(f, "For listing the command options, use jq --help.\n");
7164
} else {
7265
(void) fprintf(f,
7366
"Command options:\n"
@@ -118,7 +111,7 @@ static void usage(int code, int keep_it_short) {
118111
}
119112

120113
static void die() {
121-
fprintf(stderr, "Use %s --help for help with command-line options,\n", progname);
114+
fprintf(stderr, "Use jq --help for help with command-line options,\n");
122115
fprintf(stderr, "or see the jq manpage, or online docs at https://jqlang.org\n");
123116
exit(2);
124117
}
@@ -321,8 +314,6 @@ int main(int argc, char* argv[]) {
321314
jv ARGS = jv_array(); /* positional arguments */
322315
jv program_arguments = jv_object(); /* named arguments */
323316

324-
if (argc) progname = argv[0];
325-
326317
jq = jq_init();
327318
if (jq == NULL) {
328319
perror("jq_init");
@@ -349,7 +340,7 @@ int main(int argc, char* argv[]) {
349340
} else if (further_args_are_json) {
350341
jv v = jv_parse(argv[i]);
351342
if (!jv_is_valid(v)) {
352-
fprintf(stderr, "%s: invalid JSON text passed to --jsonargs\n", progname);
343+
fprintf(stderr, "jq: invalid JSON text passed to --jsonargs\n");
353344
die();
354345
}
355346
ARGS = jv_array_append(ARGS, v);
@@ -426,13 +417,13 @@ int main(int argc, char* argv[]) {
426417
dumpopts |= JV_PRINT_TAB | JV_PRINT_PRETTY;
427418
} else if (isoption(&text, 0, "indent", is_short)) {
428419
if (i >= argc - 1) {
429-
fprintf(stderr, "%s: --indent takes one parameter\n", progname);
420+
fprintf(stderr, "jq: --indent takes one parameter\n");
430421
die();
431422
}
432423
dumpopts &= ~(JV_PRINT_TAB | JV_PRINT_INDENT_FLAGS(7));
433424
int indent = atoi(argv[i+1]);
434425
if (indent < -1 || indent > 7) {
435-
fprintf(stderr, "%s: --indent takes a number between -1 and 7\n", progname);
426+
fprintf(stderr, "jq: --indent takes a number between -1 and 7\n");
436427
die();
437428
}
438429
dumpopts |= JV_PRINT_INDENT_FLAGS(indent);
@@ -453,21 +444,21 @@ int main(int argc, char* argv[]) {
453444
further_args_are_json = 1;
454445
} else if (isoption(&text, 0, "arg", is_short)) {
455446
if (i >= argc - 2) {
456-
fprintf(stderr, "%s: --arg takes two parameters (e.g. --arg varname value)\n", progname);
447+
fprintf(stderr, "jq: --arg takes two parameters (e.g. --arg varname value)\n");
457448
die();
458449
}
459450
if (!jv_object_has(jv_copy(program_arguments), jv_string(argv[i+1])))
460451
program_arguments = jv_object_set(program_arguments, jv_string(argv[i+1]), jv_string(argv[i+2]));
461452
i += 2; // skip the next two arguments
462453
} else if (isoption(&text, 0, "argjson", is_short)) {
463454
if (i >= argc - 2) {
464-
fprintf(stderr, "%s: --argjson takes two parameters (e.g. --argjson varname text)\n", progname);
455+
fprintf(stderr, "jq: --argjson takes two parameters (e.g. --argjson varname text)\n");
465456
die();
466457
}
467458
if (!jv_object_has(jv_copy(program_arguments), jv_string(argv[i+1]))) {
468459
jv v = jv_parse(argv[i+2]);
469460
if (!jv_is_valid(v)) {
470-
fprintf(stderr, "%s: invalid JSON text passed to --argjson\n", progname);
461+
fprintf(stderr, "jq: invalid JSON text passed to --argjson\n");
471462
die();
472463
}
473464
program_arguments = jv_object_set(program_arguments, jv_string(argv[i+1]), v);
@@ -477,14 +468,14 @@ int main(int argc, char* argv[]) {
477468
isoption(&text, 0, "slurpfile", is_short)) {
478469
const char *which = raw ? "rawfile" : "slurpfile";
479470
if (i >= argc - 2) {
480-
fprintf(stderr, "%s: --%s takes two parameters (e.g. --%s varname filename)\n", progname, which, which);
471+
fprintf(stderr, "jq: --%s takes two parameters (e.g. --%s varname filename)\n", which, which);
481472
die();
482473
}
483474
if (!jv_object_has(jv_copy(program_arguments), jv_string(argv[i+1]))) {
484475
jv data = jv_load_file(argv[i+2], raw);
485476
if (!jv_is_valid(data)) {
486477
data = jv_invalid_get_msg(data);
487-
fprintf(stderr, "%s: Bad JSON in --%s %s %s: %s\n", progname, which,
478+
fprintf(stderr, "jq: Bad JSON in --%s %s %s: %s\n", which,
488479
argv[i+1], argv[i+2], jv_string_value(data));
489480
jv_free(data);
490481
ret = JQ_ERROR_SYSTEM;
@@ -519,9 +510,9 @@ int main(int argc, char* argv[]) {
519510
goto out;
520511
} else {
521512
if (is_short) {
522-
fprintf(stderr, "%s: Unknown option -%c\n", progname, text[0]);
513+
fprintf(stderr, "jq: Unknown option -%c\n", text[0]);
523514
} else {
524-
fprintf(stderr, "%s: Unknown option --%s\n", progname, text);
515+
fprintf(stderr, "jq: Unknown option --%s\n", text);
525516
}
526517
die();
527518
}
@@ -599,7 +590,7 @@ int main(int argc, char* argv[]) {
599590
jv data = jv_load_file(program, 1);
600591
if (!jv_is_valid(data)) {
601592
data = jv_invalid_get_msg(data);
602-
fprintf(stderr, "%s: %s\n", progname, jv_string_value(data));
593+
fprintf(stderr, "jq: %s\n", jv_string_value(data));
603594
jv_free(data);
604595
ret = JQ_ERROR_SYSTEM;
605596
goto out;

0 commit comments

Comments
 (0)