[Python-Dev] New Module: CommandLoop

Raymond Hettinger python at rcn.com
Mon Feb 20 00:14:35 CET 2006

[Crutcher Dunnavant]
> Anyway, I'm looking for feedback, feature requests before starting the
> submission process.

With respect to the API, the examples tend to be visually dominated dominated by 
the series of decorators.  The three decorators do nothing more than add a 
function attribute, so they are essentially doing the same type of action. 
Consider combining those into a single decorator and using keywords for the 
various arguments.  For example, change:

        @cmdloop.shorthelp('say goodbye')
        @cmdloop.usage('goodbye TARGET')

to just:

        @cmdloop.addspec(aliases=['goodbye'], shorthelp ='say goodbye', 
usage='goodbye TARGET')

leaving the possibility of multiple decorators when one line gets to long:

        @cmdloop.addspec(aliases=['goodbye'], shorthelp ='say goodbye')
        @cmdloop.addspec(usage='goodbye TARGET  # where TARGET is a filename in 
the current directory')

Another thought on the API is to consider adding another decorator option to 
make commands case-insensitive so that 'help', 'HELP', and 'Help' will all work:

Also, in the absence of readline(), consider adding support for "!" style 
repeats of previous commands.

The exception hierarchy looks to be well-designed.  I'm not clear on whether it 
is internal or part of the API.  If the latter, is there an example of how to 
trap and handle specific exceptions in specific contexts?

If you're interested, here are a few code comments based on my first 

1) The "chars" variable can be eliminated and the "while chars" and 
"c=chars.pop(0)" sequence simplified to just:
    for c in reversed(str):
        . . .

2) Can the reformatDocString() function be replaced by textwrap.dedent() or do 
they do something different?

3) In _mapCommands(), the sort can be simplified from:
        self._cmds.sort(lambda a, b: cmp(a.aliases[0], b.aliases[0]))
    or if you want to avoid module dependencies:
        self._cmds.sort(key=lambda a: a[0])

4) In _preCommand, the sort simplifies from:
                    names = self.aliasdict.keys()
                    for name in names:
                    for name in sorted(self.aliasdict):


More information about the Python-Dev mailing list