{"record":{"id":"cdfdfaca9e16351c","repo":"python/cpython","slug":"nargs-for-store-actions-must-be-0-if-you-have","errorCode":null,"errorMessage":"nargs for store actions must be != 0; if you have nothing to store, actions such as store true or store const may be more appropriate","messagePattern":"nargs for store actions must be != 0; if you have nothing to store, actions such as store true or store const may be more appropriate","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"Lib/argparse.py","lineNumber":1111,"sourceCode":"        return ' | '.join(self.option_strings)\n\n\nclass _StoreAction(Action):\n\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):","sourceCodeStart":1093,"sourceCodeEnd":1129,"githubUrl":"https://github.com/python/cpython/blob/bc6749cc3b5ae4a5e88a6cc2d5b3bebbe354eae6/Lib/argparse.py#L1093-L1129","documentation":"_StoreAction (the default action for add_argument with a value) rejects nargs=0 at construction: an argument that stores nothing is contradictory, and argparse points you at store_true/store_const which are designed for zero-argument flags. The check fires when the parser is built, before any parsing.","triggerScenarios":"parser.add_argument('--flag', nargs=0); programmatically passing a computed nargs that evaluates to 0; copying nargs from another argument without adjusting it.","commonSituations":"Generated CLIs where a count value of 0 becomes nargs=0; confusion between nargs and type (nargs=0 is never valid for storing); refactors that move a flag from store_true to the default action.","solutions":["For a flag with no value, use action='store_true' (or 'store_false', 'store_const').","If the argument takes exactly one value, omit nargs entirely.","Guard generated nargs values: skip or default to 1 when the computed count is 0."],"exampleFix":"# before\nparser.add_argument('--verbose', nargs=0)  # ValueError: nargs for store actions must be != 0\n\n# after\nparser.add_argument('--verbose', action='store_true')","handlingStrategy":"validation","validationCode":"def normalize_nargs(nargs):\n    if nargs == 0:\n        raise ValueError('nargs=0 is invalid for store actions; use store_true')\n    return nargs","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Use action='store_true' for valueless flags","Omit nargs for single-value arguments","Skip or clamp computed nargs of 0 in generated CLIs"],"tags":["python","argparse","cli","nargs","parser-construction"],"backgroundTag":null,"analyzedSha":"bc6749cc3b5ae4a5e88a6cc2d5b3bebbe354eae6","analyzedAt":"2026-08-14T22:01:13.976Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}