NationalSecurityAgency/ghidra · error

%s: option requires an argument -- %c\n

Error message

%s: option requires an argument -- %c\n

What it means

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.

Source

Thrown at GPL/DemanglerGnu/src/demangler_gnu_v2_41/c/getopt.c:826

	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. Supply the missing argument: `prog -o value` or `prog -ovalue`.
  2. If the argument is genuinely optional, drop the ':' for that option in optstring.
  3. Make optstring start with ':' to distinguish 'missing argument' (returns ':') from 'unknown option' (returns '?').
  4. Set opterr = 0 and produce a custom, friendlier message using optopt.

Example fix

// before: optstring "o:"; invocation  prog -o
while ((c = getopt(argc, argv, "o:")) != -1) { ... }

// after: supply the argument
//   prog -o somefile
// or distinguish the error by leading ':' in optstring:
while ((c = getopt(argc, argv, ":o:")) != -1) { ... }
Defensive patterns

Strategy: validation

Validate before calling

// require ':' first in optstring to distinguish missing-arg (returns ':')
while ((c = getopt(argc, argv, ":o:")) != -1) {
  if (c == ':') { fprintf(stderr, "-%c needs an argument\n", optopt); }
}

Prevention

When it happens

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

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

Related errors


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