From 02bcbee245b9b5a0c75345ada0f1d7e70d19b3c0 Mon Sep 17 00:00:00 2001 From: Ian Cordasco Date: Mon, 6 Jun 2016 19:46:29 -0500 Subject: [PATCH] Add --stdin-display-name to modify report output --- docs/source/user/options.rst | 24 ++++++++++++++++++++++++ flake8/main/cli.py | 7 +++++++ flake8/style_guide.py | 2 ++ 3 files changed, 33 insertions(+) diff --git a/docs/source/user/options.rst b/docs/source/user/options.rst index ce30bdf..e336daa 100644 --- a/docs/source/user/options.rst +++ b/docs/source/user/options.rst @@ -192,6 +192,30 @@ another-example*.py +.. option:: --stdin-display-name= + + Provide the name to use to report warnings and errors from code on stdin. + + Instead of reporting an error as something like: + + .. code:: + + stdin:82:73 E501 line too long + + You can specify this option to have it report whatever value you want + instead of stdin. + + This defaults to: ``stdin`` + + Command-line example: + + .. prompt:: bash + + cat file.py | flake8 --stdin-display-name=file.py - + + This **can not** be specified in config files. + + .. option:: --format= Select the formatter used to display errors to the user. diff --git a/flake8/main/cli.py b/flake8/main/cli.py index b426e1e..0b50e27 100644 --- a/flake8/main/cli.py +++ b/flake8/main/cli.py @@ -86,6 +86,13 @@ def register_default_options(option_manager): 'separated list. (Default: %default)', ) + add_option( + '--stdin-display-name', default='stdin', + help='The name used when reporting errors from code passed via stdin.' + ' This is useful for editors piping the file contents to flake8.' + ' (Default: %default)', + ) + # TODO(sigmavirus24): Figure out --first/--repeat add_option( diff --git a/flake8/style_guide.py b/flake8/style_guide.py index 57d86b2..6be9ff6 100644 --- a/flake8/style_guide.py +++ b/flake8/style_guide.py @@ -237,6 +237,8 @@ class StyleGuide(object): """Handle an error reported by a check.""" error = Error(code, filename, line_number, column_number, text, physical_line) + if error.filename is None or error.filename == '-': + error = error._replace(filename=self.options.stdin_display_name) error_is_selected = (self.should_report_error(error.code) is Decision.Selected) is_not_inline_ignored = self.is_inline_ignored(error) is False