NationalSecurityAgency/ghidra · error

%s: invalid option -- %c

Error message

%s: invalid option -- %c

What it means

Same underlying condition as error 22 (short option character not in optstring), but in non-POSIX mode the wording is 'invalid option' instead of 'illegal option'. This is the default wording when posixly_correct is false.

Source

Thrown at GPL/DemanglerGnu/src/demangler_gnu_v2_24/c/getopt.c:797

  {
    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. Remove or correct the unsupported short option
  2. Check `tool --help` for valid flags
  3. Verify the option exists in this build/version

Example fix

// before
./c++filt -Z
// after
./c++filt -n
Defensive patterns

Strategy: validation

Validate before calling

// Verify each short-option flag char is present in the optstring
static int validate_short_flags(int argc, char *const argv[], const char *optstring) {
    for (int i = 1; i < argc && argv[i][0] == '-' && argv[i][1] != '-'; i++) {
        for (const char *p = argv[i] + 1; *p && *p != '='; p++) {
            if (strchr(optstring, *p) == NULL) {
                fprintf(stderr, "Invalid option: -%c\n", *p);
                return 0;
            }
        }
    }
    return 1;
}

Type guard

static int is_valid_short_opt_char(char c, const char *optstring) {
    return c != ':' && strchr(optstring, c) != NULL;
}

Try / catch

int c;
while ((c = getopt(argc, argv, optstring)) != -1) {
    if (c == '?') { fprintf(stderr, "Usage: %s [options]\n", argv[0]); exit(EXIT_FAILURE); }
}

Prevention

When it happens

Trigger: User passes `-X` where `X` is not in optstring; posixly_correct is false; opterr is non-zero. Also fires when c is ':' but ':' isn't a leading character in optstring.

Common situations: Passing an unsupported short option in the default (non-POSIX) environment; typo in a short option flag; using options from a different version of the tool.

Related errors


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