python/cpython · error

Argument expected for the -%c option\n

Error message

Argument expected for the -%c option\n

What it means

Emitted by CPython's getopt (Python/getopt.c) when a short option declared with a required argument (followed by ':' in SHORT_OPTS, e.g. -W, -c, -m in some contexts) appears at the end of argv with no remaining token to use as its argument. The parser needed argv[_PyOS_optind] but _PyOS_optind >= argc, so it reports the missing argument and returns '_'.

Source

Thrown at Python/getopt.c:155

        if (_PyOS_opterr) {
            fprintf(stderr, "Unknown option: -%c\n", (char)option);
        }
        return '_';
    }

    if (*(ptr + 1) == L':') {
        if (*opt_ptr != L'\0') {
            _PyOS_optarg  = opt_ptr;
            opt_ptr = L"";
        }

        else {
            if (_PyOS_optind >= argc) {
                if (_PyOS_opterr) {
                    fprintf(stderr,
                        "Argument expected for the -%c option\n", (char)option);
                }
                return '_';
            }

            _PyOS_optarg = argv[_PyOS_optind++];
        }
    }

    return option;
}

View on GitHub (pinned to bc6749cc3b)

Solutions

  1. Provide the argument immediately after the option: `python -W error script.py`
  2. Guard dynamic values: ensure the variable is non-empty before building the command
  3. Consult `python -h` to confirm which short options take arguments

Example fix

# before
python -W "$WARNINGS" script.py   # $WARNINGS empty -> error
# after
python -W "${WARNINGS:-default}" script.py
Defensive patterns

Strategy: validation

Validate before calling

# bash: refuse to run if an option-argument is missing
: "${WARNINGS:?WARNINGS must be set for -W}" 
python -W "$WARNINGS" app.py

Prevention

When it happens

Trigger: Invoking python with a short option that takes an argument as the last token, e.g. `python -W` with nothing after it, or a value variable that expanded empty (`python -m "$MOD"` with MOD unset).

Common situations: Dynamic script invocation where the argument comes from an environment variable or command substitution that produced nothing; truncated command lines in generated docs or Makefiles.

Related errors


AI-assisted analysis of python/cpython@bc6749cc3b (2026-08-14). Data as JSON: /api/errors/92d909b328448c01. Report an issue: GitHub.