{"record":{"id":"ed10d04c493cf97d","repo":"NationalSecurityAgency/ghidra","slug":"s-option-requires-an-argument-c","errorCode":null,"errorMessage":"%s: option requires an argument -- %c","messagePattern":"(.+?): option requires an argument -- %c","errorType":"console","errorClass":null,"httpStatus":null,"severity":"error","filePath":"GPL/DemanglerGnu/src/demangler_gnu_v2_24/c/getopt.c","lineNumber":827,"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":809,"sourceCodeEnd":845,"githubUrl":"https://github.com/NationalSecurityAgency/ghidra/blob/d5f144c24d6bc53c9cbf4448c6d11143e7696206/GPL/DemanglerGnu/src/demangler_gnu_v2_24/c/getopt.c#L809-L845","documentation":"A short option that requires an argument (marked with ':' in optstring) was given as the last token on the command line, leaving no value for it. getopt detects optind == argc after consuming the option, reports the missing argument per POSIX 1003.2, and returns '?' (or ':' if optstring starts with ':').","triggerScenarios":"optstring contains `o:` (option requires argument); user passes `-o` as the final argument with no value following; optind has already advanced to argc. opterr must be non-zero.","commonSituations":"Trailing option with no value (e.g. `c++filt -s` with no style name); shell quoting error that swallowed the argument; script that conditionally appends an option but not its value.","solutions":["Provide the required argument value after the option","Check shell quoting if the argument was present but not passed","If the argument is optional in your workflow, restructure the command logic"],"exampleFix":"// before\n./c++filt -s\n// after\n./c++filt -s gnu-v3","handlingStrategy":"validation","validationCode":"// Check that each option requiring an argument has one\nstatic int check_required_args(int argc, char *const argv[], const char *optstring) {\n    for (int i = 1; i < argc; i++) {\n        if (argv[i][0] == '-' && argv[i][1] != '-') {\n            const char *p;\n            for (p = argv[i] + 1; *p; p++) {\n                char *pos = strchr(optstring, *p);\n                if (pos && pos[1] == ':') {\n                    // this option needs an argument\n                    if (p[1] == '\\0' && i + 1 >= argc) {\n                        fprintf(stderr, \"Option -%c requires an argument\\n\", *p);\n                        return 0;\n                    }\n                    break; // rest of token is the argument\n                }\n            }\n        }\n    }\n    return 1;\n}","typeGuard":null,"tryCatchPattern":"int c;\nwhile ((c = getopt(argc, argv, optstring)) != -1) {\n    if (c == '?' || c == ':') { exit(EXIT_FAILURE); }\n}","preventionTips":["Always provide a value immediately after argument-requiring options","Use --option=value syntax to make arguments explicit","Test commands with empty/short argument lists"],"tags":["cli","getopt","arguments","c"],"backgroundTag":null,"analyzedSha":"d5f144c24d6bc53c9cbf4448c6d11143e7696206","analyzedAt":"2026-08-14T01:00:57.564Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}