NationalSecurityAgency/ghidra · error

%s: option `-W %s' is ambiguous

Error message

%s: option `-W %s' is ambiguous

What it means

Using POSIX `-W foo` long-option shorthand, the prefix `foo` matches two or more entries in the longopts table with no exact match. getopt sets `ambig` on the second prefix match and, if no exact name was found, reports ambiguity and returns '?'.

Source

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

		  pfound = p;
		  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 `-W %s' is ambiguous\n"),
		       argv[0], argv[optind]);
	    nextchar += strlen (nextchar);
	    optind++;
	    return '?';
	  }
	if (pfound != NULL)
	  {
	    option_index = indfound;
	    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
		  {
		    if (opterr)
		      fprintf (stderr, _("\

View on GitHub (pinned to d5f144c24d)

Solutions

  1. Spell out the full option name instead of abbreviating
  2. Use a longer prefix that is unique
  3. Use the standard --long-option syntax instead of -W

Example fix

// before
./tool -W pre
// after
./tool -W prefix  # or --prefix
Defensive patterns

Strategy: validation

Validate before calling

// Check if a -W prefix is ambiguous against the longopts table
static int is_w_prefix_unique(const char *prefix, const struct option *longopts) {
    int count = 0;
    for (const struct option *o = longopts; o->name; o++) {
        if (strncmp(o->name, prefix, strlen(prefix)) == 0) count++;
    }
    return count <= 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 `-W pre` where longopts contains entries like `prefix` and `preview` (both start with `pre`); no exact match for `pre` exists. opterr is non-zero.

Common situations: Abbreviating a long option via -W that shares a prefix with another option; using -W shorthand in scripts with fragile abbreviations; longopts table grew new entries in a newer version creating new ambiguities.

Related errors


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