Command Line Interface

The command line interface of Flake8 is modeled as an application via Application. When a user runs flake8 at their command line, main() is run which handles management of the application.

User input is parsed twice to accommodate logging and verbosity options passed by the user as early as possible. This is so as much logging can be produced as possible.

The default Flake8 options are registered by register_default_options(). Trying to register these options in plugins will result in errors.

API Documentation

flake8.main.cli.main(argv: Optional[List[str]] = None)None[source]

Execute the main bit of the application.

This handles the creation of an instance of Application, runs it, and then exits the application.

Parameters

argv (list) – The arguments to be passed to the application for parsing.

class flake8.main.application.Application(program='flake8', version='3.7.9')[source]

Abstract our application into a class.

args: List[str]

The left over arguments that were not parsed by option_manager

catastrophic_failure

Whether or not something catastrophic happened and we should exit with a non-zero status code

check_plugins: plugin_manager.Checkers

The instance of flake8.plugins.manager.Checkers

config_finder

The instance of flake8.options.config.ConfigFileFinder

end_time: float

The timestamp when the Application finished reported errors.

exit()None[source]

Handle finalization and exiting the program.

This should be the last thing called on the application instance. It will check certain options and exit appropriately.

file_checker_manager: checker.Manager

The flake8.checker.Manager that will handle running all of the checks selected by the user.

find_plugins()None[source]

Find and load the plugins for this application.

If check_plugins, or formatting_plugins are None then this method will update them with the appropriate plugin manager instance. Given the expense of finding plugins (via entrypoints) we want this to be idempotent and so only update those attributes if they are None.

formatter: BaseFormatter

The user-selected formatter from formatting_plugins

formatter_for(formatter_plugin_name)[source]

Retrieve the formatter class by plugin name.

formatting_plugins: plugin_manager.ReportFormatters

The instance of flake8.plugins.manager.ReportFormatters

guide: style_guide.StyleGuideManager

The flake8.style_guide.StyleGuideManager built from the user’s options

initialize(argv: Optional[List[str]])None[source]

Initialize the application to be run.

This finds the plugins, registers their options, and parses the command-line arguments.

local_plugins: config.LocalPlugins

The flake8.options.config.LocalPlugins found in config

make_config_finder()[source]

Make our ConfigFileFinder based on preliminary opts and args.

make_file_checker_manager()None[source]

Initialize our FileChecker Manager.

make_formatter(formatter_class: Optional[Type[BaseFormatter]] = None)None[source]

Initialize a formatter based on the parsed options.

make_guide()None[source]

Initialize our StyleGuide.

option_manager

The instance of flake8.options.manager.OptionManager used to parse and handle the options and arguments passed by the user

options: optparse.Values

The user-supplied options parsed into an instance of optparse.Values

parse_configuration_and_cli(argv: Optional[List[str]] = None)None[source]

Parse configuration files and the CLI options.

Parameters

argv (list) – Command-line arguments passed in directly.

parse_preliminary_options_and_args(argv: Optional[List[str]] = None)None[source]

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 that we can find config files and configure logging.

Since plugins aren’t loaded yet, there may be some as-yet-unknown options; we ignore those for now, they’ll be parsed later when we do real option parsing.

Sets self.prelim_opts and self.prelim_args.

Parameters

argv (list) – Command-line arguments passed in directly.

parsed_diff: Dict[str, Set[int]]

The parsed diff information

prelim_args: List[str]

The preliminary arguments parsed from CLI before plugins are loaded

prelim_opts: optparse.Values

The preliminary options parsed from CLI before plugins are loaded, into a optparse.Values instance

program

The name of the program being run

register_plugin_options()None[source]

Register options provided by plugins to our option manager.

report()[source]

Report errors, statistics, and benchmarks.

report_benchmarks()[source]

Aggregate, calculate, and report benchmarks for this run.

report_errors()None[source]

Report all the errors found by flake8 3.0.

This also updates the result_count attribute with the total number of errors, warnings, and other messages found.

report_statistics()[source]

Aggregate and report statistics from this run.

result_count

The number of errors, warnings, and other messages after running flake8 and taking into account ignored errors and lines.

run(argv: Optional[List[str]] = None)None[source]

Run our application.

This method will also handle KeyboardInterrupt exceptions for the entirety of the flake8 application. If it sees a KeyboardInterrupt it will forcibly clean up the Manager.

run_checks(files: Optional[List[str]] = None)None[source]

Run the actual checks with the FileChecker Manager.

This method encapsulates the logic to make a Manger instance run the checks it is managing.

Parameters

files (list) – List of filenames to process

running_against_diff

Whether the program is processing a diff or not

start_time

The timestamp when the Application instance was instantiated.

total_result_count

The total number of errors before accounting for ignored errors and lines.

version

The version of the program being run

flake8.main.options.register_default_options(option_manager)[source]

Register the default options on our OptionManager.

The default options include:

  • -v/--verbose

  • -q/--quiet

  • --count

  • --diff

  • --exclude

  • --filename

  • --format

  • --hang-closing

  • --ignore

  • --extend-ignore

  • --per-file-ignores

  • --max-line-length

  • --max-doc-length

  • --select

  • --disable-noqa

  • --show-source

  • --statistics

  • --enable-extensions

  • --exit-zero

  • -j/--jobs

  • --output-file

  • --tee

  • --append-config

  • --config

  • --isolated

  • --benchmark

  • --bug-report