python/cpython · error

Expected long option\n

Error message

Expected long option\n

What it means

Command-line parse error from CPython's internal getopt (Python/getopt.c): an argument began with '--' (long-option form) but had no option name after the dashes — i.e. the bare argument '--'. Long options require a non-empty name, so the parser prints 'Expected long option' (when error printing is enabled) and returns -1, causing the interpreter to reject the command line.

Source

Thrown at Python/getopt.c:105

            return 'h';
        }

        else if (wcscmp(argv[_PyOS_optind], L"--version") == 0) {
            ++_PyOS_optind;
            return 'V';
        }

        opt_ptr = &argv[_PyOS_optind++][1];
    }

    if ((option = *opt_ptr++) == L'\0')
        return -1;

    if (option == L'-') {
        // Parse long option.
        if (*opt_ptr == L'\0') {
            if (_PyOS_opterr) {
                fprintf(stderr, "Expected long option\n");
            }
            return -1;
        }
        *longindex = 0;
        const _PyOS_LongOption *opt;
        for (opt = &longopts[*longindex]; opt->name; opt = &longopts[++(*longindex)]) {
            if (!wcscmp(opt->name, opt_ptr))
                break;
        }
        if (!opt->name) {
            if (_PyOS_opterr) {
                fprintf(stderr, "Unknown option: %ls\n", argv[_PyOS_optind - 1]);
            }
            return '_';
        }
        opt_ptr = L"";
        if (!opt->has_arg) {
            return opt->val;

View on GitHub (pinned to bc6749cc3b)

Solutions

  1. Remove the bare '--' from the python command line: `python3 script.py`
  2. For env-var options, use the documented forms (PYTHONFOO or `python3 -X ...`) rather than inventing long flags
  3. If you need a literal '--' passed to your program, place it after the script name: `python3 script.py -- args`

Example fix

# before
python3 -- script.py

# after
python3 script.py
Defensive patterns

Strategy: validation

Validate before calling

# shell: strip bare '--' from the interpreter portion of the command
# set -- $(printf '%s\n' "$@" | grep -v '^--$')

Prevention

When it happens

Trigger: Passing a literal '--' argument to the python interpreter, e.g. `python3 -- script.py`, or `python3 --` with nothing after; also reachable via APIs that funnel argv through _PyOS_GetOpt (e.g. some embedders and frozen tools).

Common situations: Copy-pasting command lines from tools (like git or grep) where '--' means end-of-options — python's CLI does not support that idiom; shell quoting mistakes producing an empty long option; scripts building argument lists with an empty '--<name>' variable.

Related errors


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