Skip to content

Commit c9251ff

Browse files
committed
refactor: ♻️ Use functional style
1 parent 52882f5 commit c9251ff

File tree

2 files changed

+33
-24
lines changed

2 files changed

+33
-24
lines changed

src/seedcase_flower/cli.py

Lines changed: 7 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,38 +4,21 @@
44
from typing import Any, Optional
55

66
from cyclopts import App, Parameter, config
7-
from cyclopts.annotations import get_hint_name
87
from cyclopts.help import ColumnSpec, DefaultFormatter, DescriptionRenderer
98

109
# from seedcase_flower.config import Config as FlowerConfig
11-
from seedcase_flower.internals import BuildStyle, _read_properties, _resolve_uri
12-
13-
14-
def names_renderer(entry):
15-
"""Massage the option flags in the help into a more readable format."""
16-
names = []
17-
if entry.names:
18-
for name in sorted(entry.names):
19-
if not name.startswith("-"):
20-
# Don't output redundant value placeholder for boolean flags
21-
if get_hint_name(entry.type) == "bool":
22-
name = ""
23-
else:
24-
# Matching the `dim` used by default in cyclopts for `choices` and
25-
# `defaults` in the description
26-
name = f"[dim]<{name}>[/dim]"
27-
else:
28-
name = f"[bold cyan]{name}[/bold cyan]"
29-
names.append(name)
30-
31-
return f"{' '.join(names)}".strip()
32-
10+
from seedcase_flower.internals import (
11+
BuildStyle,
12+
_format_param_help,
13+
_read_properties,
14+
_resolve_uri,
15+
)
3316

3417
app = App(
3518
help="Flower generates human-readable documentation from Data Packages.",
3619
help_formatter=DefaultFormatter(
3720
column_specs=(
38-
ColumnSpec(renderer=names_renderer),
21+
ColumnSpec(renderer=_format_param_help),
3922
ColumnSpec(renderer=DescriptionRenderer(newline_metadata=True)),
4023
)
4124
),

src/seedcase_flower/internals.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,13 @@
22

33
import json
44
from enum import Enum
5+
from itertools import repeat
56
from pathlib import Path
67
from typing import Any
78

9+
from cyclopts.annotations import get_hint_name
10+
from cyclopts.help import HelpEntry
11+
812

913
class BuildStyle(Enum):
1014
"""Built-in styles for outputting to file."""
@@ -26,3 +30,25 @@ def _read_properties(path: Path) -> dict[str, Any]:
2630
with open(path) as properties_file:
2731
datapackage: dict[str, Any] = json.load(properties_file)
2832
return datapackage
33+
34+
35+
def _format_param_help(entry: HelpEntry) -> str:
36+
"""Re-structure the parameter help into a more readable format."""
37+
if entry.names:
38+
names = map(_add_highlight_syntax, sorted(entry.names), repeat(entry.type))
39+
return f"{' '.join(names)}".strip()
40+
41+
42+
def _add_highlight_syntax(name: str, entry_type: type | None) -> str:
43+
"""Add markup character to highlight in colors, etc where desired."""
44+
if not name.startswith("-"):
45+
# Don't output redundant value placeholder for boolean flags
46+
if get_hint_name(entry_type) == "bool":
47+
name = ""
48+
else:
49+
# Matching the `dim` used by default in cyclopts for `choices` and
50+
# `defaults` in the description
51+
name = f"[dim]<{name}>[/dim]"
52+
else:
53+
name = f"[bold cyan]{name}[/bold cyan]"
54+
return name

0 commit comments

Comments
 (0)