mirror of
https://fuchsia.googlesource.com/third_party/github.com/pylint-dev/pylint
synced 2024-09-21 16:19:21 +00:00
65 lines
2.1 KiB
Python
65 lines
2.1 KiB
Python
# Licensed under the GPL: https://www.gnu.org/licenses/old-licenses/gpl-2.0.html
|
|
# For details: https://github.com/PyCQA/pylint/blob/main/LICENSE
|
|
# Copyright (c) https://github.com/PyCQA/pylint/blob/main/CONTRIBUTORS.txt
|
|
|
|
"""Utils for arguments/options parsing and handling."""
|
|
|
|
|
|
from typing import Any, Dict, Union
|
|
|
|
from pylint.config.argument import _Argument, _StoreTrueArgument
|
|
|
|
IMPLEMENTED_OPTDICT_KEYS = {"action", "default", "type", "choices", "help", "metavar"}
|
|
"""This is used to track our progress on accepting all optdict keys."""
|
|
|
|
|
|
def _convert_option_to_argument(
|
|
opt: str, optdict: Dict[str, Any]
|
|
) -> Union[_Argument, _StoreTrueArgument]:
|
|
"""Convert an optdict to an Argument class instance."""
|
|
# See if the optdict contains any keys we don't yet implement
|
|
# pylint: disable-next=fixme
|
|
# TODO: This should be removed once the migration to argparse is finished
|
|
for key, value in optdict.items():
|
|
if key not in IMPLEMENTED_OPTDICT_KEYS:
|
|
print("Unhandled key found in Argument creation:", key) # pragma: no cover
|
|
print("It's value is:", value) # pragma: no cover
|
|
|
|
# Get the long and short flags
|
|
flags = [f"--{opt}"]
|
|
if "short" in optdict:
|
|
flags += [f"-{optdict['short']}"]
|
|
|
|
# Get the action type
|
|
action = optdict.get("action", "store")
|
|
|
|
# pylint: disable-next=fixme
|
|
# TODO: Remove this handling after we have deprecated multiple-choice arguments
|
|
choices = optdict.get("choices", None)
|
|
if opt == "confidence":
|
|
choices = None
|
|
|
|
if action == "store_true":
|
|
return _StoreTrueArgument(
|
|
flags=flags,
|
|
action=action,
|
|
default=optdict["default"],
|
|
arg_help=optdict["help"],
|
|
)
|
|
return _Argument(
|
|
flags=flags,
|
|
action=action,
|
|
default=optdict["default"],
|
|
arg_type=optdict["type"],
|
|
choices=choices,
|
|
arg_help=optdict["help"],
|
|
metavar=optdict["metavar"],
|
|
)
|
|
|
|
|
|
def _parse_rich_type_value(value: Any) -> str:
|
|
"""Parse rich (toml) types into strings."""
|
|
if isinstance(value, (list, tuple)):
|
|
return ",".join(value)
|
|
return str(value)
|