Skip to content

Commit

Permalink
Made formatting cli option more consistent (#1404)
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
ssbarnea authored Feb 22, 2021
1 parent 1b9e21e commit 08264af
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 5 deletions.
4 changes: 2 additions & 2 deletions src/ansiblelint/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions src/ansiblelint/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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',
Expand Down
8 changes: 7 additions & 1 deletion src/ansiblelint/formatters/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

import rich

from ansiblelint.config import options

if TYPE_CHECKING:
from ansiblelint.errors import MatchError

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 08264af

Please sign in to comment.