Show conventional colored "error:"/"FATAL:" for CLI option errors

This commit is contained in:
Rangi
2025-10-23 12:38:43 -04:00
parent f065243cd2
commit efb5a88edb
7 changed files with 31 additions and 25 deletions

View File

@@ -7,6 +7,7 @@
#include <string>
#include "extern/getopt.hpp" // option
#include "usage.hpp"
void cli_ParseArgs(
int argc,
@@ -14,7 +15,7 @@ void cli_ParseArgs(
char const *shortOpts,
option const *longOpts,
void (*parseArg)(int, char *),
void (*fatal)(char const *, ...)
Usage usage
);
#endif // RGBDS_CLI_HPP

View File

@@ -513,7 +513,7 @@ int main(int argc, char *argv[]) {
options.maxErrors = 100; // LCOV_EXCL_LINE
}
cli_ParseArgs(argc, argv, optstring, longopts, parseArg, fatal);
cli_ParseArgs(argc, argv, optstring, longopts, parseArg, usage);
if (!options.targetFileName && options.objectFileName) {
options.targetFileName = options.objectFileName;

View File

@@ -2,26 +2,30 @@
#include <errno.h>
#include <fstream>
#include <stdio.h>
#include <string.h>
#include <string>
#include <vector>
#include "extern/getopt.hpp"
#include "style.hpp"
#include "usage.hpp"
#include "util.hpp" // isBlankSpace
using namespace std::literals;
// Turn an at-file's contents into an argv that `getopt` can handle, appending them to `argPool`.
static std::vector<size_t> readAtFile(
std::string const &path, std::vector<char> &argPool, void (*fatal)(char const *, ...)
) {
static std::vector<size_t>
readAtFile(std::string const &path, std::vector<char> &argPool, Usage usage) {
std::vector<size_t> argvOfs;
std::filebuf file;
if (!file.open(path, std::ios_base::in)) {
std::string msg = "Error reading at-file \""s + path + "\": " + strerror(errno);
fatal(msg.c_str());
return argvOfs; // Since we can't mark the `fatal` function pointer as [[noreturn]]
style_Set(stderr, STYLE_RED, true);
fputs("FATAL: ", stderr);
style_Reset(stderr);
fprintf(stderr, "Failed to open at-file \"%s\": %s\n", path.c_str(), strerror(errno));
usage.printAndExit(1);
}
for (;;) {
@@ -71,7 +75,7 @@ void cli_ParseArgs(
char const *shortOpts,
option const *longOpts,
void (*parseArg)(int, char *),
void (*fatal)(char const *, ...)
Usage usage
) {
struct AtFileStackEntry {
int parentInd; // Saved offset into parent argv
@@ -112,7 +116,7 @@ void cli_ParseArgs(
// It would be nice to compute the char pointers on the fly, but reallocs don't allow
// that; so we must compute the offsets after the pool is fixed
std::vector<size_t> offsets = readAtFile(&musl_optarg[1], argPool, fatal);
std::vector<size_t> offsets = readAtFile(&musl_optarg[1], argPool, usage);
stackEntry.argv.reserve(offsets.size() + 2); // Avoid a bunch of reallocs
for (size_t ofs : offsets) {
stackEntry.argv.push_back(&argPool.data()[ofs]);

25
src/extern/getopt.cpp vendored
View File

@@ -11,15 +11,21 @@
#include <string.h>
#include <wchar.h>
#include "style.hpp"
char *musl_optarg;
int musl_optind = 1, musl_opterr = 1, musl_optopt;
int musl_optreset = 0;
static int musl_optpos;
static void musl_getopt_msg(char const *a, char const *b, char const *c, size_t l) {
static void musl_getopt_msg(char const *msg, char const *param, size_t len) {
FILE *f = stderr;
if (fputs(a, f) >= 0 && fwrite(b, strlen(b), 1, f) && fwrite(c, 1, l, f) == l) {
style_Set(f, STYLE_RED, true);
fputs("error: ", f);
style_Reset(f);
if (fwrite(msg, strlen(msg), 1, f) && fwrite(param, 1, len, f) == len) {
putc('\n', f);
}
}
@@ -90,7 +96,7 @@ static int getopt(int argc, char *argv[], char const *optstring) {
if (d != c || c == ':') {
musl_optopt = c;
if (optstring[0] != ':' && musl_opterr) {
musl_getopt_msg(argv[0], ": unrecognized option: ", optchar, k);
musl_getopt_msg("unrecognized option: ", optchar, k);
}
return '?';
}
@@ -106,7 +112,7 @@ static int getopt(int argc, char *argv[], char const *optstring) {
return ':';
}
if (musl_opterr) {
musl_getopt_msg(argv[0], ": option requires an argument: ", optchar, k);
musl_getopt_msg("option requires an argument: ", optchar, k);
}
return '?';
}
@@ -228,8 +234,7 @@ static int musl_getopt_long_core(
return '?';
}
musl_getopt_msg(
argv[0],
": option does not take an argument: ",
"option does not take an argument: ",
longopts[i].name,
strlen(longopts[i].name)
);
@@ -247,10 +252,7 @@ static int musl_getopt_long_core(
return '?';
}
musl_getopt_msg(
argv[0],
": option requires an argument: ",
longopts[i].name,
strlen(longopts[i].name)
"option requires an argument: ", longopts[i].name, strlen(longopts[i].name)
);
return '?';
}
@@ -269,8 +271,7 @@ static int musl_getopt_long_core(
musl_optopt = 0;
if (!colon && musl_opterr) {
musl_getopt_msg(
argv[0],
cnt ? ": option is ambiguous: " : ": unrecognized option: ",
cnt ? "option is ambiguous: " : "unrecognized option: ",
argv[musl_optind] + 2,
strlen(argv[musl_optind] + 2)
);

View File

@@ -346,7 +346,7 @@ static void initLogo() {
}
int main(int argc, char *argv[]) {
cli_ParseArgs(argc, argv, optstring, longopts, parseArg, fatal);
cli_ParseArgs(argc, argv, optstring, longopts, parseArg, usage);
if ((options.cartridgeType & 0xFF00) == TPP1 && !options.japanese) {
warning(

View File

@@ -639,7 +639,7 @@ static void replaceExtension(std::string &path, char const *extension) {
}
int main(int argc, char *argv[]) {
cli_ParseArgs(argc, argv, optstring, longopts, parseArg, fatal);
cli_ParseArgs(argc, argv, optstring, longopts, parseArg, usage);
if (options.nbColorsPerPal == 0) {
options.nbColorsPerPal = 1u << options.bitDepth;

View File

@@ -415,7 +415,7 @@ static void verboseOutputConfig() {
// LCOV_EXCL_STOP
int main(int argc, char *argv[]) {
cli_ParseArgs(argc, argv, optstring, longopts, parseArg, fatal);
cli_ParseArgs(argc, argv, optstring, longopts, parseArg, usage);
verboseOutputConfig();