Skip to content

Commit efb5a88

Browse files
committed
Show conventional colored "error:"/"FATAL:" for CLI option errors
1 parent f065243 commit efb5a88

File tree

7 files changed

+31
-25
lines changed

7 files changed

+31
-25
lines changed

include/cli.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,15 @@
77
#include <string>
88

99
#include "extern/getopt.hpp" // option
10+
#include "usage.hpp"
1011

1112
void cli_ParseArgs(
1213
int argc,
1314
char *argv[],
1415
char const *shortOpts,
1516
option const *longOpts,
1617
void (*parseArg)(int, char *),
17-
void (*fatal)(char const *, ...)
18+
Usage usage
1819
);
1920

2021
#endif // RGBDS_CLI_HPP

src/asm/main.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -513,7 +513,7 @@ int main(int argc, char *argv[]) {
513513
options.maxErrors = 100; // LCOV_EXCL_LINE
514514
}
515515

516-
cli_ParseArgs(argc, argv, optstring, longopts, parseArg, fatal);
516+
cli_ParseArgs(argc, argv, optstring, longopts, parseArg, usage);
517517

518518
if (!options.targetFileName && options.objectFileName) {
519519
options.targetFileName = options.objectFileName;

src/cli.cpp

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,30 @@
22

33
#include <errno.h>
44
#include <fstream>
5+
#include <stdio.h>
56
#include <string.h>
67
#include <string>
78
#include <vector>
89

910
#include "extern/getopt.hpp"
11+
#include "style.hpp"
12+
#include "usage.hpp"
1013
#include "util.hpp" // isBlankSpace
1114

1215
using namespace std::literals;
1316

1417
// Turn an at-file's contents into an argv that `getopt` can handle, appending them to `argPool`.
15-
static std::vector<size_t> readAtFile(
16-
std::string const &path, std::vector<char> &argPool, void (*fatal)(char const *, ...)
17-
) {
18+
static std::vector<size_t>
19+
readAtFile(std::string const &path, std::vector<char> &argPool, Usage usage) {
1820
std::vector<size_t> argvOfs;
1921

2022
std::filebuf file;
2123
if (!file.open(path, std::ios_base::in)) {
22-
std::string msg = "Error reading at-file \""s + path + "\": " + strerror(errno);
23-
fatal(msg.c_str());
24-
return argvOfs; // Since we can't mark the `fatal` function pointer as [[noreturn]]
24+
style_Set(stderr, STYLE_RED, true);
25+
fputs("FATAL: ", stderr);
26+
style_Reset(stderr);
27+
fprintf(stderr, "Failed to open at-file \"%s\": %s\n", path.c_str(), strerror(errno));
28+
usage.printAndExit(1);
2529
}
2630

2731
for (;;) {
@@ -71,7 +75,7 @@ void cli_ParseArgs(
7175
char const *shortOpts,
7276
option const *longOpts,
7377
void (*parseArg)(int, char *),
74-
void (*fatal)(char const *, ...)
78+
Usage usage
7579
) {
7680
struct AtFileStackEntry {
7781
int parentInd; // Saved offset into parent argv
@@ -112,7 +116,7 @@ void cli_ParseArgs(
112116

113117
// It would be nice to compute the char pointers on the fly, but reallocs don't allow
114118
// that; so we must compute the offsets after the pool is fixed
115-
std::vector<size_t> offsets = readAtFile(&musl_optarg[1], argPool, fatal);
119+
std::vector<size_t> offsets = readAtFile(&musl_optarg[1], argPool, usage);
116120
stackEntry.argv.reserve(offsets.size() + 2); // Avoid a bunch of reallocs
117121
for (size_t ofs : offsets) {
118122
stackEntry.argv.push_back(&argPool.data()[ofs]);

src/extern/getopt.cpp

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,21 @@
1111
#include <string.h>
1212
#include <wchar.h>
1313

14+
#include "style.hpp"
15+
1416
char *musl_optarg;
1517
int musl_optind = 1, musl_opterr = 1, musl_optopt;
1618
int musl_optreset = 0;
1719
static int musl_optpos;
1820

19-
static void musl_getopt_msg(char const *a, char const *b, char const *c, size_t l) {
21+
static void musl_getopt_msg(char const *msg, char const *param, size_t len) {
2022
FILE *f = stderr;
2123

22-
if (fputs(a, f) >= 0 && fwrite(b, strlen(b), 1, f) && fwrite(c, 1, l, f) == l) {
24+
style_Set(f, STYLE_RED, true);
25+
fputs("error: ", f);
26+
style_Reset(f);
27+
28+
if (fwrite(msg, strlen(msg), 1, f) && fwrite(param, 1, len, f) == len) {
2329
putc('\n', f);
2430
}
2531
}
@@ -90,7 +96,7 @@ static int getopt(int argc, char *argv[], char const *optstring) {
9096
if (d != c || c == ':') {
9197
musl_optopt = c;
9298
if (optstring[0] != ':' && musl_opterr) {
93-
musl_getopt_msg(argv[0], ": unrecognized option: ", optchar, k);
99+
musl_getopt_msg("unrecognized option: ", optchar, k);
94100
}
95101
return '?';
96102
}
@@ -106,7 +112,7 @@ static int getopt(int argc, char *argv[], char const *optstring) {
106112
return ':';
107113
}
108114
if (musl_opterr) {
109-
musl_getopt_msg(argv[0], ": option requires an argument: ", optchar, k);
115+
musl_getopt_msg("option requires an argument: ", optchar, k);
110116
}
111117
return '?';
112118
}
@@ -228,8 +234,7 @@ static int musl_getopt_long_core(
228234
return '?';
229235
}
230236
musl_getopt_msg(
231-
argv[0],
232-
": option does not take an argument: ",
237+
"option does not take an argument: ",
233238
longopts[i].name,
234239
strlen(longopts[i].name)
235240
);
@@ -247,10 +252,7 @@ static int musl_getopt_long_core(
247252
return '?';
248253
}
249254
musl_getopt_msg(
250-
argv[0],
251-
": option requires an argument: ",
252-
longopts[i].name,
253-
strlen(longopts[i].name)
255+
"option requires an argument: ", longopts[i].name, strlen(longopts[i].name)
254256
);
255257
return '?';
256258
}
@@ -269,8 +271,7 @@ static int musl_getopt_long_core(
269271
musl_optopt = 0;
270272
if (!colon && musl_opterr) {
271273
musl_getopt_msg(
272-
argv[0],
273-
cnt ? ": option is ambiguous: " : ": unrecognized option: ",
274+
cnt ? "option is ambiguous: " : "unrecognized option: ",
274275
argv[musl_optind] + 2,
275276
strlen(argv[musl_optind] + 2)
276277
);

src/fix/main.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,7 @@ static void initLogo() {
346346
}
347347

348348
int main(int argc, char *argv[]) {
349-
cli_ParseArgs(argc, argv, optstring, longopts, parseArg, fatal);
349+
cli_ParseArgs(argc, argv, optstring, longopts, parseArg, usage);
350350

351351
if ((options.cartridgeType & 0xFF00) == TPP1 && !options.japanese) {
352352
warning(

src/gfx/main.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -639,7 +639,7 @@ static void replaceExtension(std::string &path, char const *extension) {
639639
}
640640

641641
int main(int argc, char *argv[]) {
642-
cli_ParseArgs(argc, argv, optstring, longopts, parseArg, fatal);
642+
cli_ParseArgs(argc, argv, optstring, longopts, parseArg, usage);
643643

644644
if (options.nbColorsPerPal == 0) {
645645
options.nbColorsPerPal = 1u << options.bitDepth;

src/link/main.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -415,7 +415,7 @@ static void verboseOutputConfig() {
415415
// LCOV_EXCL_STOP
416416

417417
int main(int argc, char *argv[]) {
418-
cli_ParseArgs(argc, argv, optstring, longopts, parseArg, fatal);
418+
cli_ParseArgs(argc, argv, optstring, longopts, parseArg, usage);
419419

420420
verboseOutputConfig();
421421

0 commit comments

Comments
 (0)