{"record":{"id":"893b07bdd74fc56d","repo":"python/cpython","slug":"one-of-the-arguments-s-is-required","errorCode":null,"errorMessage":"one of the arguments %s is required","messagePattern":"one of the arguments (.+?) is required","errorType":"exception","errorClass":"ArgumentError","httpStatus":null,"severity":"error","filePath":"Lib/argparse.py","lineNumber":2503,"sourceCode":"\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\n        return namespace, extras\n\n    def _read_args_from_files(self, arg_strings):\n        # expand arguments referencing files\n        new_arg_strings = []\n        for arg_string in arg_strings:\n\n            # for regular arguments, just add them back into the list\n            if not arg_string or arg_string[0] not in self.fromfile_prefix_chars:\n                new_arg_strings.append(arg_string)\n\n            # replace arguments referencing files with the file content\n            else:\n                try:\n                    with open(arg_string[1:],\n                              encoding=_sys.getfilesystemencoding(),","sourceCodeStart":2485,"sourceCodeEnd":2521,"githubUrl":"https://github.com/python/cpython/blob/bc6749cc3b5ae4a5e88a6cc2d5b3bebbe354eae6/Lib/argparse.py#L2485-L2521","documentation":"Raised when a mutually exclusive group was created with required=True but no member of that group appeared among the seen non-default actions after parsing. argparse iterates the group's actions, and if none was used it raises ArgumentError listing the group's option names (excluding those whose help is SUPPRESS).","triggerScenarios":"g = parser.add_mutually_exclusive_group(required=True); g.add_argument('--foo'); g.add_argument('--bar'); the user supplies neither --foo nor --bar. Also fires when the only supplied member equals its default (defaults do not count as 'seen non-default').","commonSituations":"Input-source groups like (--file | --url | --stdin) where exactly one must be given; refactoring a single required option into an either/or group and forgetting to update callers; frontends that hide both flags behind an optional GUI toggle.","solutions":["Supply exactly one of the options listed in the error message.","If 'none' is acceptable, create the group without required=True and handle the absence in code.","If exactly-one semantics are wrong, replace the group with a single required option and choices."],"exampleFix":"# before\ng = parser.add_mutually_exclusive_group(required=True)\ng.add_argument('--file'); g.add_argument('--url')\nparser.parse_args([])  # error: one of the arguments --file --url is required\n\n# after\ng = parser.add_mutually_exclusive_group(required=False)\ng.add_argument('--file'); g.add_argument('--url')","handlingStrategy":"validation","validationCode":"def required_group_satisfied(parser, argv):\n    prov = {a.split('=')[0] for a in argv}\n    for g in parser._mutually_exclusive_groups:\n        if not g.required:\n            continue\n        names = {s for a in g._group_actions for s in a.option_strings}\n        if not (names & prov):\n            return sorted(names)\n    return None","typeGuard":null,"tryCatchPattern":"try:\n    args = parser.parse_args(argv)\nexcept argparse.ArgumentError as e:\n    if 'is required' in str(e):\n        print('choose one of the listed options or rerun with --help')","preventionTips":["Make the group optional (required=False) and validate presence in application code with a clearer message.","Offer a default member (e.g. --stdin) so interactive users are never stuck.","Document exclusive groups prominently in README/usage text."],"tags":["argparse","cli","mutually-exclusive","required-argument"],"backgroundTag":null,"analyzedSha":"bc6749cc3b5ae4a5e88a6cc2d5b3bebbe354eae6","analyzedAt":"2026-08-14T22:01:13.976Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}