{"record":{"id":"6427d977794cdb84","repo":"python/cpython","slug":"unrecognized-arguments-s","errorCode":null,"errorMessage":"unrecognized arguments: %s","messagePattern":"unrecognized arguments: (.+?)","errorType":"exception","errorClass":"ArgumentError","httpStatus":null,"severity":"error","filePath":"Lib/argparse.py","lineNumber":2157,"sourceCode":"                if action.option_strings]\n\n    def _get_positional_actions(self):\n        return [action\n                for action in self._actions\n                if not action.option_strings]\n\n    # =====================================\n    # Command line argument parsing methods\n    # =====================================\n\n    def parse_args(self, args=None, namespace=None):\n        args, argv = self.parse_known_args(args, namespace)\n        if argv:\n            msg = _('unrecognized arguments: %s') % ' '.join(argv)\n            if self.exit_on_error:\n                self.error(msg)\n            else:\n                raise ArgumentError(None, msg)\n        return args\n\n    def parse_known_args(self, args=None, namespace=None):\n        return self._parse_known_args2(args, namespace, intermixed=False)\n\n    def _parse_known_args2(self, args, namespace, intermixed):\n        if args is None:\n            # args default to the system args\n            args = _sys.argv[1:]\n        else:\n            # make sure that args are mutable\n            args = list(args)\n\n        # default Namespace built from parser defaults\n        if namespace is None:\n            namespace = Namespace()\n\n        # add any action defaults that aren't present","sourceCodeStart":2139,"sourceCodeEnd":2175,"githubUrl":"https://github.com/python/cpython/blob/bc6749cc3b5ae4a5e88a6cc2d5b3bebbe354eae6/Lib/argparse.py#L2139-L2175","documentation":"parse_args() calls parse_known_args() and refuses leftover arguments: if anything in argv remains unmatched, it raises ArgumentError('unrecognized arguments: ...') (or calls parser.error() and exits when exit_on_error is True, the default). It means the command line contains options/positionals the parser has no definition for.","triggerScenarios":"Typo'd flags (--verbse) when only --verbose is defined; passing values to a flag declared without nargs; extra positionals beyond declared ones; a required option consumed differently under allow_abbrev=False; forwarding unknown passthrough args to a child process without parse_known_args.","commonSituations":"Wrapper scripts that append extra args; flag renamed between versions while callers still use the old name; abbreviations disabled; arguments intended for another tool after '--' not declared with REMAINDER.","solutions":["Add a definition for the argument, or fix the typo in the invocation.","If extra args are expected, use parse_known_args() and handle the remainder yourself.","Collect passthrough args explicitly with add_argument('rest', nargs=argparse.REMAINDER) or parse the args after '--' manually."],"exampleFix":"# before\nargs = parser.parse_args(['--verbose', '--output', 'x.txt'])\n# ArgumentError: unrecognized arguments: --output x.txt  (no --output defined)\n\n# after\nparser.add_argument('--output', '-o')\nargs = parser.parse_args(['--verbose', '--output', 'x.txt'])  # ok","handlingStrategy":"fallback","validationCode":"expected = {'--verbose', '--output'}\n\ndef args_recognized(argv: list[str]) -> bool:\n    return all(a in expected or not a.startswith('-') for a in argv)","typeGuard":null,"tryCatchPattern":"from argparse import ArgumentError\n\ntry:\n    args = parser.parse_args(argv)\nexcept (ArgumentError, SystemExit):\n    args, extra = parser.parse_known_args(argv)\n    if extra:\n        print(f'ignoring unknown arguments: {extra}')","preventionTips":["Use parse_known_args() when passthrough/extra args are legitimate","Declare nargs=argparse.REMAINDER for tools that forward args to a child process","Test CLIs against real invocations in CI to catch renamed/missing flags"],"tags":["python","argparse","cli","unknown-arguments","user-input"],"backgroundTag":null,"analyzedSha":"bc6749cc3b5ae4a5e88a6cc2d5b3bebbe354eae6","analyzedAt":"2026-08-14T22:01:13.976Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}