]>
Commit | Line | Data |
---|---|---|
53e6db90 DC |
1 | """Procedure for parsing args, config, loading plugins.""" |
2 | from __future__ import annotations | |
3 | ||
4 | import argparse | |
5 | from typing import Sequence | |
6 | ||
7 | import flake8 | |
8 | from flake8.main import options | |
9 | from flake8.options import aggregator | |
10 | from flake8.options import config | |
11 | from flake8.options import manager | |
12 | from flake8.plugins import finder | |
13 | ||
14 | ||
15 | def parse_args( | |
16 | argv: Sequence[str], | |
17 | ) -> tuple[finder.Plugins, argparse.Namespace]: | |
18 | """Procedure for parsing args, config, loading plugins.""" | |
19 | prelim_parser = options.stage1_arg_parser() | |
20 | ||
21 | args0, rest = prelim_parser.parse_known_args(argv) | |
22 | # XXX (ericvw): Special case "forwarding" the output file option so | |
23 | # that it can be reparsed again for the BaseFormatter.filename. | |
24 | if args0.output_file: | |
25 | rest.extend(("--output-file", args0.output_file)) | |
26 | ||
27 | flake8.configure_logging(args0.verbose, args0.output_file) | |
28 | ||
29 | cfg, cfg_dir = config.load_config( | |
30 | config=args0.config, | |
31 | extra=args0.append_config, | |
32 | isolated=args0.isolated, | |
33 | ) | |
34 | ||
35 | plugin_opts = finder.parse_plugin_options( | |
36 | cfg, | |
37 | cfg_dir, | |
38 | enable_extensions=args0.enable_extensions, | |
39 | require_plugins=args0.require_plugins, | |
40 | ) | |
41 | raw_plugins = finder.find_plugins(cfg, plugin_opts) | |
42 | plugins = finder.load_plugins(raw_plugins, plugin_opts) | |
43 | ||
44 | option_manager = manager.OptionManager( | |
45 | version=flake8.__version__, | |
46 | plugin_versions=plugins.versions_str(), | |
47 | parents=[prelim_parser], | |
48 | formatter_names=list(plugins.reporters), | |
49 | ) | |
50 | options.register_default_options(option_manager) | |
51 | option_manager.register_plugins(plugins) | |
52 | ||
53 | opts = aggregator.aggregate_options(option_manager, cfg, cfg_dir, rest) | |
54 | ||
55 | for loaded in plugins.all_plugins(): | |
56 | parse_options = getattr(loaded.obj, "parse_options", None) | |
57 | if parse_options is None: | |
58 | continue | |
59 | ||
60 | # XXX: ideally we wouldn't have two forms of parse_options | |
61 | try: | |
62 | parse_options( | |
63 | option_manager, | |
64 | opts, | |
65 | opts.filenames, | |
66 | ) | |
67 | except TypeError: | |
68 | parse_options(opts) | |
69 | ||
70 | return plugins, opts |