from typing import Callable, Protocol import traceback from CellFrame import AppCliServer from DAP.Core import logIt from optparse import OptionParser from pycfhelpers.node.types import ReplyId class ReplyObject: def __init__(self, reply_id: ReplyId): self.reply_id = reply_id def reply(self, message: str): AppCliServer.setReplyText(message, self.reply_id) class CliCallback(Protocol): def __call__(self, *args, reply_object: ReplyObject, **kwargs) -> None: pass class CFCliCommand: def __init__(self, command: str, callback: CliCallback, help_text: str = ""): """ Use https://docs.python.org/3.10/library/optparse.html, for configuration cli command """ self.command = command self.callback = callback self.help_text = help_text self.parser = OptionParser() def register(self): def callback_wrapper(argv: list[str], reply_id: ReplyId): (options, args) = self.parser.parse_args(argv[1:]) self.callback(*args, reply_object=ReplyObject(reply_id), **options.__dict__) help_text = self.help_text or self.parser.format_help() try: AppCliServer.cmdItemCreate(self.command, callback_wrapper, help_text, "") except Exception: logIt.error(f"Error = {traceback.format_exc()}")