NationalSecurityAgency/ghidra · error
%s: invalid option -- %c\n
Error message
%s: invalid option -- %c\n
What it means
GNU getopt's non-POSIX diagnostic for an invalid short option. Printed when posixly_correct is false and a short-option character is absent from optstring or equals ':'. Identical control flow to the POSIX variant: optopt is set and '?' is returned.
Source
Thrown at GPL/DemanglerGnu/src/demangler_gnu_v2_41/c/getopt.c:796
{
char c = *nextchar++;
char *temp = my_index (optstring, c);
/* Increment `optind' when we start to process its last character. */
if (*nextchar == '\0')
++optind;
if (temp == NULL || c == ':')
{
if (opterr)
{
if (posixly_correct)
/* 1003.2 specifies the format of this message. */
fprintf (stderr, _("%s: illegal option -- %c\n"),
argv[0], c);
else
fprintf (stderr, _("%s: invalid option -- %c\n"),
argv[0], c);
}
optopt = c;
return '?';
}
/* Convenience. Treat POSIX -W foo same as long option --foo */
if (temp[0] == 'W' && temp[1] == ';')
{
char *nameend;
const struct option *p;
const struct option *pfound = NULL;
int exact = 0;
int ambig = 0;
int indfound = 0;
int option_index;
/* This is an option that requires an argument. */
if (*nextchar != '\0')View on GitHub (pinned to d5f144c24d)
Solutions
- Add the missing option letter to optstring.
- Enable POSIXLY_CORRECT if you want the 1003.2 wording (does not fix the underlying invalid option).
- Set opterr = 0 and handle '?' / optopt manually.
- Validate argv before calling getopt for early, custom error reporting.
Example fix
// before
while ((c = getopt(argc, argv, "ab:c")) != -1) { ... } // user: prog -z
// after
while ((c = getopt(argc, argv, "ab:cz")) != -1) { ... } Defensive patterns
Strategy: validation
Validate before calling
opterr = 0;
while ((c = getopt(argc, argv, "ab:c")) != -1) {
if (c == '?') { fprintf(stderr, "unknown option -%c\n", optopt); exit(2); }
} Prevention
- Treat optstring as the single source of truth for accepted flags.
- Pre-validate argv against optstring for clearer messages.
- Set opterr=0 in programs that localize or customize messages.
When it happens
Trigger: A short option not in optstring is passed while POSIX mode is off; the ':' character is used as an option letter.
Common situations: Default (non-POSIX) builds; optstring drift relative to help text; a typo'd flag on the command line.
Related errors
- %s: unrecognized option `%c%s'\n
- %s: illegal option -- %c\n
- %s: option requires an argument -- %c\n
- %s: option `-W %s' is ambiguous\n
- %s: option `-W %s' doesn't allow an argument\n
AI-assisted analysis of NationalSecurityAgency/ghidra@d5f144c24d (2026-08-14).
Data as JSON: /api/errors/aa8dd8e100d7942b.
Report an issue: GitHub.