{"record":{"id":"90d7b478db9289b8","repo":"python/cpython","slug":"argument-expected-for-the-ls-options-n","errorCode":null,"errorMessage":"Argument expected for the %ls options\\n","messagePattern":"Argument expected for the %ls options\\\\n","errorType":"console","errorClass":null,"httpStatus":null,"severity":"error","filePath":"Python/getopt.c","lineNumber":127,"sourceCode":"        *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;\n        }\n        if (_PyOS_optind >= argc) {\n            if (_PyOS_opterr) {\n                fprintf(stderr, \"Argument expected for the %ls options\\n\",\n                        argv[_PyOS_optind - 1]);\n            }\n            return '_';\n        }\n        _PyOS_optarg = argv[_PyOS_optind++];\n        return opt->val;\n    }\n\n    if ((ptr = wcschr(SHORT_OPTS, option)) == NULL) {\n        if (_PyOS_opterr) {\n            fprintf(stderr, \"Unknown option: -%c\\n\", (char)option);\n        }\n        return '_';\n    }\n\n    if (*(ptr + 1) == L':') {\n        if (*opt_ptr != L'\\0') {\n            _PyOS_optarg  = opt_ptr;","sourceCodeStart":109,"sourceCodeEnd":145,"githubUrl":"https://github.com/python/cpython/blob/bc6749cc3b5ae4a5e88a6cc2d5b3bebbe354eae6/Python/getopt.c#L109-L145","documentation":"Emitted by CPython's command-line parser (_PyOS_GetOpt in Python/getopt.c) when a long option that requires an argument (e.g. --check-hash-based-pycs) is passed as the last command-line token with no argument following it. The parser walks argv, finds the option in its long-option table with has_arg set, but _PyOS_optind >= argc so there is nothing left to consume. The option letter '_' is then returned, which the caller treats as an unknown/bad option.","triggerScenarios":"Running the interpreter with a trailing long option that takes an argument but omitting the value, e.g. `python --check-hash-based-pycs` with nothing after it. Any -X style long option registered in the option table (has_arg != 0) whose argument would need to be argv[_PyOS_optind] when optind already equals argc.","commonSituations":"Shell scripts where the option value comes from an unset variable that expands to nothing (`python --check-hash-based-pycs \"$PYC_MODE\"` with PYC_MODE empty/absent); CI wrappers appending flags dynamically; typos that detach the value from the option.","solutions":["Supply the required argument as the next token or with '=' syntax: `python --check-hash-based-pycs=default`","If the value comes from a variable, quote it and verify it is non-empty before invoking python: `[ -n \"$PYC_MODE\" ] && python --check-hash-based-pycs=\"$PYC_MODE\"`","Check `python -h` (or the usage output printed on failure) to confirm which long options require arguments"],"exampleFix":"# before\npython --check-hash-based-pycs \"$PYC_MODE\"   # $PYC_MODE unset -> error\n# after\npython --check-hash-based-pycs \"${PYC_MODE:-default}\"","handlingStrategy":"validation","validationCode":"# bash: ensure long-option values are non-empty before invoking python\n[ -n \"$PYC_MODE\" ] || PYC_MODE=default\npython --check-hash-based-pycs=\"$PYC_MODE\" app.py","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Always use --opt=value form for long options with arguments","Never build python command lines from possibly-empty variables without defaults","Wrap dynamic CLI construction in a script that validates each flag/argument pair"],"tags":["cli","command-line","getopt","cpython","startup"],"backgroundTag":null,"analyzedSha":"bc6749cc3b5ae4a5e88a6cc2d5b3bebbe354eae6","analyzedAt":"2026-08-14T22:01:13.976Z","schemaVersion":2},"datasetVersion":"2026-08-16T03:17:38.424Z"}