467,077 Members | 1,026 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,077 developers. It's quick & easy.

Using the built-in cmd class

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?

Ashu

Jul 18 '05 #1
  • viewed: 1470
Share:
1 Reply
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

Jul 18 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by anthony Lichnewsky | last post: by
3 posts views Thread by Kevin Jackson | last post: by
reply views Thread by yenruyee | last post: by
7 posts views Thread by ajikoe@gmail.com | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.