Using the built-in cmd class
Daniel Dittmar
daniel.dittmar at sap.com
Tue Dec 9 04:52:43 EST 2003
Dipl. -Ing. Ashu Akoachere wrote:
> Has any one got experience in using the built-in cmd class? I have
> derived a subclass from the base class, but it seems as if the
> arguments of functions using the cmd-interpreter are limited in their
> number. Precisely' I have something like this:
> .
> .def help_add(self):
> print "Adds arguments"
>
> def do_add(self,arg1,arg2):
> return (arg1 + arg2)
>
> Python complains about the number of arguments in the do_add
> function. Has anyone got a modified cmd that can handle multiple
> arguments?
The do_* methods get a string consisting of the remainder of the command
line.
So your method has to look like this:
def do_add (self, strarg):
intargs = [int (arg) for arg in strarg.split ()]
print intargs [0] + intargs [1]
If you return something true from a do_* method, the command loop will stop,
this is probably not what you intended.
You can do the split outside of all your do_* methods. This could be done by
overriding the method parseline:
def parseline (self, line):
cmd, arg, line = Cmd.parseline (self, line)
return cmd, arg.split (), line
But if you really want to define the number of arguments in your do_add,
then typing 'add 1 2 3' would lead to a TypeError exception, which is
probably not helpful for the user.
Daniel
More information about the Python-list
mailing list