summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorEric N. Vander Weele <ericvw@gmail.com>2019-08-28 16:20:38 -0400
committerEric N. Vander Weele <ericvw@gmail.com>2019-08-29 16:06:02 -0400
commitcf4bc53c127d7e6c29f69fcd0a1920d0aa88c023 (patch)
tree442ed06e7fc4bf4e90449f73e74d878b93bf8542 /src
parent7708e5b4abca1c9437caf59907a10753a6a5b1c9 (diff)
downloadflake8-cf4bc53c127d7e6c29f69fcd0a1920d0aa88c023.tar.gz
Hoist passing through sys.argv at the CLI layer
`flake8.main.cli.main()` is the primary entry point for the command-line implementation of flake8 (invoked via `__main__` or `console_scripts`). Therefore, it is reasonable for the entry point to be responsible for obtaining command line arguments from `sys.argv` there. Note that `sys.argv[1:]` is necessary in order to strip off the script name. Formerly, this was not needed in `Application.parse_preliminary_options_and_args()`, which was using `sys.argv[:]` because the result of the argument parsing was just for determining additional configuration to be loaded. Then, the *real* CLI argument parsing was forwarding the original `None` value to `argparse.ArgumentParser.parse_args()`, which internally was obtaining arguments as `sys.argv[1:]`. Additionally, the contract for various argument parsing methods to be guaranteed a `List[str]`.
Diffstat (limited to 'src')
-rw-r--r--src/flake8/main/application.py18
-rw-r--r--src/flake8/main/cli.py4
2 files changed, 13 insertions, 9 deletions
diff --git a/src/flake8/main/application.py b/src/flake8/main/application.py
index 6e5373f..6393dad 100644
--- a/src/flake8/main/application.py
+++ b/src/flake8/main/application.py
@@ -97,8 +97,8 @@ class Application(object):
#: The parsed diff information
self.parsed_diff = {} # type: Dict[str, Set[int]]
- def parse_preliminary_options_and_args(self, argv=None):
- # type: (Optional[List[str]]) -> None
+ def parse_preliminary_options_and_args(self, argv):
+ # type: (List[str]) -> None
"""Get preliminary options and args from CLI, pre-plugin-loading.
We need to know the values of a few standard options and args now, so
@@ -121,7 +121,7 @@ class Application(object):
# 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 if argv is not None else sys.argv)[:]
+ args = argv[:]
try:
args.remove("--version")
except ValueError:
@@ -136,8 +136,8 @@ class Application(object):
pass
opts, args = self.option_manager.parse_known_args(args)
- # parse_known_args includes program name and unknown options as args
- args = [a for a in args[1:] if not a.startswith("-")]
+ # parse_known_args includes unknown options as args
+ args = [a for a in args if not a.startswith("-")]
self.prelim_opts, self.prelim_args = opts, args
def exit(self):
@@ -344,7 +344,7 @@ class Application(object):
self.formatter.show_statistics(self.guide.stats)
def initialize(self, argv):
- # type: (Optional[List[str]]) -> None
+ # type: (List[str]) -> None
"""Initialize the application to be run.
This finds the plugins, registers their options, and parses the
@@ -373,13 +373,13 @@ class Application(object):
self.formatter.stop()
def _run(self, argv):
- # type: (Optional[List[str]]) -> None
+ # type: (List[str]) -> None
self.initialize(argv)
self.run_checks()
self.report()
- def run(self, argv=None):
- # type: (Optional[List[str]]) -> None
+ def run(self, argv):
+ # type: (List[str]) -> None
"""Run our application.
This method will also handle KeyboardInterrupt exceptions for the
diff --git a/src/flake8/main/cli.py b/src/flake8/main/cli.py
index 53f31e9..1bc9155 100644
--- a/src/flake8/main/cli.py
+++ b/src/flake8/main/cli.py
@@ -1,4 +1,5 @@
"""Command-line implementation of flake8."""
+import sys
from typing import List, Optional
from flake8.main import application
@@ -14,6 +15,9 @@ def main(argv=None):
:param list argv:
The arguments to be passed to the application for parsing.
"""
+ if argv is None:
+ argv = sys.argv[1:]
+
app = application.Application()
app.run(argv)
app.exit()