diff --git a/flakehell/_logic/_discover.py b/flakehell/_logic/_discover.py index bc1e3e2..f10ba12 100644 --- a/flakehell/_logic/_discover.py +++ b/flakehell/_logic/_discover.py @@ -22,11 +22,11 @@ } -def get_installed(app) -> Iterator[Dict[str, Any]]: +def get_installed(app, argv=[]) -> Iterator[Dict[str, Any]]: plugins_codes = defaultdict(list) versions = dict() - app.initialize([]) + app.initialize(argv) codes: Iterable[str] for check_type in ('ast_plugins', 'logical_line_plugins', 'physical_line_plugins'): diff --git a/flakehell/_patched/_app.py b/flakehell/_patched/_app.py index 6bdceea..54e046b 100644 --- a/flakehell/_patched/_app.py +++ b/flakehell/_patched/_app.py @@ -1,9 +1,8 @@ # built-in import sys -from argparse import ArgumentParser from itertools import chain from pathlib import Path -from typing import Any, Dict, List, Optional, Tuple +from typing import Any, Dict, List # external from flake8.main.application import Application @@ -57,35 +56,17 @@ def option_manager(self, manager): group.add_argument('--safe', action='store_true', help='suppress exceptions from plugins') self._option_manager = manager - def get_toml_config(self, path: Path = None) -> Dict[str, Any]: + def get_toml_config(self, path: Path = None, additional_bases: List[Path] = []) -> Dict[str, Any]: if path is not None: - return read_config(path) + return read_config(*additional_bases, path) # lookup for config from current dir up to root root = Path().resolve() for dir_path in chain([root], root.parents): path = dir_path / 'pyproject.toml' if path.exists(): - return read_config(path) + return read_config(*additional_bases, path) return dict() - @staticmethod - def extract_toml_config_path(argv: List[str]) -> Tuple[Optional[Path], List[str]]: - if not argv: - return None, argv - - if '--help' in argv: - argv = argv.copy() - argv.remove('--help') - if not argv: - return None, ['--help'] - - parser = ArgumentParser() - parser.add_argument('--config') - known, unknown = parser.parse_known_args(argv) - if known.config and known.config.endswith('.toml'): - return Path(known.config).expanduser(), unknown - return None, argv - def parse_configuration_and_cli(self, config_finder, argv: List[str]) -> None: parser = self.option_manager.parser for action in parser._actions.copy(): @@ -96,9 +77,16 @@ def parse_configuration_and_cli(self, config_finder, argv: List[str]) -> None: continue parser._handle_conflict_resolve(None, [(name, action)]) - # if passed `--config` with path to TOML-config, we should extract it - # before passing into flake8 mechanisms - config_path, argv = self.extract_toml_config_path(argv=argv) + # Read --config file + config_path = None + if config_finder.config_file and config_finder.config_file.endswith('.toml'): + config_path = Path(config_finder.config_file).expanduser() + + # Read --append-config files and treat them as additional bases + additional_bases = [] + for extra_config_file in config_finder.extra_config_files: + if extra_config_file.endswith('.toml'): + additional_bases.append(Path(extra_config_file).expanduser()) # make default config config, _ = self.option_manager.parse_args([]) @@ -107,7 +95,7 @@ def parse_configuration_and_cli(self, config_finder, argv: List[str]) -> None: # patch config wtih TOML # If config is explicilty passed, it will be used # If config isn't specified, flakehell will lookup for it - config.__dict__.update(self.get_toml_config(config_path)) + config.__dict__.update(self.get_toml_config(config_path, additional_bases)) # Parse CLI options and legacy flake8 configs. # Based on `aggregate_options`. diff --git a/flakehell/commands/_code.py b/flakehell/commands/_code.py index 00ac1d8..041323b 100644 --- a/flakehell/commands/_code.py +++ b/flakehell/commands/_code.py @@ -13,18 +13,16 @@ def code_command(argv) -> CommandResult: """ if not argv: return ExitCode.NO_PLUGIN_NAME, 'no plugin name provided' - if argv[0] == '--help': + if '--help' in argv: print(code_command.__doc__) return ExitCode.OK, '' - if len(argv) > 1: - return ExitCode.TOO_MANY_ARGS, 'the command accept only one argument' - code = argv[0] app = FlakeHellApplication(program=NAME, version=VERSION) - plugins = sorted(get_installed(app=app), key=lambda p: p['name']) + plugins = sorted(get_installed(app=app, argv=argv), key=lambda p: p['name']) if not plugins: return ExitCode.NO_PLUGINS_INSTALLED, 'no plugins installed' + code = app.args[0] messages = [] checked = set() for plugin in plugins: diff --git a/flakehell/commands/_missed.py b/flakehell/commands/_missed.py index b0efe17..dd20bcf 100644 --- a/flakehell/commands/_missed.py +++ b/flakehell/commands/_missed.py @@ -11,11 +11,9 @@ def missed_command(argv) -> CommandResult: if argv and argv[0] == '--help': print(missed_command.__doc__) return ExitCode.OK, '' - if argv: - return ExitCode.TOO_MANY_ARGS, 'the command does not accept arguments' app = FlakeHellApplication(program=NAME, version=VERSION) - installed_plugins = sorted(get_installed(app=app), key=lambda p: p['name']) + installed_plugins = sorted(get_installed(app=app, argv=argv), key=lambda p: p['name']) if not installed_plugins: return ExitCode.NO_PLUGINS_INSTALLED, 'no plugins installed' diff --git a/flakehell/commands/_plugins.py b/flakehell/commands/_plugins.py index c7b6734..db56d04 100644 --- a/flakehell/commands/_plugins.py +++ b/flakehell/commands/_plugins.py @@ -12,7 +12,7 @@ def plugins_command(argv) -> CommandResult: """Show all installed plugins, their codes prefix, and matched rules from config. """ app = FlakeHellApplication(program=NAME, version=VERSION) - plugins = sorted(get_installed(app=app), key=lambda p: p['name']) + plugins = sorted(get_installed(app=app, argv=argv), key=lambda p: p['name']) if not plugins: return ExitCode.NO_PLUGINS_INSTALLED, 'no plugins installed'