You are on page 1of 1

#----- Commands Executioner -----

cmds = []
funcs = []

def register_cmd(cmd, func):


global cmds, funcs
cmds.append(cmd)
funcs.append(func)

def unregister_cmd(cmd):
global cmds, funcs
i = cmds.index(cmd)
if i >= 0:
del funcs[i], cmds[i]

def execute_cmd(cmd, *args):


global cmds, funcs
if cmd in cmds:
return funcs[cmds.index(cmd)](*args)
return None

#----- Random Test Function -----


buffer = list("Hello world!")

def get_buffer_char(index):
global buffer
if index >= 0 and index <= len(buffer): return buffer[index]
return ""

#----- Test -----


register_cmd('GETCHAR', get_buffer_char)
print(execute_cmd('GETCHAR', 0))
unregister_cmd('GETCHAR')

#----- Notes -----


#Another function can be defined to mimic a terminal's behavior.
#As such, it allows the user to execute particular instruction/commands on devices
as web-servers or one-time algorithms.

You might also like