{"id":"349b234bfb0ad324","repo":"pypa/pip","slug":"value-for-option-must-be-one-of","errorCode":null,"errorMessage":"Value for option {} must be one of {}","messagePattern":"Value for option (.+?) must be one of (.+?)","errorType":"exception","errorClass":"OptionError","httpStatus":null,"severity":"error","filePath":"src/pip/_vendor/pygments/util.py","lineNumber":49,"sourceCode":"    \"\"\"Raised if one of the lookup functions didn't find a matching class.\"\"\"\n\n\nclass OptionError(Exception):\n    \"\"\"\n    This exception will be raised by all option processing functions if\n    the type or value of the argument is not correct.\n    \"\"\"\n\ndef get_choice_opt(options, optname, allowed, default=None, normcase=False):\n    \"\"\"\n    If the key `optname` from the dictionary is not in the sequence\n    `allowed`, raise an error, otherwise return it.\n    \"\"\"\n    string = options.get(optname, default)\n    if normcase:\n        string = string.lower()\n    if string not in allowed:\n        raise OptionError('Value for option {} must be one of {}'.format(optname, ', '.join(map(str, allowed))))\n    return string\n\n\ndef get_bool_opt(options, optname, default=None):\n    \"\"\"\n    Intuitively, this is `options.get(optname, default)`, but restricted to\n    Boolean value. The Booleans can be represented as string, in order to accept\n    Boolean value from the command line arguments. If the key `optname` is\n    present in the dictionary `options` and is not associated with a Boolean,\n    raise an `OptionError`. If it is absent, `default` is returned instead.\n\n    The valid string values for ``True`` are ``1``, ``yes``, ``true`` and\n    ``on``, the ones for ``False`` are ``0``, ``no``, ``false`` and ``off``\n    (matched case-insensitively).\n    \"\"\"\n    string = options.get(optname, default)\n    if isinstance(string, bool):\n        return string","sourceCodeStart":31,"sourceCodeEnd":67,"githubUrl":"https://github.com/pypa/pip/blob/d7d0d0a39494e28ec1c407bd0680e4a4d1067791/src/pip/_vendor/pygments/util.py#L31-L67","documentation":"Pygments' get_choice_opt raises OptionError when an option's value is not a member of the explicitly allowed set. It is used to validate enumerated/choice options passed in a lexer or formatter options dictionary, producing a message that lists both the option name and the allowed values.","triggerScenarios":"Calling get_choice_opt(options, optname, allowed) where options[optname] is a value not contained in the allowed sequence; for example passing style_name='native' to an option whose allowed set excludes it, or a formatter/lexer that forwards a user-supplied config dict to get_choice_opt.","commonSituations":"Passing an invalid string to a choice-style config key; a typo in an enumerated value read from a config file or CLI; version upgrades that rename or remove a previously-valid choice.","solutions":["Check the error message's allowed list and set options[optname] to one of those exact values.","If a default is acceptable, omit the key so get_choice_opt falls back to the provided default.","Validate the value against the allowed set before constructing the formatter/lexer."],"exampleFix":"# before\nopt = get_choice_opt(opts, 'encoding', ['utf-8', 'latin1'])  # opts['encoding']='ascii'\n\n# after\nopts['encoding'] = 'utf-8'\nopt = get_choice_opt(opts, 'encoding', ['utf-8', 'latin1'])","handlingStrategy":"validation","validationCode":"ALLOWED = ('utf-8', 'latin1')\nif opts.get('encoding') not in ALLOWED:\n    raise ValueError(f\"encoding must be one of {ALLOWED}\")\nget_choice_opt(opts, 'encoding', ALLOWED)","typeGuard":null,"tryCatchPattern":"from pip._vendor.pygments.util import OptionError\ntry:\n    val = get_choice_opt(opts, 'encoding', ['utf-8', 'latin1'])\nexcept OptionError as e:\n    opts['encoding'] = 'utf-8'\n    val = 'utf-8'","preventionTips":["Keep the allowed set in one constant shared by validation and the call.","Reject config values early at the boundary before they reach Pygments."],"tags":["pygments","config","validation"],"analyzedSha":"d7d0d0a39494e28ec1c407bd0680e4a4d1067791","analyzedAt":"2026-08-04T20:55:04.259Z","schemaVersion":2}