add --style validation

add Disabled class to style registry

patch bump
This commit is contained in:
onyx-and-iris 2025-06-22 12:36:29 +01:00
parent fe3a975ba3
commit 1c2d1abb2a
3 changed files with 39 additions and 15 deletions

View File

@ -1,3 +1,3 @@
"""module for package metadata."""
__version__ = '0.11.2'
__version__ = '0.11.3'

View File

@ -8,6 +8,15 @@ from . import styles
from .__about__ import __version__ as version
def validate_style(ctx: click.Context, param: click.Parameter, value: str) -> str:
"""Validate the style option."""
if value not in styles.registry:
raise click.BadParameter(
f"Invalid style '{value}'. Available styles: {', '.join(styles.registry.keys())}"
)
return value
@click.group()
@click.option(
'-d',
@ -43,6 +52,7 @@ from .__about__ import __version__ as version
show_default=True,
show_envvar=True,
help='The style to use for output.',
callback=validate_style,
)
@click.option(
'-b',

View File

@ -3,15 +3,15 @@
import os
from dataclasses import dataclass
_registry = {}
registry = {}
def register_style(cls):
"""Register a style class."""
key = cls.__name__.lower()
if key in _registry:
if key in registry:
raise ValueError(f'Style {key} is already registered.')
_registry[key] = cls
registry[key] = cls
return cls
@ -19,12 +19,12 @@ def register_style(cls):
class Style:
"""Base class for styles."""
name: str = 'no_colour'
border: str = 'none'
header: str = 'none'
cell: str = 'none'
highlight: str = 'none'
warning: str = 'none'
name: str
border: str
header: str
cell: str
highlight: str
warning: str
no_border: bool = False
def __post_init__(self):
@ -34,6 +34,24 @@ class Style:
self.border = None
@register_style
@dataclass
class Disabled(Style):
"""Disabled style."""
name: str = 'disabled'
header: str = ''
border: str = 'none'
cell: str = 'none'
highlight: str = 'none'
warning: str = 'none'
def __post__init__(self):
"""Post-initialization to set default values."""
super().__post_init__()
os.environ['NO_COLOR'] = '1'
@register_style
@dataclass
class Red(Style):
@ -192,8 +210,4 @@ class Black(Style):
def request_style_obj(style_name: str, no_border: bool) -> Style:
"""Request a style object by name."""
key = style_name.lower()
if key not in _registry:
os.environ['NO_COLOR'] = '1' # Disable colour output
return Style(no_border=no_border)
return _registry[key](no_border=no_border)
return registry[style_name.lower()](no_border=no_border)