{"record":{"id":"9ea796f8eaa6e46b","repo":"python/cpython","slug":"nargs-must-be-r-to-supply-const","errorCode":null,"errorMessage":"nargs must be %r to supply const","messagePattern":"nargs must be %r to supply const","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"Lib/argparse.py","lineNumber":1115,"sourceCode":"\n    def __init__(self,\n                 option_strings,\n                 dest,\n                 nargs=None,\n                 const=None,\n                 default=None,\n                 type=None,\n                 choices=None,\n                 required=False,\n                 help=None,\n                 metavar=None,\n                 deprecated=False):\n        if nargs == 0:\n            raise ValueError('nargs for store actions must be != 0; if you '\n                             'have nothing to store, actions such as store '\n                             'true or store const may be more appropriate')\n        if const is not None and nargs != OPTIONAL:\n            raise ValueError('nargs must be %r to supply const' % OPTIONAL)\n        super(_StoreAction, self).__init__(\n            option_strings=option_strings,\n            dest=dest,\n            nargs=nargs,\n            const=const,\n            default=default,\n            type=type,\n            choices=choices,\n            required=required,\n            help=help,\n            metavar=metavar,\n            deprecated=deprecated)\n\n    def __call__(self, parser, namespace, values, option_string=None):\n        setattr(namespace, self.dest, values)\n\n\nclass _StoreConstAction(Action):","sourceCodeStart":1097,"sourceCodeEnd":1133,"githubUrl":"https://github.com/python/cpython/blob/bc6749cc3b5ae4a5e88a6cc2d5b3bebbe354eae6/Lib/argparse.py#L1097-L1133","documentation":"_StoreAction requires nargs to be '?' (OPTIONAL) when const is supplied, because const is only defined for the case where the option appears without a value. Supplying const with any other nargs (including None/default, '*', '+', or an int) is ambiguous and raises ValueError at parser-construction time.","triggerScenarios":"parser.add_argument('--foo', const='x') without nargs='?'; parser.add_argument('--foo', const='x', nargs='*'); passing const through a wrapper that does not set nargs.","commonSituations":"Wanting a default-but-different value for a flag and forgetting nargs='?'; copy-pasting an argument spec and deleting the nargs line; dynamic argument builders that always forward const.","solutions":["Add nargs='?': add_argument('--foo', nargs='?', const='x', default='y').","If you only need a value when absent, use default instead of const.","For count-style flags, use action='count' or 'store_const' rather than const on a store action."],"exampleFix":"# before\nparser.add_argument('--log', const='verbose')  # ValueError: nargs must be '?' to supply const\n\n# after\nparser.add_argument('--log', nargs='?', const='verbose', default='info')","handlingStrategy":"validation","validationCode":"def validate_const_nargs(const, nargs):\n    if const is not None and nargs != '?':\n        raise ValueError(\"const requires nargs='?'\")","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Pair const with nargs='?' always","Use default (not const) for absent-value behavior","Prefer store_const for fixed-value flags"],"tags":["python","argparse","cli","nargs","const","parser-construction"],"backgroundTag":null,"analyzedSha":"bc6749cc3b5ae4a5e88a6cc2d5b3bebbe354eae6","analyzedAt":"2026-08-14T22:01:13.976Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}