{"record":{"id":"2d17c14ef8c556c0","repo":"python/cpython","slug":"expected-long-option-n","errorCode":null,"errorMessage":"Expected long option\\n","messagePattern":"Expected long option\\\\n","errorType":"console","errorClass":null,"httpStatus":null,"severity":"error","filePath":"Python/getopt.c","lineNumber":105,"sourceCode":"            return 'h';\n        }\n\n        else if (wcscmp(argv[_PyOS_optind], L\"--version\") == 0) {\n            ++_PyOS_optind;\n            return 'V';\n        }\n\n        opt_ptr = &argv[_PyOS_optind++][1];\n    }\n\n    if ((option = *opt_ptr++) == L'\\0')\n        return -1;\n\n    if (option == L'-') {\n        // Parse long option.\n        if (*opt_ptr == L'\\0') {\n            if (_PyOS_opterr) {\n                fprintf(stderr, \"Expected long option\\n\");\n            }\n            return -1;\n        }\n        *longindex = 0;\n        const _PyOS_LongOption *opt;\n        for (opt = &longopts[*longindex]; opt->name; opt = &longopts[++(*longindex)]) {\n            if (!wcscmp(opt->name, opt_ptr))\n                break;\n        }\n        if (!opt->name) {\n            if (_PyOS_opterr) {\n                fprintf(stderr, \"Unknown option: %ls\\n\", argv[_PyOS_optind - 1]);\n            }\n            return '_';\n        }\n        opt_ptr = L\"\";\n        if (!opt->has_arg) {\n            return opt->val;","sourceCodeStart":87,"sourceCodeEnd":123,"githubUrl":"https://github.com/python/cpython/blob/bc6749cc3b5ae4a5e88a6cc2d5b3bebbe354eae6/Python/getopt.c#L87-L123","documentation":"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.","triggerScenarios":"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).","commonSituations":"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.","solutions":["Remove the bare '--' from the python command line: `python3 script.py`","For env-var options, use the documented forms (PYTHONFOO or `python3 -X ...`) rather than inventing long flags","If you need a literal '--' passed to your program, place it after the script name: `python3 script.py -- args`"],"exampleFix":"# before\npython3 -- script.py\n\n# after\npython3 script.py","handlingStrategy":"validation","validationCode":"# shell: strip bare '--' from the interpreter portion of the command\n# set -- $(printf '%s\\n' \"$@\" | grep -v '^--$')","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Do not use GNU '--' end-of-options syntax with the python launcher itself","Put '--' after the script name when a script must receive it as an argument","Validate generated command lines in scripts (reject empty '--<name>' tokens)"],"tags":["cli","getopt","command-line","cpython","argument-parsing"],"backgroundTag":null,"analyzedSha":"bc6749cc3b5ae4a5e88a6cc2d5b3bebbe354eae6","analyzedAt":"2026-08-14T22:01:13.976Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}