Last active
August 30, 2023 17:51
-
-
Save claytonjroberts/4e7569e4938c943d6c4466abb52c43fa to your computer and use it in GitHub Desktop.
Python Typer Extensions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"""Extensions for the Typer CLI.""" | |
from typing import Optional | |
import typer | |
def command_category(command_category: str): | |
"""Modify the docstring of a function to mark it as a test command. | |
Requires the function to have a docstring and rich markdown enabled for proper rendering | |
""" | |
additional: Optional[str] = None | |
dict_categories = { | |
"test": "bold yellow", | |
"demo": "bold blue", | |
"management": "bold green", | |
"debug": "bold green", | |
} | |
if command_category.lower().strip() not in dict_categories: | |
raise ValueError(f"Unknown command category {command_category}") | |
else: | |
formatting = dict_categories[command_category] | |
additional = f"[{formatting}]\[{command_category.upper()}][/{formatting}] " | |
def decorator(func): | |
func.__doc__ = additional + func.__doc__ | |
return func | |
return decorator | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment