[Python-Dev] New Module: CommandLoop (original) (raw)

Crutcher Dunnavant crutcher at gmail.com
Sun Feb 19 23:02:20 CET 2006


oops, error in the example: s/commandLoop/CommandLoop/g

On 2/19/06, Crutcher Dunnavant <crutcher at gmail.com> wrote:

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/software/python/modules/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.commandLoop): PS1='hello>' @cmdloop.aliases('hello', 'hi', 'hola') @cmdloop.shorthelp('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.InvalidArguments print 'Hello %s!' % args[0] @cmdloop.aliases('quit') def quitCmd(self, flags, args): ''' Quit the environment. ''' raise cmdloop.HaltLoop Hello().runLoop() Here's a more complex example: import cmdloop class HelloGoodbye(cmdloop.CommandLoop): PS1='hello>' def init(self, defaulttarget = 'world'): self.defaulttarget = defaulttarget self.targetlist = [] @cmdloop.aliases('hello', 'hi', 'hola') @cmdloop.shorthelp('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.InvalidArguments if args: target = args[0] else: target = self.defaulttarget if target not in self.targetlist: self.targetlist.append(target) print 'Hello %s!' % target @cmdloop.aliases('goodbye') @cmdloop.shorthelp('say goodbye') @cmdloop.usage('goodbye TARGET') def goodbyeCmd(self, flags, args): ''' Say goodbye to TARGET. ''' if flags or len(args) != 1: raise cmdloop.InvalidArguments target = args[0] if target in self.targetlist: print 'Goodbye %s!' % target self.targetlist.remove(target) else: print "I haven't said hello to %s." % target @cmdloop.aliases('quit') def quitCmd(self, flags, args): ''' Quit the environment. ''' raise cmdloop.HaltLoop def onLoopExit(self): if len(self.targetlist): self.pushCommands(('quit',)) for target in self.targetlist: self.pushCommands(('goodbye', target)) else: raise cmdloop.HaltLoop HelloGoodbye().runLoop() -- Crutcher Dunnavant <crutcher at gmail.com> littlelanguages.com monket.samedi-studios.com

-- Crutcher Dunnavant <crutcher at gmail.com> littlelanguages.com monket.samedi-studios.com



More information about the Python-Dev mailing list