Skip to content

Commit 2bd960a

Browse files
Add automatically generated options
1 parent 0429229 commit 2bd960a

File tree

6 files changed

+41
-10
lines changed

6 files changed

+41
-10
lines changed
Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
__all__ = ["FORMATTERS"]
22

3+
from typing import List
4+
5+
from pydocstringformatter.formatting.base import Formatter
36
from pydocstringformatter.formatting.formatter import (
47
BeginningQuotesFormatter,
58
ClosingQuotesFormatter,
69
)
710

8-
FORMATTERS = [
11+
FORMATTERS: List[Formatter] = [
912
BeginningQuotesFormatter(),
1013
ClosingQuotesFormatter(),
1114
]

pydocstringformatter/formatting/base.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
class Formatter:
66
"""Base class for docstring formatter"""
77

8-
optional: bool = False
8+
optional = False
99
name: str
1010

1111
@abc.abstractmethod

pydocstringformatter/run.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,17 @@
99
from typing import List, Union
1010

1111
from pydocstringformatter import __version__, formatting, utils
12+
from pydocstringformatter.formatting import FORMATTERS
1213

1314

1415
class _Run:
1516
"""Main class that represent a run of the program"""
1617

1718
def __init__(self, argv: Union[List[str], None]) -> None:
1819
self.arg_parser = utils._register_arguments(__version__)
20+
self.arg_parser = utils._register_arguments_formatters(
21+
self.arg_parser, FORMATTERS
22+
)
1923
self.config = argparse.Namespace()
2024

2125
if argv := argv or sys.argv[1:]:
@@ -46,10 +50,11 @@ def _format_file(self, filename: Path) -> bool:
4650

4751
for index, tokeninfo in enumerate(tokens):
4852
new_tokeninfo = tokeninfo
49-
53+
formatter_options = vars(self.config)
5054
if utils._is_docstring(new_tokeninfo, tokens[index - 1]):
5155
for formatter in formatting.FORMATTERS:
52-
new_tokeninfo = formatter.treat_token(new_tokeninfo)
56+
if formatter_options[formatter.name]:
57+
new_tokeninfo = formatter.treat_token(new_tokeninfo)
5358
changed_tokens.append(new_tokeninfo)
5459

5560
if tokeninfo != new_tokeninfo:

pydocstringformatter/utils/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
_parse_command_line_arguments,
33
_parse_toml_file,
44
_register_arguments,
5+
_register_arguments_formatters,
56
)
67
from pydocstringformatter.utils.exceptions import (
78
ParsingError,
@@ -20,4 +21,5 @@
2021
"ParsingError",
2122
"_parse_toml_file",
2223
"TomlParsingError",
24+
"_register_arguments_formatters",
2325
]

pydocstringformatter/utils/argument_parsing.py

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import tomli
66

7+
from pydocstringformatter.formatting.base import Formatter
78
from pydocstringformatter.utils.exceptions import TomlParsingError, UnrecognizedOption
89

910
OPTIONS_TYPES = {"write": "store_true"}
@@ -19,24 +20,40 @@ def _parse_command_line_arguments(
1920
def _register_arguments(version: str) -> argparse.ArgumentParser:
2021
"""Create an argument parser and add all supported arguments"""
2122
parser = argparse.ArgumentParser(prog="pydocstringformatter")
22-
2323
parser.add_argument("files", nargs="*", type=str)
24-
2524
parser.add_argument(
2625
"-w",
2726
"--write",
2827
action="store_true",
2928
help="Write the changes to file instead of printing the files to stdout",
3029
)
31-
3230
parser.add_argument(
3331
"-v",
3432
"--version",
3533
action="version",
3634
version=version,
3735
help="Show version number and exit",
3836
)
37+
return parser
38+
3939

40+
def _register_arguments_formatters(
41+
parser: argparse.ArgumentParser, formatters: List[Formatter]
42+
) -> argparse.ArgumentParser:
43+
"""Register a list of formatters, so they can all be deactivated or activated."""
44+
for formatter in formatters:
45+
name = formatter.name
46+
help_text = f"ctivate the {name} formatter"
47+
parser.add_argument(
48+
f"--{name}",
49+
action="store_true",
50+
dest=name,
51+
default=not formatter.optional,
52+
help=f"A{help_text} : {formatter.__doc__}",
53+
)
54+
parser.add_argument(
55+
f"--no-{name}", action="store_false", dest=name, help=f"Dea{help_text}"
56+
)
4057
return parser
4158

4259

tests/test_run.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,13 @@ def test_no_arguments(capsys: pytest.CaptureFixture[str]) -> None:
1212
"""Test that we warn when no arguments are provided"""
1313
sys.argv = ["pydocstringformatter"]
1414
pydocstringformatter.run_docstring_formatter()
15-
output = capsys.readouterr()
16-
assert output.out.startswith("usage: pydocstringformatter [-h]")
17-
assert not output.err
15+
out, err = capsys.readouterr()
16+
assert out.startswith("usage: pydocstringformatter [-h]")
17+
assert "--beginning-quotes" in out
18+
assert "Activate the beginning-quotes formatter" in out
19+
assert "--no-beginning-quotes" in out
20+
assert "Deactivate the beginning-quotes formatter" in out
21+
assert not err
1822

1923

2024
def test_sys_agv_as_arguments(

0 commit comments

Comments
 (0)