NationalSecurityAgency/ghidra · error
%s: illegal option -- %c\n
Error message
%s: illegal option -- %c\n
What it means
GNU getopt's POSIX-mode diagnostic for an illegal short option. Printed (POSIX 1003.2 format) when posixly_correct is set and a short-option character is not found in optstring or is ':'. getopt then sets optopt and returns '?'. Only the diagnostic differs from the non-POSIX variant; behavior is identical.
Source
Thrown at GPL/DemanglerGnu/src/demangler_gnu_v2_41/c/getopt.c:793
}
/* Look at and handle the next short option-character. */
{
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;View on GitHub (pinned to d5f144c24d)
Solutions
- Add the missing character to optstring.
- Unset POSIXLY_CORRECT if POSIX message formatting is not required (changes the wording, not the return).
- Set opterr = 0 to silence the diagnostic and branch on the '?' return / optopt yourself.
- Correct the user's typo via the optopt value.
Example fix
// before: optstring omits 'z'; POSIXLY_CORRECT=1 prog -z
while ((c = getopt(argc, argv, "ab:c")) != -1) { ... }
// after
while ((c = getopt(argc, argv, "ab:cz")) != -1) { ... } Defensive patterns
Strategy: validation
Validate before calling
// guard the POSIX-mode illegal-option path
opterr = 0;
int c = getopt(argc, argv, "ab:c");
if (c == '?') { reportInvalidOption(optopt); } Prevention
- If POSIX wording is unwanted, do not set POSIXLY_CORRECT.
- Keep optstring authoritative over documented flags.
- Handle '?' + optopt rather than relying on stderr text.
When it happens
Trigger: POSIXLY_CORRECT (or posixly_correct) is in effect and the user passes a short option letter absent from optstring, or the reserved ':' character as an option.
Common situations: Programs run under a POSIXLY_CORRECT=1 environment, or built in an environment that defaults to POSIX mode; option letter typo; optstring not updated for a newly documented flag.
Related errors
- %s: illegal option -- %c
- %s: option `-W %s' is ambiguous
- %s: option `-W %s' doesn't allow an argument
- %s: unrecognized option `%c%s'\n
- %s: invalid option -- %c\n
AI-assisted analysis of NationalSecurityAgency/ghidra@d5f144c24d (2026-08-14).
Data as JSON: /api/errors/3ab7182bacfa0810.
Report an issue: GitHub.