diff --git a/src/slobs_cli/__about__.py b/src/slobs_cli/__about__.py index 6da7d8e..9f2d52a 100644 --- a/src/slobs_cli/__about__.py +++ b/src/slobs_cli/__about__.py @@ -1,3 +1,3 @@ """module for package metadata.""" -__version__ = '0.11.2' +__version__ = '0.11.3' diff --git a/src/slobs_cli/cli.py b/src/slobs_cli/cli.py index d3c4441..374a561 100644 --- a/src/slobs_cli/cli.py +++ b/src/slobs_cli/cli.py @@ -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', diff --git a/src/slobs_cli/styles.py b/src/slobs_cli/styles.py index e85f23d..123988c 100644 --- a/src/slobs_cli/styles.py +++ b/src/slobs_cli/styles.py @@ -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)