Skip to content

Commit 386d63d

Browse files
authored
Change the way rules are listed from command line (#2940
- rename plain formatting to brief - make brief implicit formatting for rule listing - rename rich formatting to full
1 parent f777905 commit 386d63d

File tree

7 files changed

+36
-16
lines changed

7 files changed

+36
-16
lines changed

.config/dictionary.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ PYTHONPYCACHEPREFIX
1919
REQPASS
2020
RULEDIRS
2121
RUNLEVEL
22+
Renderable
2223
Representer
2324
SRCROOT
2425
Sbarnea

src/ansiblelint/__main__.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -141,13 +141,15 @@ def _do_list(rules: RulesCollection) -> int:
141141
if options.list_rules:
142142

143143
_rule_format_map: dict[str, Callable[..., Any]] = {
144-
"plain": rules_as_str,
145-
"rich": rules_as_rich,
144+
"brief": rules_as_str,
145+
"full": rules_as_rich,
146146
"md": rules_as_md,
147147
"docs": rules_as_docs,
148148
}
149149

150-
console.print(_rule_format_map[options.format](rules), highlight=False)
150+
console.print(
151+
_rule_format_map.get(options.format, rules_as_str)(rules), highlight=False
152+
)
151153
return 0
152154

153155
if options.list_tags:

src/ansiblelint/cli.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ def get_cli_parser() -> argparse.ArgumentParser:
224224
default=False,
225225
action="store_true",
226226
help="List all the rules. For listing rules only the following formats "
227-
"for argument -f are supported: {plain, rich, md}",
227+
"for argument -f are supported: {brief, full, md} with 'brief' as default.",
228228
)
229229
listing_group.add_argument(
230230
"-T",
@@ -238,10 +238,11 @@ def get_cli_parser() -> argparse.ArgumentParser:
238238
"-f",
239239
"--format",
240240
dest="format",
241-
default="rich",
241+
default=None,
242242
choices=[
243-
"rich",
244-
"plain",
243+
"brief",
244+
# "plain",
245+
"full",
245246
"md",
246247
"json",
247248
"codeclimate",
@@ -526,10 +527,16 @@ def get_config(arguments: list[str]) -> Namespace:
526527
options = parser.parse_args(arguments)
527528

528529
# docs is not document, being used for internal documentation building
529-
if options.list_rules and options.format not in ["plain", "rich", "md", "docs"]:
530+
if options.list_rules and options.format not in [
531+
None,
532+
"brief",
533+
"full",
534+
"md",
535+
"docs",
536+
]:
530537
parser.error(
531538
f"argument -f: invalid choice: '{options.format}'. "
532-
f"In combination with argument -L only 'plain', "
539+
f"In combination with argument -L only 'brief', "
533540
f"'rich' or 'md' are supported with -f."
534541
)
535542

src/ansiblelint/config.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@
107107
cwd=".",
108108
display_relative_path=True,
109109
exclude_paths=[],
110-
format="rich",
110+
format="brief",
111111
lintables=[],
112112
list_rules=False,
113113
list_tags=False,

src/ansiblelint/generate_docs.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from typing import Iterable
55

66
from rich import box
7+
from rich.console import RenderableType
78

89
# Remove this compatibility try-catch block once we drop support for rich < 10.7.0
910
try:
@@ -70,9 +71,18 @@ def rules_as_docs(rules: RulesCollection) -> str:
7071
return "All markdown files for rules were dumped!"
7172

7273

73-
def rules_as_str(rules: RulesCollection) -> str:
74+
def rules_as_str(rules: RulesCollection) -> RenderableType:
7475
"""Return rules as string."""
75-
return "\n".join([str(rule) for rule in rules.alphabetical()])
76+
table = Table(show_header=False, header_style="title", box=box.SIMPLE)
77+
for rule in rules.alphabetical():
78+
if rule.tags:
79+
tag = f"[dim] ({', '.join(rule.tags)})[/dim]"
80+
else:
81+
tag = ""
82+
table.add_row(
83+
f"[link={RULE_DOC_URL}{rule.id}/]{rule.id}[/link]", rule.shortdesc + tag
84+
)
85+
return table
7686

7787

7888
def rules_as_md(rules: RulesCollection) -> str:

test/test_list_rules.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ def test_list_rules_includes_opt_in_rules() -> None:
2424
@pytest.mark.parametrize(
2525
("result", "returncode", "format_string"),
2626
(
27-
(False, 0, "plain"),
28-
(False, 0, "rich"),
27+
(False, 0, "brief"),
28+
(False, 0, "full"),
2929
(False, 0, "md"),
3030
(True, 2, "json"),
3131
(True, 2, "codeclimate"),
@@ -35,7 +35,7 @@ def test_list_rules_includes_opt_in_rules() -> None:
3535
),
3636
ids=(
3737
"plain",
38-
"rich",
38+
"full",
3939
"md",
4040
"json",
4141
"codeclimate",

test/test_rules_collection.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ def test_rich_rule_listing() -> None:
139139
descriptions in the console output.
140140
"""
141141
rules_path = os.path.abspath("./test/rules/fixtures")
142-
result = run_ansible_lint("-r", rules_path, "-f", "rich", "-L")
142+
result = run_ansible_lint("-r", rules_path, "-f", "full", "-L")
143143
assert result.returncode == 0
144144

145145
for rule in RulesCollection([rules_path]):

0 commit comments

Comments
 (0)