Skip to content

Commit f739901

Browse files
DanielNoordPierre-Sassoulas
authored andcommitted
Refactor to avoid using a default value in argparse
See discussion here: #15 (comment) And here #21
1 parent 7976d68 commit f739901

File tree

4 files changed

+42
-12
lines changed

4 files changed

+42
-12
lines changed

.github/CONTRIBUTING.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ Use `pre-commit install` to install the pre-commit hook for the repository.
99
- Implement a Formatter by inheriting from `pydocstringformatter.formatting.Formatter`
1010
- Add your new formatter to `pydocstringformatter.formatting.FORMATTERS`
1111
- Write a clear docstring because this will be user-facing: it's what will be seen in
12-
the help message for the formatters command line option.
12+
the help message for the formatter's command line option.
1313
- Choose a proper name because this will be user-facing: the name will be used to turn
14-
the checker on and off via the command line or config files.
14+
the formatter on and off via the command line or config files.
1515

1616
### Testing
1717

pydocstringformatter/run.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,20 @@
99
from typing import List, Union
1010

1111
from pydocstringformatter import __version__, formatting, utils
12-
from pydocstringformatter.formatting import FORMATTERS
1312

1413

1514
class _Run:
1615
"""Main class that represent a run of the program."""
1716

1817
def __init__(self, argv: Union[List[str], None]) -> None:
1918
self.arg_parser = utils._register_arguments(__version__)
20-
utils._register_arguments_formatters(self.arg_parser, FORMATTERS)
19+
utils._register_arguments_formatters(self.arg_parser, formatting.FORMATTERS)
2120
self.config = argparse.Namespace()
2221

2322
if argv := argv or sys.argv[1:]:
24-
utils._parse_toml_file(self.arg_parser, self.config)
25-
utils._parse_command_line_arguments(self.arg_parser, argv, self.config)
26-
23+
utils._parse_options(
24+
self.arg_parser, self.config, argv, formatting.FORMATTERS
25+
)
2726
self._check_files(self.config.files)
2827
else:
2928
self.arg_parser.print_help()

pydocstringformatter/utils/__init__.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
from pydocstringformatter.utils.argument_parsing import (
2-
_parse_command_line_arguments,
3-
_parse_toml_file,
2+
_parse_options,
43
_register_arguments,
54
_register_arguments_formatters,
65
)
@@ -18,12 +17,11 @@
1817
"_find_python_files",
1918
"_generate_diff",
2019
"_is_docstring",
21-
"_parse_command_line_arguments",
22-
"_parse_toml_file",
2320
"ParsingError",
2421
"PydocstringFormatterError",
2522
"_register_arguments",
2623
"_register_arguments_formatters",
2724
"TomlParsingError",
25+
"_parse_options",
2826
"_print_to_console",
2927
]

pydocstringformatter/utils/argument_parsing.py

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ def _register_arguments_formatters(
5151
f"--{name}",
5252
action="store_true",
5353
dest=name,
54-
default=not formatter.optional,
5554
help=f"A{help_text} : {formatter.__doc__}",
5655
)
5756
parser.add_argument(
@@ -100,3 +99,37 @@ def _parse_toml_file(
10099
arguments += _parse_toml_option(key, value)
101100

102101
parser.parse_args(arguments, namespace)
102+
103+
104+
def _load_formatters_default_option(
105+
parser: argparse.ArgumentParser,
106+
namespace: argparse.Namespace,
107+
formatters: List[Formatter],
108+
) -> None:
109+
"""Load the list of formatters based on their 'optional' attribute."""
110+
arguments: List[str] = []
111+
for formatter in formatters:
112+
if formatter.optional:
113+
arguments.append(f"--no-{formatter.name}")
114+
elif not formatter.optional:
115+
arguments.append(f"--{formatter.name}")
116+
117+
parser.parse_known_args(arguments, namespace)
118+
119+
120+
def _parse_options(
121+
parser: argparse.ArgumentParser,
122+
namespace: argparse.Namespace,
123+
argv: List[str],
124+
formatters: List[Formatter],
125+
) -> None:
126+
"""Load all default option values.
127+
128+
The order of parsing is:
129+
1. default values, 2. configuration files, 3. command line arguments.
130+
"""
131+
_load_formatters_default_option(parser, namespace, formatters)
132+
133+
_parse_toml_file(parser, namespace)
134+
135+
_parse_command_line_arguments(parser, argv, namespace)

0 commit comments

Comments
 (0)