diff --git a/docs/usage.rst b/docs/usage.rst index dd7dd37a..8b33302e 100644 --- a/docs/usage.rst +++ b/docs/usage.rst @@ -9,6 +9,7 @@ Current usage of ``pydocstringformatter``: [--exit-code] [--max-summary-lines int] [--summary-quotes-same-line] [--max-line-length int] + [--style {pep257} [{pep257} ...]] [--strip-whitespaces --no-strip-whitespaces] [--split-summary-body --no-split-summary-body] [--linewrap-full-docstring --no-linewrap-full-docstring] @@ -44,6 +45,9 @@ Current usage of ``pydocstringformatter``: is enforced for single line docstrings. --max-line-length int Maximum line length of docstrings. + --style {pep257} [{pep257} ...] + Docstring styles that are used in the project. Can be + more than one. default formatters: these formatters are turned on by default diff --git a/pydocstringformatter/_configuration/arguments_manager.py b/pydocstringformatter/_configuration/arguments_manager.py index db096887..e58a7439 100644 --- a/pydocstringformatter/_configuration/arguments_manager.py +++ b/pydocstringformatter/_configuration/arguments_manager.py @@ -118,6 +118,15 @@ def register_arguments(self, version: str) -> None: metavar="int", ) + self.configuration_group.add_argument( + "--style", + action="extend", + type=str, + nargs="+", + choices=["pep257"], + help="Docstring styles that are used in the project. Can be more than one.", + ) + def parse_options( self, argv: list[str], @@ -125,7 +134,7 @@ def parse_options( """Load all default option values. The order of parsing is: - 1. configuration files, 2. command line arguments. + 1. configuration files, 2. command line arguments, 3. set default values. """ # pylint: disable=protected-access toml_parsing.parse_toml_file(self.parser, self.namespace) @@ -134,6 +143,11 @@ def parse_options( self.parser, self.namespace, argv ) + # 'style' uses the 'extend' action. If we use a normal default value, + # and the default gets supplied as well style == ["pep257", "pep257"]. + if self.namespace.style is None: + self.namespace.style = ["pep257"] + def print_help(self) -> None: """Print the help or usage message.""" self.parser.print_help() diff --git a/tests/test_config.py b/tests/test_config.py index 985f5635..1c177ade 100644 --- a/tests/test_config.py +++ b/tests/test_config.py @@ -7,6 +7,7 @@ import pydocstringformatter from pydocstringformatter._utils import exceptions +from pydocstringformatter.run import _Run HERE = Path(__file__) CONFIG_DATA = HERE.parent / "data" / "config" @@ -225,3 +226,27 @@ def test_quiet_argument(capsys: pytest.CaptureFixture[str], test_file: str) -> N output = capsys.readouterr() assert not output.out assert not output.err + + +class TestStyleOption: + """Tests for the --style option.""" + + def test_style_default(self, test_file: str) -> None: + """Test that the default value of --style is pep257.""" + run = _Run([test_file]) + assert run.config.style == ["pep257"] + + def test_style_pep257(self, test_file: str) -> None: + """Test that we don't duplicate the default value if we pass it again.""" + run = _Run([test_file, "--style", "pep257"]) + assert run.config.style == ["pep257"] + + def test_style_invalid_choice(self, capsys: pytest.CaptureFixture[str]) -> None: + """Test that we correctly reject invalid styles.""" + with pytest.raises(SystemExit) as excinfo: + _Run(["--style", "invalid"]) + assert excinfo.value.code == 2 + + output = capsys.readouterr() + assert not output.out + assert "--style: invalid choice: 'invalid'" in output.err