From d9c5b998d592716f83cc06f2f2ce02fa4f855514 Mon Sep 17 00:00:00 2001 From: Sorin Sbarnea Date: Mon, 22 Feb 2021 16:58:07 +0000 Subject: [PATCH] Made formatting cli option more consistent - Quiet formatter is not triggered with -f quiet - Allow combining -q with various formatters that can now change their behaviors. - When a calling with `-p -q`, we no longer print the full message --- src/ansiblelint/app.py | 4 ++-- src/ansiblelint/cli.py | 4 ++-- src/ansiblelint/formatters/__init__.py | 8 +++++++- 3 files changed, 11 insertions(+), 5 deletions(-) diff --git a/src/ansiblelint/app.py b/src/ansiblelint/app.py index f9831640f1..2d13564296 100644 --- a/src/ansiblelint/app.py +++ b/src/ansiblelint/app.py @@ -68,9 +68,9 @@ def choose_formatter_factory( ) -> Type[formatters.BaseFormatter]: """Select an output formatter based on the incoming command line arguments.""" r: Type[formatters.BaseFormatter] = formatters.Formatter - if options_list.quiet: + if options_list.format == 'quiet': r = formatters.QuietFormatter - elif options_list.parseable: + elif options_list.parseable or options_list.format == 'pep8': r = formatters.ParseableFormatter elif options_list.parseable_severity: r = formatters.ParseableSeverityFormatter diff --git a/src/ansiblelint/cli.py b/src/ansiblelint/cli.py index d7ccd50146..4434725dec 100644 --- a/src/ansiblelint/cli.py +++ b/src/ansiblelint/cli.py @@ -131,7 +131,7 @@ def get_cli_parser() -> argparse.ArgumentParser: '-f', dest='format', default='rich', - choices=['rich', 'plain', 'rst', 'codeclimate'], + choices=['rich', 'plain', 'rst', 'codeclimate', 'quiet', 'pep8'], help="Format used rules output, (default: %(default)s)", ) parser.add_argument( @@ -146,7 +146,7 @@ def get_cli_parser() -> argparse.ArgumentParser: dest='parseable', default=False, action='store_true', - help="parseable output in the format of pep8", + help="parseable output, same as '-f pep8'", ) parser.add_argument( '--parseable-severity', diff --git a/src/ansiblelint/formatters/__init__.py b/src/ansiblelint/formatters/__init__.py index 1e2d845ab9..e98bfed564 100644 --- a/src/ansiblelint/formatters/__init__.py +++ b/src/ansiblelint/formatters/__init__.py @@ -7,6 +7,8 @@ import rich +from ansiblelint.config import options + if TYPE_CHECKING: from ansiblelint.errors import MatchError @@ -84,8 +86,12 @@ class ParseableFormatter(BaseFormatter): def format(self, match: "MatchError") -> str: result = ( f"[filename]{self._format_path(match.filename or '')}[/]:{match.position}: " - f"[error_code]{match.rule.id}[/] [dim]{self.escape(match.message)}[/]" + f"[error_code]{match.rule.id}[/]" ) + + if not options.quiet: + result += f" [dim]{match.message}[/]" + if match.tag: result += f" [dim][error_code]({match.tag})[/][/]" return result