for line in sys.stdin:
args = line.strip().split()
if not args:
# skip blank lines
continue
if args[0] == “cmd1”:
cmd1(args[1:])
elif args[0] == “cmd2”:
cmd2(args[1:])
else:
print “unknown command:”, args[0]
# Registry of commands
_registry = {}
def register(function):
"""Add command to registry."""
_registry[function.__name__] = function
def mainloop(prompt="> "):
"""Run event loop."""
import sys
while True:
sys.stdout.write(prompt)
line = sys.stdin.readline()
if not line:
# terminate on end of input
break
args = line.strip().split()
if not args:
# ignore empty lines
continue
ambiguous = False
f = None
# look for command prefix in registry
for k in _registry.keys():
if k.startswith(args[0]):
if f is not None:
ambiguous = True
break
f = _registry[k]
if ambiguous:
print ("Error: %s is an "
"ambiguous command" % args[0])
elif f is None:
print ("Error: %s is an "
"unknown command" % args[0])
else:
if not f(args[1:]):
break
def test(args):
print "test", args
return True
def tilt(args):
print "tilt", args
return True
def verify(args):
print "verify", args
return True
def quit(args):
print "quit"
return False
import cli
cli.register(test)
cli.register(tilt)
cli.register(verify)
cli.register(quit)
cli.mainloop()
print "Returned from command loop"
python3 -m pip install Pmw
python3 -m pip install --user Pmw