The order example demonstrates how you can create an custom command. This example consists of one python script /demoCustomCMD.py and manifest file /manifest.json.
from DAP.Core import logIt
from CellFrame import AppCliServer
"""
This function takes two arguments
argv is an array of incoming arguments
indexStrReply is an internal index that correlates what is needed
to fill the desired buffer with the data that will be passed to the CLI.
"""
def cmdDemo(argv, indexStrReply):
reply = "Arguments :\n"
for i in range(len(argv)):
reply += "arg["+str(i)+"]: "+argv[i]+"\n"
AppCliServer.setReplyText(reply, indexStrReply)
def cmdDemo2(argv, indexStrReply):
AppCliServer.setReplyText("I simple demo command", indexStrReply)
def init():
logIt.notice("Running plugin order")
"""
The cmdItemCreate function creates a CLI command.
This function takes four arguments.
Command name, command handler function, short description of
of the command, full description of the command.
"""
AppCliServer.cmdItemCreate("demo", cmdDemo, "Command demo",
"""
This command is intended to demonstrate the work of custom command in the CellFrame API for Python.
""")
AppCliServer.cmdItemCreate("demo2", cmdDemo2, "Second command demo",
"""
This is demo and testing
""")
return 0
{
"name": "demoCustomCMD",
"version": "1.0",
"author": "DEMLABS (C) 2022",
"dependencies": [],
"description": "This is a plugin example for working custom command."
}
This /demoCustomCMD.py script contains three functions: cmdDemo
, cmdDemo2
and init
.
The execution of the plugin starts with the init
. The cmdDemo
is a handler of an demo user's command and cmdDemo2
is a handler of an demo2 command.
The demo command should have a following format:
demo <arg1> <arg2> ... <argN>
The demo command prints all arguments.
The demo2 command should have a following format:
demo2
The demo command prints a "I simple demo command." string.
init
function:def init():
logIt.notice
function logs a message: logIt.notice("Running plugin order")
cmdItemCreate
function creates a CLI command. This function takes four arguments: a command name, a command handler function, short description of the command, full description of the command: AppCliServer.cmdItemCreate("demo", cmdDemo, "Command demo",
"""
This command is intended to demonstrate the work of custom command in the CellFrame API for Python.
""")
AppCliServer.cmdItemCreate("demo2", cmdDemo2, "Second command demo",
"""
This is demo and testing
""")
return 0
cmdCMD
functions gets two arguments: argv
and indexStrReptly
. The argv
is a list that contains the command name and the arguments that the user specified when running the demo command. The indexStrReply
argument is intended to respond to the user CLI.def cmdDemo(argv, indexStrReply):
reply
variable is used to format a response.reply = "Arguments :\n"
for i in range(len(argv)):
reply += "arg[" + str(i) + "]: " + argv[i] + "\n"
setReplyText
method sets the response to the user's command. The reply
is a string that will be sent to the client. The indexStrReply
is an argument from the cmdDemo
function. AppCliServer.setReplyText(reply, indexStrReply)
Setting the response to the user's command using the The setReplyText
method.
def cmdDemo2(argv, indexStrReply):
AppCliServer.setReplyText("I simple demo command.", indexStrReply)