Using the built-in cmd class

  • Thread starter Dipl. -Ing. Ashu Akoachere
  • Start date
D

Dipl. -Ing. Ashu Akoachere

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
 
D

Daniel Dittmar

Dipl. -Ing. Ashu Akoachere said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,172
Messages
2,570,934
Members
47,474
Latest member
AntoniaDea

Latest Threads

Top