Skip to content
Snippets Groups Projects
Commit b14900a2 authored by Dmitry Puzyrkov's avatar Dmitry Puzyrkov
Browse files

Merge branch 'add_cli' into 'main'

add cli optparse format

See merge request !2
parents 173aa38f e7b95f00
No related branches found
No related tags found
1 merge request!2add cli optparse format
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()}")
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment