472,145 Members | 1,443 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,145 software developers and data experts.

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
1 1556
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 | last post: by
reply views Thread by Saiars | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.