NationalSecurityAgency/ghidra · error

%s: option requires an argument -- %c

Error message

%s: option requires an argument -- %c

What it means

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 ':').

Source

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

	int exact = 0;
	int ambig = 0;
	int indfound = 0;
	int option_index;

	/* This is an option that requires an argument.  */
	if (*nextchar != '\0')
	  {
	    optarg = nextchar;
	    /* If we end this ARGV-element by taking the rest as an arg,
	       we must advance to the next element now.  */
	    optind++;
	  }
	else if (optind == argc)
	  {
	    if (opterr)
	      {
		/* 1003.2 specifies the format of this message.  */
		fprintf (stderr, _("%s: option requires an argument -- %c\n"),
			 argv[0], c);
	      }
	    optopt = c;
	    if (optstring[0] == ':')
	      c = ':';
	    else
	      c = '?';
	    return c;
	  }
	else
	  /* We already incremented `optind' once;
	     increment it again when taking next ARGV-elt as argument.  */
	  optarg = argv[optind++];

	/* optarg is now the argument, see if it's in the
	   table of longopts.  */

	for (nextchar = nameend = optarg; *nameend && *nameend != '='; nameend++)

View on GitHub (pinned to d5f144c24d)

Solutions

  1. Provide the required argument value after the option
  2. Check shell quoting if the argument was present but not passed
  3. If the argument is optional in your workflow, restructure the command logic

Example fix

// before
./c++filt -s
// after
./c++filt -s gnu-v3
Defensive patterns

Strategy: validation

Validate before calling

// Check that each option requiring an argument has one
static int check_required_args(int argc, char *const argv[], const char *optstring) {
    for (int i = 1; i < argc; i++) {
        if (argv[i][0] == '-' && argv[i][1] != '-') {
            const char *p;
            for (p = argv[i] + 1; *p; p++) {
                char *pos = strchr(optstring, *p);
                if (pos && pos[1] == ':') {
                    // this option needs an argument
                    if (p[1] == '\0' && i + 1 >= argc) {
                        fprintf(stderr, "Option -%c requires an argument\n", *p);
                        return 0;
                    }
                    break; // rest of token is the argument
                }
            }
        }
    }
    return 1;
}

Try / catch

int c;
while ((c = getopt(argc, argv, optstring)) != -1) {
    if (c == '?' || c == ':') { exit(EXIT_FAILURE); }
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


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