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.""" """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 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.group()
@click.option( @click.option(
'-d', '-d',
@ -43,6 +52,7 @@ from .__about__ import __version__ as version
show_default=True, show_default=True,
show_envvar=True, show_envvar=True,
help='The style to use for output.', help='The style to use for output.',
callback=validate_style,
) )
@click.option( @click.option(
'-b', '-b',

View File

@ -3,15 +3,15 @@
import os import os
from dataclasses import dataclass from dataclasses import dataclass
_registry = {} registry = {}
def register_style(cls): def register_style(cls):
"""Register a style class.""" """Register a style class."""
key = cls.__name__.lower() key = cls.__name__.lower()
if key in _registry: if key in registry:
raise ValueError(f'Style {key} is already registered.') raise ValueError(f'Style {key} is already registered.')
_registry[key] = cls registry[key] = cls
return cls return cls
@ -19,12 +19,12 @@ def register_style(cls):
class Style: class Style:
"""Base class for styles.""" """Base class for styles."""
name: str = 'no_colour' name: str
border: str = 'none' border: str
header: str = 'none' header: str
cell: str = 'none' cell: str
highlight: str = 'none' highlight: str
warning: str = 'none' warning: str
no_border: bool = False no_border: bool = False
def __post_init__(self): def __post_init__(self):
@ -34,6 +34,24 @@ class Style:
self.border = None 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 @register_style
@dataclass @dataclass
class Red(Style): class Red(Style):
@ -192,8 +210,4 @@ class Black(Style):
def request_style_obj(style_name: str, no_border: bool) -> Style: def request_style_obj(style_name: str, no_border: bool) -> Style:
"""Request a style object by name.""" """Request a style object by name."""
key = style_name.lower() return registry[style_name.lower()](no_border=no_border)
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)