Skip to content

Commit 9131888

Browse files
committed
Add a _StoreTrueArgument class
1 parent 19956be commit 9131888

3 files changed

Lines changed: 77 additions & 9 deletions

File tree

pylint/config/argument.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,3 +158,47 @@ def __init__(
158158
See:
159159
https://docs.python.org/3/library/argparse.html#metavar
160160
"""
161+
162+
163+
class _StoreTrueArgument:
164+
"""Class representing a 'store_true' argument to be passed by an argparse.ArgumentsParser.
165+
166+
This is based on the parameters passed to argparse.ArgumentsParser.add_message.
167+
See:
168+
https://docs.python.org/3/library/argparse.html#argparse.ArgumentParser.add_argument
169+
"""
170+
171+
def __init__(
172+
self,
173+
flags: List[str],
174+
action: str,
175+
default: _ArgumentTypes,
176+
choices: Optional[List[str]],
177+
arg_help: str,
178+
metavar: str,
179+
) -> None:
180+
self.flags = flags
181+
"""The name of the argument."""
182+
183+
self.action = action
184+
"""The action to perform with the argument."""
185+
186+
self.default = default
187+
"""The default value of the argument."""
188+
189+
self.choices = choices
190+
"""A list of possible choices for the argument.
191+
192+
None if there are no restrictions.
193+
"""
194+
195+
# argparse uses % formatting on help strings, so a % needs to be escaped
196+
self.help = arg_help.replace("%", "%%")
197+
"""The description of the argument."""
198+
199+
self.metavar = metavar
200+
"""The metavar of the argument.
201+
202+
See:
203+
https://docs.python.org/3/library/argparse.html#metavar
204+
"""

pylint/config/arguments_manager.py

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
"""Arguments manager class used to handle command-line arguments and options."""
66

77
import argparse
8-
from typing import TYPE_CHECKING, Dict, List
8+
from typing import TYPE_CHECKING, Dict, List, Union
99

10-
from pylint.config.argument import _Argument
10+
from pylint.config.argument import _Argument, _StoreTrueArgument
1111
from pylint.config.exceptions import UnrecognizedArgumentAction
1212
from pylint.config.utils import _convert_option_to_argument
1313

@@ -43,7 +43,9 @@ def _register_options_provider(self, provider: "checkers.BaseChecker") -> None:
4343
# TODO: Investigate performance impact of loading default arguments on every call
4444
self._load_default_argument_values()
4545

46-
def _add_arguments_to_parser(self, section: str, argument: _Argument) -> None:
46+
def _add_arguments_to_parser(
47+
self, section: str, argument: Union[_Argument, _StoreTrueArgument]
48+
) -> None:
4749
"""Iterates over all argument sections and add them to the parser object."""
4850
try:
4951
section_group = self._argument_groups_dict[section]
@@ -54,10 +56,11 @@ def _add_arguments_to_parser(self, section: str, argument: _Argument) -> None:
5456

5557
@staticmethod
5658
def _add_parser_option(
57-
section_group: argparse._ArgumentGroup, argument: _Argument
59+
section_group: argparse._ArgumentGroup,
60+
argument: Union[_Argument, _StoreTrueArgument],
5861
) -> None:
5962
"""Add an argument."""
60-
if argument.action == "store":
63+
if isinstance(argument, _Argument):
6164
section_group.add_argument(
6265
*argument.flags,
6366
action=argument.action,
@@ -67,6 +70,15 @@ def _add_parser_option(
6770
metavar=argument.metavar,
6871
choices=argument.choices,
6972
)
73+
elif isinstance(argument, _StoreTrueArgument):
74+
section_group.add_argument(
75+
*argument.flags,
76+
action=argument.action,
77+
default=argument.default,
78+
help=argument.help,
79+
metavar=argument.metavar,
80+
choices=argument.choices,
81+
)
7082
else:
7183
raise UnrecognizedArgumentAction
7284

pylint/config/utils.py

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,17 @@
55
"""Utils for arguments/options parsing and handling."""
66

77

8-
from typing import Any, Dict
8+
from typing import Any, Dict, Union
99

10-
from pylint.config.argument import _Argument
10+
from pylint.config.argument import _Argument, _StoreTrueArgument
1111

1212
IMPLEMENTED_OPTDICT_KEYS = {"action", "default", "type", "choices", "help", "metavar"}
1313
"""This is used to track our progress on accepting all optdict keys."""
1414

1515

16-
def _convert_option_to_argument(opt: str, optdict: Dict[str, Any]) -> _Argument:
16+
def _convert_option_to_argument(
17+
opt: str, optdict: Dict[str, Any]
18+
) -> Union[_Argument, _StoreTrueArgument]:
1719
"""Convert an optdict to an Argument class instance."""
1820
# See if the optdict contains any keys we don't yet implement
1921
# pylint: disable-next=fixme
@@ -23,9 +25,19 @@ def _convert_option_to_argument(opt: str, optdict: Dict[str, Any]) -> _Argument:
2325
print("Unhandled key found in Argument creation:", key) # pragma: no cover
2426
print("It's value is:", value) # pragma: no cover
2527

28+
action = optdict.get("action", "store")
29+
if action == "store_true":
30+
return _StoreTrueArgument(
31+
flags=[f"--{opt}"],
32+
action=action,
33+
default=optdict["default"],
34+
choices=optdict.get("choices", None),
35+
arg_help=optdict["help"],
36+
metavar=optdict["metavar"],
37+
)
2638
return _Argument(
2739
flags=[f"--{opt}"],
28-
action=optdict.get("action", "store"),
40+
action=action,
2941
default=optdict["default"],
3042
arg_type=optdict["type"],
3143
choices=optdict.get("choices", None),

0 commit comments

Comments
 (0)