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

  1. Add the missing option letter to optstring.
  2. Enable POSIXLY_CORRECT if you want the 1003.2 wording (does not fix the underlying invalid option).
  3. Set opterr = 0 and handle '?' / optopt manually.
  4. 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

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


AI-assisted analysis of NationalSecurityAgency/ghidra@d5f144c24d (2026-08-14). Data as JSON: /api/errors/aa8dd8e100d7942b. Report an issue: GitHub.