mirror of
https://github.com/gbdev/rgbds.git
synced 2025-11-20 18:22:07 +00:00
Show conventional colored "error:"/"FATAL:" for CLI option errors
This commit is contained in:
@@ -7,6 +7,7 @@
|
|||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
#include "extern/getopt.hpp" // option
|
#include "extern/getopt.hpp" // option
|
||||||
|
#include "usage.hpp"
|
||||||
|
|
||||||
void cli_ParseArgs(
|
void cli_ParseArgs(
|
||||||
int argc,
|
int argc,
|
||||||
@@ -14,7 +15,7 @@ void cli_ParseArgs(
|
|||||||
char const *shortOpts,
|
char const *shortOpts,
|
||||||
option const *longOpts,
|
option const *longOpts,
|
||||||
void (*parseArg)(int, char *),
|
void (*parseArg)(int, char *),
|
||||||
void (*fatal)(char const *, ...)
|
Usage usage
|
||||||
);
|
);
|
||||||
|
|
||||||
#endif // RGBDS_CLI_HPP
|
#endif // RGBDS_CLI_HPP
|
||||||
|
|||||||
@@ -513,7 +513,7 @@ int main(int argc, char *argv[]) {
|
|||||||
options.maxErrors = 100; // LCOV_EXCL_LINE
|
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) {
|
if (!options.targetFileName && options.objectFileName) {
|
||||||
options.targetFileName = options.objectFileName;
|
options.targetFileName = options.objectFileName;
|
||||||
|
|||||||
20
src/cli.cpp
20
src/cli.cpp
@@ -2,26 +2,30 @@
|
|||||||
|
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
|
#include <stdio.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
#include "extern/getopt.hpp"
|
#include "extern/getopt.hpp"
|
||||||
|
#include "style.hpp"
|
||||||
|
#include "usage.hpp"
|
||||||
#include "util.hpp" // isBlankSpace
|
#include "util.hpp" // isBlankSpace
|
||||||
|
|
||||||
using namespace std::literals;
|
using namespace std::literals;
|
||||||
|
|
||||||
// Turn an at-file's contents into an argv that `getopt` can handle, appending them to `argPool`.
|
// Turn an at-file's contents into an argv that `getopt` can handle, appending them to `argPool`.
|
||||||
static std::vector<size_t> readAtFile(
|
static std::vector<size_t>
|
||||||
std::string const &path, std::vector<char> &argPool, void (*fatal)(char const *, ...)
|
readAtFile(std::string const &path, std::vector<char> &argPool, Usage usage) {
|
||||||
) {
|
|
||||||
std::vector<size_t> argvOfs;
|
std::vector<size_t> argvOfs;
|
||||||
|
|
||||||
std::filebuf file;
|
std::filebuf file;
|
||||||
if (!file.open(path, std::ios_base::in)) {
|
if (!file.open(path, std::ios_base::in)) {
|
||||||
std::string msg = "Error reading at-file \""s + path + "\": " + strerror(errno);
|
style_Set(stderr, STYLE_RED, true);
|
||||||
fatal(msg.c_str());
|
fputs("FATAL: ", stderr);
|
||||||
return argvOfs; // Since we can't mark the `fatal` function pointer as [[noreturn]]
|
style_Reset(stderr);
|
||||||
|
fprintf(stderr, "Failed to open at-file \"%s\": %s\n", path.c_str(), strerror(errno));
|
||||||
|
usage.printAndExit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
for (;;) {
|
for (;;) {
|
||||||
@@ -71,7 +75,7 @@ void cli_ParseArgs(
|
|||||||
char const *shortOpts,
|
char const *shortOpts,
|
||||||
option const *longOpts,
|
option const *longOpts,
|
||||||
void (*parseArg)(int, char *),
|
void (*parseArg)(int, char *),
|
||||||
void (*fatal)(char const *, ...)
|
Usage usage
|
||||||
) {
|
) {
|
||||||
struct AtFileStackEntry {
|
struct AtFileStackEntry {
|
||||||
int parentInd; // Saved offset into parent argv
|
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
|
// 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
|
// 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
|
stackEntry.argv.reserve(offsets.size() + 2); // Avoid a bunch of reallocs
|
||||||
for (size_t ofs : offsets) {
|
for (size_t ofs : offsets) {
|
||||||
stackEntry.argv.push_back(&argPool.data()[ofs]);
|
stackEntry.argv.push_back(&argPool.data()[ofs]);
|
||||||
|
|||||||
25
src/extern/getopt.cpp
vendored
25
src/extern/getopt.cpp
vendored
@@ -11,15 +11,21 @@
|
|||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <wchar.h>
|
#include <wchar.h>
|
||||||
|
|
||||||
|
#include "style.hpp"
|
||||||
|
|
||||||
char *musl_optarg;
|
char *musl_optarg;
|
||||||
int musl_optind = 1, musl_opterr = 1, musl_optopt;
|
int musl_optind = 1, musl_opterr = 1, musl_optopt;
|
||||||
int musl_optreset = 0;
|
int musl_optreset = 0;
|
||||||
static int musl_optpos;
|
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;
|
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);
|
putc('\n', f);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -90,7 +96,7 @@ static int getopt(int argc, char *argv[], char const *optstring) {
|
|||||||
if (d != c || c == ':') {
|
if (d != c || c == ':') {
|
||||||
musl_optopt = c;
|
musl_optopt = c;
|
||||||
if (optstring[0] != ':' && musl_opterr) {
|
if (optstring[0] != ':' && musl_opterr) {
|
||||||
musl_getopt_msg(argv[0], ": unrecognized option: ", optchar, k);
|
musl_getopt_msg("unrecognized option: ", optchar, k);
|
||||||
}
|
}
|
||||||
return '?';
|
return '?';
|
||||||
}
|
}
|
||||||
@@ -106,7 +112,7 @@ static int getopt(int argc, char *argv[], char const *optstring) {
|
|||||||
return ':';
|
return ':';
|
||||||
}
|
}
|
||||||
if (musl_opterr) {
|
if (musl_opterr) {
|
||||||
musl_getopt_msg(argv[0], ": option requires an argument: ", optchar, k);
|
musl_getopt_msg("option requires an argument: ", optchar, k);
|
||||||
}
|
}
|
||||||
return '?';
|
return '?';
|
||||||
}
|
}
|
||||||
@@ -228,8 +234,7 @@ static int musl_getopt_long_core(
|
|||||||
return '?';
|
return '?';
|
||||||
}
|
}
|
||||||
musl_getopt_msg(
|
musl_getopt_msg(
|
||||||
argv[0],
|
"option does not take an argument: ",
|
||||||
": option does not take an argument: ",
|
|
||||||
longopts[i].name,
|
longopts[i].name,
|
||||||
strlen(longopts[i].name)
|
strlen(longopts[i].name)
|
||||||
);
|
);
|
||||||
@@ -247,10 +252,7 @@ static int musl_getopt_long_core(
|
|||||||
return '?';
|
return '?';
|
||||||
}
|
}
|
||||||
musl_getopt_msg(
|
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 '?';
|
return '?';
|
||||||
}
|
}
|
||||||
@@ -269,8 +271,7 @@ static int musl_getopt_long_core(
|
|||||||
musl_optopt = 0;
|
musl_optopt = 0;
|
||||||
if (!colon && musl_opterr) {
|
if (!colon && musl_opterr) {
|
||||||
musl_getopt_msg(
|
musl_getopt_msg(
|
||||||
argv[0],
|
cnt ? "option is ambiguous: " : "unrecognized option: ",
|
||||||
cnt ? ": option is ambiguous: " : ": unrecognized option: ",
|
|
||||||
argv[musl_optind] + 2,
|
argv[musl_optind] + 2,
|
||||||
strlen(argv[musl_optind] + 2)
|
strlen(argv[musl_optind] + 2)
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -346,7 +346,7 @@ static void initLogo() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
int main(int argc, char *argv[]) {
|
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) {
|
if ((options.cartridgeType & 0xFF00) == TPP1 && !options.japanese) {
|
||||||
warning(
|
warning(
|
||||||
|
|||||||
@@ -639,7 +639,7 @@ static void replaceExtension(std::string &path, char const *extension) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
int main(int argc, char *argv[]) {
|
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) {
|
if (options.nbColorsPerPal == 0) {
|
||||||
options.nbColorsPerPal = 1u << options.bitDepth;
|
options.nbColorsPerPal = 1u << options.bitDepth;
|
||||||
|
|||||||
@@ -415,7 +415,7 @@ static void verboseOutputConfig() {
|
|||||||
// LCOV_EXCL_STOP
|
// LCOV_EXCL_STOP
|
||||||
|
|
||||||
int main(int argc, char *argv[]) {
|
int main(int argc, char *argv[]) {
|
||||||
cli_ParseArgs(argc, argv, optstring, longopts, parseArg, fatal);
|
cli_ParseArgs(argc, argv, optstring, longopts, parseArg, usage);
|
||||||
|
|
||||||
verboseOutputConfig();
|
verboseOutputConfig();
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user