{"record":{"id":"49ba3d89a542fffe","repo":"NationalSecurityAgency/ghidra","slug":"s-option-requires-an-argument-c-n-49ba3d","errorCode":null,"errorMessage":"%s: option requires an argument -- %c\\n","messagePattern":"(.+?): option requires an argument -- %c\\\\n","errorType":"console","errorClass":null,"httpStatus":null,"severity":"error","filePath":"GPL/DemanglerGnu/src/demangler_gnu_v2_41/c/getopt.c","lineNumber":826,"sourceCode":"\tint exact = 0;\n\tint ambig = 0;\n\tint indfound = 0;\n\tint option_index;\n\n\t/* This is an option that requires an argument.  */\n\tif (*nextchar != '\\0')\n\t  {\n\t    optarg = nextchar;\n\t    /* If we end this ARGV-element by taking the rest as an arg,\n\t       we must advance to the next element now.  */\n\t    optind++;\n\t  }\n\telse if (optind == argc)\n\t  {\n\t    if (opterr)\n\t      {\n\t\t/* 1003.2 specifies the format of this message.  */\n\t\tfprintf (stderr, _(\"%s: option requires an argument -- %c\\n\"),\n\t\t\t argv[0], c);\n\t      }\n\t    optopt = c;\n\t    if (optstring[0] == ':')\n\t      c = ':';\n\t    else\n\t      c = '?';\n\t    return c;\n\t  }\n\telse\n\t  /* We already incremented `optind' once;\n\t     increment it again when taking next ARGV-elt as argument.  */\n\t  optarg = argv[optind++];\n\n\t/* optarg is now the argument, see if it's in the\n\t   table of longopts.  */\n\n\tfor (nextchar = nameend = optarg; *nameend && *nameend != '='; nameend++)","sourceCodeStart":808,"sourceCodeEnd":844,"githubUrl":"https://github.com/NationalSecurityAgency/ghidra/blob/d5f144c24d6bc53c9cbf4448c6d11143e7696206/GPL/DemanglerGnu/src/demangler_gnu_v2_41/c/getopt.c#L808-L844","documentation":"GNU getopt short-option diagnostic: an option that requires an argument (declared with ':' in optstring) was given as the last token, so there is no following argument to consume (optind == argc). getopt sets optopt, returns ':' if optstring starts with ':', else '?'. Printed in POSIX 1003.2 format.","triggerScenarios":"User writes `-o` (an option taking an argument) as the final argv element with no value attached, e.g. `prog -o` where optstring has \"o:\".","commonSituations":"User forgot the argument value; a quoting/shell-expansion bug ate the value; optstring marks an option as taking an argument the user believed was optional.","solutions":["Supply the missing argument: `prog -o value` or `prog -ovalue`.","If the argument is genuinely optional, drop the ':' for that option in optstring.","Make optstring start with ':' to distinguish 'missing argument' (returns ':') from 'unknown option' (returns '?').","Set opterr = 0 and produce a custom, friendlier message using optopt."],"exampleFix":"// before: optstring \"o:\"; invocation  prog -o\nwhile ((c = getopt(argc, argv, \"o:\")) != -1) { ... }\n\n// after: supply the argument\n//   prog -o somefile\n// or distinguish the error by leading ':' in optstring:\nwhile ((c = getopt(argc, argv, \":o:\")) != -1) { ... }","handlingStrategy":"validation","validationCode":"// require ':' first in optstring to distinguish missing-arg (returns ':')\nwhile ((c = getopt(argc, argv, \":o:\")) != -1) {\n  if (c == ':') { fprintf(stderr, \"-%c needs an argument\\n\", optopt); }\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Document which options take required arguments.","Lead optstring with ':' to disambiguate error returns.","Validate that required-argument options have a following token."],"tags":["cli","getopt","argument-parsing","c"],"backgroundTag":null,"analyzedSha":"d5f144c24d6bc53c9cbf4448c6d11143e7696206","analyzedAt":"2026-08-14T01:00:57.564Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}