This is something I've been working on for a bit, and I think it is
more or less ready to bring up on this list. I'd like to add a module
(though probably not for 2.5).
Before you ask, this module is _not_ compatible with cmd.py, as it is
command oriented, whereas cmd.py is line oriented.
Anyway, I'm looking for feedback, feature requests before starting the
submission process.
Code available here:
http://littlelanguages.com/web/softw...les/cmdloop.py
Base class for writing simple interactive command loop environments.
CommandLoop provides a base class for writing simple interactive user
environments. It is designed around sub-classing, has a simple command
parser, and is trivial to initialize.
Here is a trivial little environment written using CommandLoop:
import cmdloop
class Hello(cmdloop.C ommandLoop):
PS1='hello>'
@cmdloop.aliase s('hello', 'hi', 'hola')
@cmdloop.shorth elp('say hello')
@cmdloop.usage( 'hello TARGET')
def helloCmd(self, flags, args):
'''
Say hello to TARGET, which defaults to 'world'
'''
if flags or len(args) != 1:
raise cmdloop.Invalid Arguments
print 'Hello %s!' % args[0]
@cmdloop.aliase s('quit')
def quitCmd(self, flags, args):
'''
Quit the environment.
'''
raise cmdloop.HaltLoo p
Hello().runLoop ()
Here's a more complex example:
import cmdloop
class HelloGoodbye(cm dloop.CommandLo op):
PS1='hello>'
def __init__(self, default_target = 'world'):
self.default_ta rget = default_target
self.target_lis t = []
@cmdloop.aliase s('hello', 'hi', 'hola')
@cmdloop.shorth elp('say hello')
@cmdloop.usage( 'hello [TARGET]')
def helloCmd(self, flags, args):
'''
Say hello to TARGET, which defaults to 'world'
'''
if flags or len(args) > 1:
raise cmdloop.Invalid Arguments
if args:
target = args[0]
else:
target = self.default_ta rget
if target not in self.target_lis t:
self.target_lis t.append(target )
print 'Hello %s!' % target
@cmdloop.aliase s('goodbye')
@cmdloop.shorth elp('say goodbye')
@cmdloop.usage( 'goodbye TARGET')
def goodbyeCmd(self , flags, args):
'''
Say goodbye to TARGET.
'''
if flags or len(args) != 1:
raise cmdloop.Invalid Arguments
target = args[0]
if target in self.target_lis t:
print 'Goodbye %s!' % target
self.target_lis t.remove(target )
else:
print "I haven't said hello to %s." % target
@cmdloop.aliase s('quit')
def quitCmd(self, flags, args):
'''
Quit the environment.
'''
raise cmdloop.HaltLoo p
def _onLoopExit(sel f):
if len(self.target _list):
self.pushComman ds(('quit',))
for target in self.target_lis t:
self.pushComman ds(('goodbye', target))
else:
raise cmdloop.HaltLoo p
HelloGoodbye(). runLoop()