{"record":{"id":"68340b3acee1a1e9","repo":"python/cpython","slug":"the-following-arguments-are-required-s","errorCode":null,"errorMessage":"the following arguments are required: %s","messagePattern":"the following arguments are required: (.+?)","errorType":"exception","errorClass":"ArgumentError","httpStatus":null,"severity":"error","filePath":"Lib/argparse.py","lineNumber":2487,"sourceCode":"        required_actions = []\n        for action in self._actions:\n            if action not in seen_actions:\n                if action.required:\n                    required_actions.append(_get_action_name(action))\n                else:\n                    # Convert action default now instead of doing it before\n                    # parsing arguments to avoid calling convert functions\n                    # twice (which may fail) if the argument was given, but\n                    # only if it was defined already in the namespace\n                    if (action.default is not None and\n                        isinstance(action.default, str) and\n                        hasattr(namespace, action.dest) and\n                        action.default is getattr(namespace, action.dest)):\n                        setattr(namespace, action.dest,\n                                self._get_value(action, action.default))\n\n        if required_actions:\n            raise ArgumentError(None, _('the following arguments are required: %s') %\n                       ', '.join(required_actions))\n\n        # make sure all required groups had one option present\n        for group in self._mutually_exclusive_groups:\n            if group.required:\n                for action in group._group_actions:\n                    if action in seen_non_default_actions:\n                        break\n\n                # if no actions were used, report the error\n                else:\n                    names = [_get_action_name(action)\n                             for action in group._group_actions\n                             if action.help is not SUPPRESS]\n                    msg = _('one of the arguments %s is required')\n                    raise ArgumentError(None, msg % ' '.join(names))\n\n        # return the updated namespace and the extra arguments","sourceCodeStart":2469,"sourceCodeEnd":2505,"githubUrl":"https://github.com/python/cpython/blob/bc6749cc3b5ae4a5e88a6cc2d5b3bebbe354eae6/Lib/argparse.py#L2469-L2505","documentation":"After parsing completes, argparse collects required actions (required=True options or positionals without defaults) that never appeared in seen_actions and were not satisfied; if the list is non-empty it raises ArgumentError listing every missing argument name joined by commas. This is the standard 'you forgot an argument' failure of parse_args.","triggerScenarios":"An option declared with required=True (add_argument('--out', required=True)) is omitted; a positional with nargs='+' is omitted; a required argument inside a parser is skipped because the user only passed --help-adjacent flags or relied on a default that was never set.","commonSituations":"Scripts invoked by cron/ci with incomplete argument lists; new required options added in a later version breaking old invocations; wrappers that conditionally forward arguments and drop a required one.","solutions":["Supply every missing argument listed in the error message.","If the argument is genuinely optional, remove required=True or provide a sensible default.","Use nargs='?' with a default for positionals that may be absent.","Construct with exit_on_error=False and catch argparse.ArgumentError to surface a custom usage message in API contexts."],"exampleFix":"# before\nparser.add_argument('--out', required=True)\nparser.parse_args([])  # error: the following arguments are required: --out\n\n# after\nparser.add_argument('--out', default='out.txt')","handlingStrategy":"validation","validationCode":"def missing_required(parser, argv):\n    prov = {a.split('=')[0] for a in argv}\n    missing = [s or a.dest for a in parser._actions\n               if a.required and not (set(a.option_strings) & prov)\n               and not a.option_strings]  # required positionals\n    return missing","typeGuard":null,"tryCatchPattern":"try:\n    args = parser.parse_args(argv)\nexcept argparse.ArgumentError as e:\n    if 'arguments are required' in str(e):\n        print(parser.format_usage(), file=sys.stderr)\n        sys.exit(2)","preventionTips":["Provide defaults for options that older callers do not know about instead of marking new flags required.","Gate required options on other flags with subparsers or conditional parsers.","Print parser.format_usage() in wrapper scripts whenever parsing fails."],"tags":["argparse","cli","required-argument","missing-argument"],"backgroundTag":null,"analyzedSha":"bc6749cc3b5ae4a5e88a6cc2d5b3bebbe354eae6","analyzedAt":"2026-08-14T22:01:13.976Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}