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]`.
This commit is contained in:
Eric N. Vander Weele 2019-08-28 16:20:38 -04:00
parent 7708e5b4ab
commit cf4bc53c12
3 changed files with 23 additions and 9 deletions

View file

@ -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

View file

@ -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()

View file

@ -126,3 +126,13 @@ def test_bug_report_successful(capsys):
out, err = capsys.readouterr()
assert json.loads(out)
assert err == ''
def test_obtaining_args_from_sys_argv_when_not_explicity_provided(capsys):
"""Test that arguments are obtained from 'sys.argv'."""
with mock.patch('sys.argv', ['flake8', '--help']):
_call_main(None)
out, err = capsys.readouterr()
assert out.startswith('usage: flake8 [options] file file ...\n')
assert err == ''