{"record":{"id":"92d909b328448c01","repo":"python/cpython","slug":"argument-expected-for-the-c-option-n","errorCode":null,"errorMessage":"Argument expected for the -%c option\\n","messagePattern":"Argument expected for the -%c option\\\\n","errorType":"console","errorClass":null,"httpStatus":null,"severity":"error","filePath":"Python/getopt.c","lineNumber":155,"sourceCode":"        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;\n            opt_ptr = L\"\";\n        }\n\n        else {\n            if (_PyOS_optind >= argc) {\n                if (_PyOS_opterr) {\n                    fprintf(stderr,\n                        \"Argument expected for the -%c option\\n\", (char)option);\n                }\n                return '_';\n            }\n\n            _PyOS_optarg = argv[_PyOS_optind++];\n        }\n    }\n\n    return option;\n}\n","sourceCodeStart":137,"sourceCodeEnd":164,"githubUrl":"https://github.com/python/cpython/blob/bc6749cc3b5ae4a5e88a6cc2d5b3bebbe354eae6/Python/getopt.c#L137-L164","documentation":"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 '_'.","triggerScenarios":"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).","commonSituations":"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.","solutions":["Provide the argument immediately after the option: `python -W error script.py`","Guard dynamic values: ensure the variable is non-empty before building the command","Consult `python -h` to confirm which short options take arguments"],"exampleFix":"# before\npython -W \"$WARNINGS\" script.py   # $WARNINGS empty -> error\n# after\npython -W \"${WARNINGS:-default}\" script.py","handlingStrategy":"validation","validationCode":"# bash: refuse to run if an option-argument is missing\n: \"${WARNINGS:?WARNINGS must be set for -W}\" \npython -W \"$WARNINGS\" app.py","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Keep short options and their values adjacent on the command line","Use ${VAR:?} or ${VAR:-default} for any variable used as an option argument","Test wrapper scripts with `set -u` to catch unset variables early"],"tags":["cli","command-line","getopt","cpython","arguments"],"backgroundTag":null,"analyzedSha":"bc6749cc3b5ae4a5e88a6cc2d5b3bebbe354eae6","analyzedAt":"2026-08-14T22:01:13.976Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}