application: Return namespace and args from preliminary arg parsing

This is the initial step towards removing state from the `Application`
object during argument parsing and handling.  The goal is to remove
`Application.prelim_opts` and `Application.prelim_args`.
This commit is contained in:
Eric N. Vander Weele 2019-10-01 08:48:18 +02:00
parent 6bae5f7ef6
commit 6043e90855
2 changed files with 13 additions and 4 deletions

View file

@ -5,7 +5,7 @@ import argparse
import logging import logging
import sys import sys
import time import time
from typing import Dict, List, Optional, Set from typing import Dict, List, Optional, Set, Tuple
import flake8 import flake8
from flake8 import checker from flake8 import checker
@ -98,7 +98,7 @@ class Application(object):
self.parsed_diff = {} # type: Dict[str, Set[int]] self.parsed_diff = {} # type: Dict[str, Set[int]]
def parse_preliminary_options_and_args(self, argv): def parse_preliminary_options_and_args(self, argv):
# type: (List[str]) -> None # type: (List[str]) -> Tuple[argparse.Namespace, List[str]]
"""Get preliminary options and args from CLI, pre-plugin-loading. """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 We need to know the values of a few standard options and args now, so
@ -112,6 +112,10 @@ class Application(object):
:param list argv: :param list argv:
Command-line arguments passed in directly. Command-line arguments passed in directly.
:returns:
Populated namespace and list of remaining argument strings.
:rtype:
(argparse.Namespace, list)
""" """
# We haven't found or registered our plugins yet, so let's defer # We haven't found or registered our plugins yet, so let's defer
# printing the version until we aggregate options from config files # printing the version until we aggregate options from config files
@ -139,6 +143,7 @@ class Application(object):
# parse_known_args includes unknown options as args # parse_known_args includes unknown options as args
args = [a for a in args if not a.startswith("-")] args = [a for a in args if not a.startswith("-")]
self.prelim_opts, self.prelim_args = opts, args self.prelim_opts, self.prelim_args = opts, args
return opts, args
def exit(self): def exit(self):
# type: () -> None # type: () -> None

View file

@ -92,18 +92,22 @@ def test_returns_specified_plugin(application):
def test_prelim_opts_args(application): def test_prelim_opts_args(application):
"""Verify we get sensible prelim opts and args.""" """Verify we get sensible prelim opts and args."""
application.parse_preliminary_options_and_args( opts, args = application.parse_preliminary_options_and_args(
['flake8', '--foo', '--verbose', 'src', 'setup.py', '--statistics']) ['flake8', '--foo', '--verbose', 'src', 'setup.py', '--statistics'])
assert application.prelim_opts.statistics assert application.prelim_opts.statistics
assert application.prelim_opts.verbose assert application.prelim_opts.verbose
assert application.prelim_args == ['src', 'setup.py'] assert application.prelim_args == ['src', 'setup.py']
assert opts.statistics is application.prelim_opts.statistics
assert opts.verbose is application.prelim_opts.verbose
assert args is application.prelim_args
def test_prelim_opts_handles_empty(application): def test_prelim_opts_handles_empty(application):
"""Verify empty argv lists are handled correctly.""" """Verify empty argv lists are handled correctly."""
irrelevant_args = ['myexe', '/path/to/foo'] irrelevant_args = ['myexe', '/path/to/foo']
with mock.patch.object(sys, 'argv', irrelevant_args): with mock.patch.object(sys, 'argv', irrelevant_args):
application.parse_preliminary_options_and_args([]) opts, args = application.parse_preliminary_options_and_args([])
assert application.prelim_args == [] assert application.prelim_args == []
assert args is application.prelim_args