Format getopt errors more like others (sentence case, quoted options) (#1966)

This commit is contained in:
Rangi
2026-05-09 13:27:34 +02:00
committed by GitHub
parent 1af8bdda16
commit be2d028ae6
+16 -10
View File
@@ -5,6 +5,7 @@
#include "extern/getopt.hpp" #include "extern/getopt.hpp"
#include <limits.h> #include <limits.h>
#include <stdarg.h>
#include <stddef.h> #include <stddef.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
@@ -18,12 +19,15 @@ int musl_optind = 1, musl_optopt;
static int musl_optpos; static int musl_optpos;
static void musl_getopt_msg(char const *msg, char const *param) { [[gnu::format(printf, 1, 2)]]
static void musl_getopt_error(char const *fmt, ...) {
style_Set(stderr, STYLE_RED, true); style_Set(stderr, STYLE_RED, true);
fputs("error: ", stderr); fputs("error: ", stderr);
style_Reset(stderr); style_Reset(stderr);
fputs(msg, stderr); va_list args;
fputs(param, stderr); va_start(args, fmt);
vfprintf(stderr, fmt, args);
va_end(args);
putc('\n', stderr); putc('\n', stderr);
} }
@@ -92,7 +96,7 @@ static int musl_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] != ':') { if (optstring[0] != ':') {
musl_getopt_msg("unrecognized option: ", optchar); musl_getopt_error("Unrecognized option '-%s'", optchar);
} }
return '?'; return '?';
} }
@@ -107,7 +111,7 @@ static int musl_getopt(int argc, char *argv[], char const *optstring) {
if (optstring[0] == ':') { if (optstring[0] == ':') {
return ':'; return ':';
} }
musl_getopt_msg("option requires an argument: ", optchar); musl_getopt_error("Missing argument for option '-%s'", optchar);
return '?'; return '?';
} }
} }
@@ -179,7 +183,7 @@ static int
if (colon) { if (colon) {
return '?'; return '?';
} }
musl_getopt_msg("option does not take an argument: ", longopts[i].name); musl_getopt_error("Option '--%s' does not take an argument", longopts[i].name);
return '?'; return '?';
} }
musl_optarg = opt + 1; musl_optarg = opt + 1;
@@ -190,7 +194,7 @@ static int
if (colon) { if (colon) {
return ':'; return ':';
} }
musl_getopt_msg("option requires an argument: ", longopts[i].name); musl_getopt_error("Missing argument for option '--%s'", longopts[i].name);
return '?'; return '?';
} }
++musl_optind; ++musl_optind;
@@ -204,9 +208,11 @@ static int
if (argv[musl_optind][1] == '-') { if (argv[musl_optind][1] == '-') {
musl_optopt = 0; musl_optopt = 0;
if (!colon) { if (!colon) {
musl_getopt_msg( if (cnt) {
cnt ? "option is ambiguous: " : "unrecognized option: ", argv[musl_optind] + 2 musl_getopt_error("Ambiguous option '--%s'", argv[musl_optind] + 2);
); } else {
musl_getopt_error("Unrecognized option '--%s'", argv[musl_optind] + 2);
}
} }
++musl_optind; ++musl_optind;
return '?'; return '?';