summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorAnthony Sottile <asottile@umich.edu>2019-10-31 21:05:07 +0000
committerAnthony Sottile <asottile@umich.edu>2019-10-31 21:05:07 +0000
commitb14d47b3561faa0e37b1f8f36c322c0508f2c21a (patch)
treefb5857395d19d4bae838aa777f861920dc2c58a4 /src
parent87acf9e5fcf559695e8fbb659caa68776cfb9403 (diff)
parent964b3a9c21997851ef1e3305854545229a1e2ec5 (diff)
downloadflake8-b14d47b3561faa0e37b1f8f36c322c0508f2c21a.tar.gz
Merge branch 'separate-prelim-options' into 'master'
Separate pre-configuration CLI parsing See merge request pycqa/flake8!364
Diffstat (limited to 'src')
-rw-r--r--src/flake8/api/legacy.py4
-rw-r--r--src/flake8/main/application.py39
-rw-r--r--src/flake8/main/options.py100
-rw-r--r--src/flake8/options/manager.py16
4 files changed, 81 insertions, 78 deletions
diff --git a/src/flake8/api/legacy.py b/src/flake8/api/legacy.py
index 1056fe9..e530088 100644
--- a/src/flake8/api/legacy.py
+++ b/src/flake8/api/legacy.py
@@ -28,14 +28,14 @@ def get_style_guide(**kwargs):
:class:`StyleGuide`
"""
application = app.Application()
- prelim_opts, prelim_args = application.parse_preliminary_options_and_args(
+ prelim_opts, remaining_args = application.parse_preliminary_options_and_args( # noqa: E501
[]
)
flake8.configure_logging(prelim_opts.verbose, prelim_opts.output_file)
application.make_config_finder(prelim_opts.append_config)
application.find_plugins(prelim_opts.config, prelim_opts.isolated)
application.register_plugin_options()
- application.parse_configuration_and_cli([])
+ application.parse_configuration_and_cli(remaining_args)
# We basically want application.initialize to be called but with these
# options set instead before we make our formatter, notifier, internal
# style guide and file checker manager.
diff --git a/src/flake8/main/application.py b/src/flake8/main/application.py
index 60b0c5c..9555202 100644
--- a/src/flake8/main/application.py
+++ b/src/flake8/main/application.py
@@ -45,10 +45,16 @@ class Application(object):
self.program = program
#: The version of the program being run
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
#: to parse and handle the options and arguments passed by the user
self.option_manager = manager.OptionManager(
- prog="flake8", version=flake8.__version__
+ prog="flake8",
+ version=flake8.__version__,
+ parents=[self.prelim_arg_parser],
)
options.register_default_options(self.option_manager)
#: The instance of :class:`flake8.options.config.ConfigFileFinder`
@@ -110,32 +116,7 @@ class Application(object):
:rtype:
(argparse.Namespace, list)
"""
- # We haven't found or registered our plugins yet, so let's defer
- # printing the version until we aggregate options from config files
- # and the command-line. First, let's clone our arguments on the CLI,
- # then we'll attempt to remove ``--version`` so that we can avoid
- # triggering the "version" action in argparse. If it's not there, we
- # do not need to worry and we can continue. If it is, we successfully
- # defer printing the version until just a little bit later.
- # Similarly we have to defer printing the help text until later.
- args = argv[:]
- try:
- args.remove("--version")
- except ValueError:
- pass
- try:
- args.remove("--help")
- except ValueError:
- pass
- try:
- args.remove("-h")
- except ValueError:
- pass
-
- opts, args = self.option_manager.parse_known_args(args)
- # parse_known_args includes unknown options as args
- args = [a for a in args if not a.startswith("-")]
- return opts, args
+ return self.prelim_arg_parser.parse_known_args(argv)
def exit(self):
# type: () -> None
@@ -357,14 +338,14 @@ class Application(object):
"""
# NOTE(sigmavirus24): When updating this, make sure you also update
# our legacy API calls to these same methods.
- prelim_opts, prelim_args = self.parse_preliminary_options_and_args(
+ prelim_opts, remaining_args = self.parse_preliminary_options_and_args(
argv
)
flake8.configure_logging(prelim_opts.verbose, prelim_opts.output_file)
self.make_config_finder(prelim_opts.append_config)
self.find_plugins(prelim_opts.config, prelim_opts.isolated)
self.register_plugin_options()
- self.parse_configuration_and_cli(argv)
+ self.parse_configuration_and_cli(remaining_args)
self.make_formatter()
self.make_guide()
self.make_file_checker_manager()
diff --git a/src/flake8/main/options.py b/src/flake8/main/options.py
index 101bd1a..ba1f1c2 100644
--- a/src/flake8/main/options.py
+++ b/src/flake8/main/options.py
@@ -1,4 +1,5 @@
"""Contains the logic for all of the default options for Flake8."""
+import argparse
import functools
from flake8 import defaults
@@ -6,12 +7,66 @@ from flake8.main import debug
from flake8.main import vcs
+def register_preliminary_options(parser):
+ # type: (argparse.ArgumentParser) -> None
+ """Register the preliminary options on our OptionManager.
+
+ The preliminary options include:
+
+ - ``-v``/``--verbose``
+ - ``--output-file``
+ - ``--append-config``
+ - ``--config``
+ - ``--isolated``
+ """
+ add_argument = parser.add_argument
+
+ add_argument(
+ "-v",
+ "--verbose",
+ default=0,
+ action="count",
+ help="Print more information about what is happening in flake8."
+ " This option is repeatable and will increase verbosity each "
+ "time it is repeated.",
+ )
+
+ add_argument(
+ "--output-file", default=None, help="Redirect report to a file."
+ )
+
+ # Config file options
+
+ add_argument(
+ "--append-config",
+ action="append",
+ help="Provide extra config files to parse in addition to the files "
+ "found by Flake8 by default. These files are the last ones read "
+ "and so they take the highest precedence when multiple files "
+ "provide the same option.",
+ )
+
+ add_argument(
+ "--config",
+ default=None,
+ help="Path to the config file that will be the authoritative config "
+ "source. This will cause Flake8 to ignore all other "
+ "configuration files.",
+ )
+
+ add_argument(
+ "--isolated",
+ default=False,
+ action="store_true",
+ help="Ignore all configuration files.",
+ )
+
+
def register_default_options(option_manager):
"""Register the default options on our OptionManager.
The default options include:
- - ``-v``/``--verbose``
- ``-q``/``--quiet``
- ``--count``
- ``--diff``
@@ -32,11 +87,7 @@ def register_default_options(option_manager):
- ``--enable-extensions``
- ``--exit-zero``
- ``-j``/``--jobs``
- - ``--output-file``
- ``--tee``
- - ``--append-config``
- - ``--config``
- - ``--isolated``
- ``--benchmark``
- ``--bug-report``
"""
@@ -44,15 +95,6 @@ def register_default_options(option_manager):
# pep8 options
add_option(
- "-v",
- "--verbose",
- default=0,
- action="count",
- help="Print more information about what is happening in flake8."
- " This option is repeatable and will increase verbosity each "
- "time it is repeated.",
- )
- add_option(
"-q",
"--quiet",
default=0,
@@ -258,10 +300,6 @@ def register_default_options(option_manager):
)
add_option(
- "--output-file", default=None, help="Redirect report to a file."
- )
-
- add_option(
"--tee",
default=False,
parse_from_config=True,
@@ -269,32 +307,6 @@ def register_default_options(option_manager):
help="Write to stdout and output-file.",
)
- # Config file options
-
- add_option(
- "--append-config",
- action="append",
- help="Provide extra config files to parse in addition to the files "
- "found by Flake8 by default. These files are the last ones read "
- "and so they take the highest precedence when multiple files "
- "provide the same option.",
- )
-
- add_option(
- "--config",
- default=None,
- help="Path to the config file that will be the authoritative config "
- "source. This will cause Flake8 to ignore all other "
- "configuration files.",
- )
-
- add_option(
- "--isolated",
- default=False,
- action="store_true",
- help="Ignore all configuration files.",
- )
-
# Benchmarking
add_option(
diff --git a/src/flake8/options/manager.py b/src/flake8/options/manager.py
index a7f678d..def4c96 100644
--- a/src/flake8/options/manager.py
+++ b/src/flake8/options/manager.py
@@ -338,8 +338,12 @@ class OptionManager(object):
"""Manage Options and OptionParser while adding post-processing."""
def __init__(
- self, prog, version, usage="%(prog)s [options] file file ..."
- ): # type: (str, str, str) -> None
+ self,
+ prog,
+ version,
+ usage="%(prog)s [options] file file ...",
+ parents=None,
+ ): # type: (str, str, str, Optional[List[argparse.ArgumentParser]]) -> None # noqa: E501
"""Initialize an instance of an OptionManager.
:param str prog:
@@ -348,9 +352,15 @@ class OptionManager(object):
Version string for the program.
:param str usage:
Basic usage string used by the OptionParser.
+ :param argparse.ArgumentParser parents:
+ A list of ArgumentParser objects whose arguments should also be
+ included.
"""
+ if parents is None:
+ parents = []
+
self.parser = argparse.ArgumentParser(
- prog=prog, usage=usage
+ prog=prog, usage=usage, parents=parents
) # type: argparse.ArgumentParser
self._current_group = None # type: Optional[argparse._ArgumentGroup]
self.version_action = cast(