Skip to content
Open
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions src/seedcase_flower/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,25 @@
from typing import Any, Optional

from cyclopts import App, Parameter, config
from cyclopts.help import ColumnSpec, DefaultFormatter, DescriptionRenderer

# from seedcase_flower.config import Config as FlowerConfig
from seedcase_flower.internals import BuildStyle, _read_properties, _resolve_uri
from seedcase_flower.internals import (
BuildStyle,
_format_param_help,
_read_properties,
_resolve_uri,
)

app = App(
help="Flower generates human-readable documentation from Data Packages.",
default_parameter=Parameter(negative=()),
help_formatter=DefaultFormatter(
column_specs=(
ColumnSpec(renderer=_format_param_help),
ColumnSpec(renderer=DescriptionRenderer(newline_metadata=True)),
)
),
default_parameter=Parameter(negative=(), show_default=True),
config=[
config.Toml(
".flower.toml",
Expand Down
25 changes: 25 additions & 0 deletions src/seedcase_flower/internals.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@

import json
from enum import Enum
from itertools import repeat
from pathlib import Path
from typing import Any

from cyclopts.annotations import get_hint_name
from cyclopts.help import HelpEntry


class BuildStyle(Enum):
"""Built-in styles for outputting to file."""
Expand All @@ -26,3 +30,24 @@ def _read_properties(path: Path) -> dict[str, Any]:
with open(path) as properties_file:
datapackage: dict[str, Any] = json.load(properties_file)
return datapackage


def _format_param_help(entry: HelpEntry) -> str:
"""Re-structure the parameter help into a more readable format."""
# Sort to put the flag first (eg `--uri URI` instead of the default `URI --uri`)
names = map(_add_highlight_syntax, sorted(entry.names), repeat(entry.type))
return f"{' '.join(names)}".strip()


def _add_highlight_syntax(name: str, entry_type: type | None) -> str:
"""Add markup character to highlight in colors, etc where desired."""
formatted_name = f"[bold cyan]{name}[/bold cyan]"
if not name.startswith("-"):
# Matching the `dim` used by default in cyclopts for `choices` and
# `defaults` in the description
formatted_name = f"[dim]<{name}>[/dim]"

# Don't formatted_name redundant value placeholder for boolean flags
if get_hint_name(entry_type) == "bool":
formatted_name = ""
return formatted_name
Loading