{"record":{"id":"11ae7d22723532df","repo":"NationalSecurityAgency/ghidra","slug":"s-option-w-s-is-ambiguous","errorCode":null,"errorMessage":"%s: option `-W %s' is ambiguous","messagePattern":"(.+?): option `-W (.+?)' is ambiguous","errorType":"console","errorClass":null,"httpStatus":null,"severity":"error","filePath":"GPL/DemanglerGnu/src/demangler_gnu_v2_24/c/getopt.c","lineNumber":874,"sourceCode":"\t\t  pfound = p;\n\t\t  indfound = option_index;\n\t\t  exact = 1;\n\t\t  break;\n\t\t}\n\t      else if (pfound == NULL)\n\t\t{\n\t\t  /* First nonexact match found.  */\n\t\t  pfound = p;\n\t\t  indfound = option_index;\n\t\t}\n\t      else\n\t\t/* Second or later nonexact match found.  */\n\t\tambig = 1;\n\t    }\n\tif (ambig && !exact)\n\t  {\n\t    if (opterr)\n\t      fprintf (stderr, _(\"%s: option `-W %s' is ambiguous\\n\"),\n\t\t       argv[0], argv[optind]);\n\t    nextchar += strlen (nextchar);\n\t    optind++;\n\t    return '?';\n\t  }\n\tif (pfound != NULL)\n\t  {\n\t    option_index = indfound;\n\t    if (*nameend)\n\t      {\n\t\t/* Don't test has_arg with >, because some C compilers don't\n\t\t   allow it to be used on enums.  */\n\t\tif (pfound->has_arg)\n\t\t  optarg = nameend + 1;\n\t\telse\n\t\t  {\n\t\t    if (opterr)\n\t\t      fprintf (stderr, _(\"\\","sourceCodeStart":856,"sourceCodeEnd":892,"githubUrl":"https://github.com/NationalSecurityAgency/ghidra/blob/d5f144c24d6bc53c9cbf4448c6d11143e7696206/GPL/DemanglerGnu/src/demangler_gnu_v2_24/c/getopt.c#L856-L892","documentation":"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 '?'.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Spell out the full option name instead of abbreviating","Use a longer prefix that is unique","Use the standard --long-option syntax instead of -W"],"exampleFix":"// before\n./tool -W pre\n// after\n./tool -W prefix  # or --prefix","handlingStrategy":"validation","validationCode":"// Check if a -W prefix is ambiguous against the longopts table\nstatic int is_w_prefix_unique(const char *prefix, const struct option *longopts) {\n    int count = 0;\n    for (const struct option *o = longopts; o->name; o++) {\n        if (strncmp(o->name, prefix, strlen(prefix)) == 0) count++;\n    }\n    return count <= 1;\n}","typeGuard":null,"tryCatchPattern":"int c;\nwhile ((c = getopt_long(argc, argv, optstring, longopts, NULL)) != -1) {\n    if (c == '?') { exit(EXIT_FAILURE); }\n}","preventionTips":["Avoid abbreviating option names in scripts — spell them out fully","Prefer --long-option over -W shorthand","If using -W, verify the prefix is unique before deploying"],"tags":["cli","getopt","arguments","posix","c"],"backgroundTag":null,"analyzedSha":"d5f144c24d6bc53c9cbf4448c6d11143e7696206","analyzedAt":"2026-08-14T01:00:57.564Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}