application: Register preliminary options on a separate argument parser

We introduce a new `ArgumentParser` for registering the preliminary
options to be inherited by the `Application.option_manager`.  The next
step will be to use the `Application.prelim_arg_parser` for parsing and
handling preliminary options and arguments.

Note that we prevent the preliminary parser from handling `-h/--help`
and defer to that to the primary parser.
This commit is contained in:
Eric N. Vander Weele 2019-10-25 15:01:30 -04:00
parent 1d7558f7da
commit a90200353e
3 changed files with 20 additions and 10 deletions

View file

@ -45,12 +45,17 @@ class Application(object):
self.program = program self.program = program
#: The version of the program being run #: The version of the program being run
self.version = version self.version = version
#: The prelimary argument parser for handling options required for
#: obtaining and parsing the configuration file.
self.prelim_arg_parser = argparse.ArgumentParser(add_help=False)
options.register_preliminary_options(self.prelim_arg_parser)
#: The instance of :class:`flake8.options.manager.OptionManager` used #: The instance of :class:`flake8.options.manager.OptionManager` used
#: to parse and handle the options and arguments passed by the user #: to parse and handle the options and arguments passed by the user
self.option_manager = manager.OptionManager( self.option_manager = manager.OptionManager(
prog="flake8", version=flake8.__version__ prog="flake8",
version=flake8.__version__,
parents=[self.prelim_arg_parser],
) )
options.register_preliminary_options(self.option_manager)
options.register_default_options(self.option_manager) options.register_default_options(self.option_manager)
#: The instance of :class:`flake8.options.config.ConfigFileFinder` #: The instance of :class:`flake8.options.config.ConfigFileFinder`
self.config_finder = None # type: config.ConfigFileFinder self.config_finder = None # type: config.ConfigFileFinder

View file

@ -1,4 +1,5 @@
"""Contains the logic for all of the default options for Flake8.""" """Contains the logic for all of the default options for Flake8."""
import argparse
import functools import functools
from flake8 import defaults from flake8 import defaults
@ -6,7 +7,8 @@ from flake8.main import debug
from flake8.main import vcs from flake8.main import vcs
def register_preliminary_options(option_manager): def register_preliminary_options(parser):
# type: (argparse.ArgumentParser) -> None
"""Register the preliminary options on our OptionManager. """Register the preliminary options on our OptionManager.
The preliminary options include: The preliminary options include:
@ -17,9 +19,9 @@ def register_preliminary_options(option_manager):
- ``--config`` - ``--config``
- ``--isolated`` - ``--isolated``
""" """
add_option = option_manager.add_option add_argument = parser.add_argument
add_option( add_argument(
"-v", "-v",
"--verbose", "--verbose",
default=0, default=0,
@ -29,13 +31,13 @@ def register_preliminary_options(option_manager):
"time it is repeated.", "time it is repeated.",
) )
add_option( add_argument(
"--output-file", default=None, help="Redirect report to a file." "--output-file", default=None, help="Redirect report to a file."
) )
# Config file options # Config file options
add_option( add_argument(
"--append-config", "--append-config",
action="append", action="append",
help="Provide extra config files to parse in addition to the files " help="Provide extra config files to parse in addition to the files "
@ -44,7 +46,7 @@ def register_preliminary_options(option_manager):
"provide the same option.", "provide the same option.",
) )
add_option( add_argument(
"--config", "--config",
default=None, default=None,
help="Path to the config file that will be the authoritative config " help="Path to the config file that will be the authoritative config "
@ -52,7 +54,7 @@ def register_preliminary_options(option_manager):
"configuration files.", "configuration files.",
) )
add_option( add_argument(
"--isolated", "--isolated",
default=False, default=False,
action="store_true", action="store_true",

View file

@ -1,4 +1,5 @@
"""Test aggregation of config files and command-line options.""" """Test aggregation of config files and command-line options."""
import argparse
import os import os
import pytest import pytest
@ -14,11 +15,13 @@ CLI_SPECIFIED_CONFIG = 'tests/fixtures/config_files/cli-specified.ini'
@pytest.fixture @pytest.fixture
def optmanager(): def optmanager():
"""Create a new OptionManager.""" """Create a new OptionManager."""
prelim_parser = argparse.ArgumentParser(add_help=False)
options.register_preliminary_options(prelim_parser)
option_manager = manager.OptionManager( option_manager = manager.OptionManager(
prog='flake8', prog='flake8',
version='3.0.0', version='3.0.0',
parents=[prelim_parser],
) )
options.register_preliminary_options(option_manager)
options.register_default_options(option_manager) options.register_default_options(option_manager)
return option_manager return option_manager