Skip to content

Commit c1e7de3

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 fa511dc commit c1e7de3

File tree

3 files changed

+37
-8
lines changed

3 files changed

+37
-8
lines changed

pydocstringformatter/run.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,7 @@ def __init__(self, argv: Union[List[str], None]) -> None:
2121
self.config = argparse.Namespace()
2222

2323
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-
24+
utils._parse_options(self.arg_parser, self.config, argv, FORMATTERS)
2725
self._check_files(self.config.files)
2826
else:
2927
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_default_formatters(
105+
parser: argparse.ArgumentParser,
106+
namespace: argparse.Namespace,
107+
formatters: List[Formatter],
108+
) -> None:
109+
"""Load the default 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_default_formatters(parser, namespace, formatters)
132+
133+
_parse_toml_file(parser, namespace)
134+
135+
_parse_command_line_arguments(parser, argv, namespace)

0 commit comments

Comments
 (0)