NationalSecurityAgency/ghidra · error

%s: option `%s' is ambiguous\n

Error message

%s: option `%s' is ambiguous\n

What it means

In getopt (v2_41), a long-option token is a prefix of two or more entries in the longopts table and no exact match exists. getopt sets ambig on the second prefix match and, combined with !exact, reports the ambiguity and returns '?'.

Source

Thrown at GPL/DemanglerGnu/src/demangler_gnu_v2_41/c/getopt.c:686

		indfound = option_index;
		exact = 1;
		break;
	      }
	    else if (pfound == NULL)
	      {
		/* First nonexact match found.  */
		pfound = p;
		indfound = option_index;
	      }
	    else
	      /* Second or later nonexact match found.  */
	      ambig = 1;
	  }

      if (ambig && !exact)
	{
	  if (opterr)
	    fprintf (stderr, _("%s: option `%s' is ambiguous\n"),
		     argv[0], argv[optind]);
	  nextchar += strlen (nextchar);
	  optind++;
	  optopt = 0;
	  return '?';
	}

      if (pfound != NULL)
	{
	  option_index = indfound;
	  optind++;
	  if (*nameend)
	    {
	      /* Don't test has_arg with >, because some C compilers don't
		 allow it to be used on enums.  */
	      if (pfound->has_arg)
		optarg = nameend + 1;
	      else

View on GitHub (pinned to d5f144c24d)

Solutions

  1. Spell out the full option name
  2. Use a longer unambiguous prefix
  3. Check `--help` for option names

Example fix

// before
./c++filt --ver input
// after
./c++filt --verbose input
Defensive patterns

Strategy: validation

Validate before calling

// Check if a long-option prefix is ambiguous before passing to getopt
static int is_unique_prefix(const char *prefix, const struct option *longopts) {
    int matches = 0;
    size_t plen = strlen(prefix);
    for (const struct option *o = longopts; o->name; o++) {
        if (strncmp(o->name, prefix, plen) == 0) matches++;
    }
    return matches == 1;
}

Try / catch

int c;
while ((c = getopt_long(argc, argv, optstring, longopts, NULL)) != -1) {
    if (c == '?') { exit(EXIT_FAILURE); }
}

Prevention

When it happens

Trigger: User passes `--pre` and longopts contains both `prefix` and `preview`; no entry named exactly `pre`. opterr is non-zero.

Common situations: Abbreviating long options in scripts with prefixes that later become ambiguous when new options are added; using a short abbreviation that worked in an older version.

Related errors


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