mirror of
https://github.com/onyx-and-iris/vban-cli.git
synced 2026-04-16 02:23:30 +00:00
add new help formatters for different kinds of commands
This commit is contained in:
@@ -4,31 +4,24 @@ from cyclopts.help import DefaultFormatter, HelpPanel
|
||||
from rich.console import Console, ConsoleOptions
|
||||
|
||||
|
||||
class CustomHelpFormatter(DefaultFormatter):
|
||||
"""Custom help formatter that injects an index argument into the usage line and filters it out from the parameters list.
|
||||
|
||||
This formatter modifies the usage line to include an <index> argument after the 'strip' command,
|
||||
and filters out any parameters related to 'index' from the Parameters section of the help output.
|
||||
"""
|
||||
|
||||
def render_usage(self, console: Console, options: ConsoleOptions, usage) -> None:
|
||||
"""Render the usage line with index argument injected."""
|
||||
if usage:
|
||||
modified_usage = re.sub(
|
||||
r'(\S+\s+[a-z]+)\s+(COMMAND)', r'\1 <index> \2', str(usage)
|
||||
)
|
||||
console.print(f'[bold]Usage:[/bold] {modified_usage}')
|
||||
class BaseHelpFormatter(DefaultFormatter):
|
||||
"""Base help formatter that provides common functionality."""
|
||||
|
||||
def __call__(
|
||||
self, console: Console, options: ConsoleOptions, panel: HelpPanel
|
||||
) -> None:
|
||||
"""Render a help panel, filtering out the index parameter from Parameters sections."""
|
||||
"""Render a help panel, filtering out hidden parameters from Parameters sections."""
|
||||
if panel.title == 'Parameters':
|
||||
filtered_entries = [
|
||||
entry
|
||||
for entry in panel.entries
|
||||
if not (
|
||||
entry.names and any('index' in name.lower() for name in entry.names)
|
||||
entry.names
|
||||
and any(
|
||||
param in name.lower()
|
||||
for name in entry.names
|
||||
for param in self.get_filtered_params()
|
||||
)
|
||||
)
|
||||
]
|
||||
|
||||
@@ -41,3 +34,90 @@ class CustomHelpFormatter(DefaultFormatter):
|
||||
super().__call__(console, options, filtered_panel)
|
||||
else:
|
||||
super().__call__(console, options, panel)
|
||||
|
||||
def get_filtered_params(self):
|
||||
"""Return list of parameter names to filter out of help output."""
|
||||
return ['index', 'band', 'ctx', 'target', 'eq_kind']
|
||||
|
||||
|
||||
class StripHelpFormatter(BaseHelpFormatter):
|
||||
"""Help formatter for strip commands that injects <index> after 'strip'."""
|
||||
|
||||
def render_usage(self, console: Console, options: ConsoleOptions, usage) -> None:
|
||||
"""Render the usage line with index argument injected after 'strip'.
|
||||
|
||||
Handles both command groups (COMMAND) and individual commands (commandname [ARGS/OPTIONS]).
|
||||
"""
|
||||
if usage:
|
||||
modified_usage = re.sub(
|
||||
r'(\S+\s+strip)\s+(\w+\s+\[[^\]]+\]|\w+\s+\[[^\]]+\]|\w+(?:\s+\[[^\]]+\])*|COMMAND)',
|
||||
r'\1 <index> \2',
|
||||
str(usage),
|
||||
)
|
||||
if modified_usage == str(usage):
|
||||
modified_usage = re.sub(
|
||||
r'(\S+\s+strip)\s+(\w+)', r'\1 <index> \2', str(usage)
|
||||
)
|
||||
console.print(f'[bold]Usage:[/bold] {modified_usage}')
|
||||
|
||||
|
||||
class StripSubcommandHelpFormatter(BaseHelpFormatter):
|
||||
"""Help formatter for strip subcommands that injects <index> after 'strip'."""
|
||||
|
||||
def render_usage(self, console: Console, options: ConsoleOptions, usage) -> None:
|
||||
"""Render the usage line with index argument injected after 'strip'."""
|
||||
if usage:
|
||||
modified_usage = re.sub(
|
||||
r'(\S+\s+strip)\s+(\w+)\s+(COMMAND)', r'\1 <index> \2 \3', str(usage)
|
||||
)
|
||||
console.print(f'[bold]Usage:[/bold] {modified_usage}')
|
||||
|
||||
|
||||
class BusHelpFormatter(BaseHelpFormatter):
|
||||
"""Help formatter for bus commands that injects <index> after 'bus'."""
|
||||
|
||||
def render_usage(self, console: Console, options: ConsoleOptions, usage) -> None:
|
||||
"""Render the usage line with index argument injected after 'bus'.
|
||||
|
||||
Handles both command groups (COMMAND) and individual commands (commandname [ARGS/OPTIONS])."""
|
||||
if usage:
|
||||
modified_usage = re.sub(
|
||||
r'(\S+\s+bus)\s+(\w+\s+\[[^\]]+\]|\w+\s+\[[^\]]+\]|\w+(?:\s+\[[^\]]+\])*|COMMAND)',
|
||||
r'\1 <index> \2',
|
||||
str(usage),
|
||||
)
|
||||
if modified_usage == str(usage):
|
||||
modified_usage = re.sub(
|
||||
r'(\S+\s+bus)\s+(\w+)', r'\1 <index> \2', str(usage)
|
||||
)
|
||||
console.print(f'[bold]Usage:[/bold] {modified_usage}')
|
||||
|
||||
|
||||
class EqHelpFormatter(BaseHelpFormatter):
|
||||
"""Help formatter for eq commands that works with both strip and bus commands.
|
||||
|
||||
Injects <index> after 'strip' or 'bus' and <band> after 'cell'."""
|
||||
|
||||
def render_usage(self, console: Console, options: ConsoleOptions, usage) -> None:
|
||||
"""Render the usage line with proper <index> placement for both strip and bus commands."""
|
||||
if usage:
|
||||
modified_usage = re.sub(
|
||||
r'(\S+\s+)(\w+)(\s+eq\s+)(COMMAND)', r'\1\2 <index>\3\4', str(usage)
|
||||
)
|
||||
console.print(f'[bold]Usage:[/bold] {modified_usage}')
|
||||
|
||||
|
||||
class CellHelpFormatter(BaseHelpFormatter):
|
||||
"""Help formatter for cell commands that works with both strip and bus commands.
|
||||
|
||||
Injects <index> after 'strip' or 'bus' and <band> after 'cell'."""
|
||||
|
||||
def render_usage(self, console: Console, options: ConsoleOptions, usage) -> None:
|
||||
"""Render the usage line with proper <index> and <band> placement."""
|
||||
if usage:
|
||||
modified_usage = re.sub(
|
||||
r'(\S+\s+)(\w+)(\s+eq\s+cell\s+)(COMMAND)',
|
||||
r'\1\2 <index>\3<band> \4',
|
||||
str(usage),
|
||||
)
|
||||
console.print(f'[bold]Usage:[/bold] {modified_usage}')
|
||||
|
||||
Reference in New Issue
Block a user