Cmd Module

G

Godwin Burby

Dear Pythoneer,
I was just curious about using the cmd module for building my
own command line interface. i saw a problem. The script is as follows:

from cmd import Cmd
import getpass

class CmdTest(cmd):
def __init__(self):
super(CmdTest, self).__init__()

def do_login(self):
passwd = getpass.getpass()
if passwd = 'godwin': print "You are a valid user"

if __name__ == '__main__':
ctest = CmdTest()
ctest.cmdloop()

The interpreter reports that the first argument to super should be a
type rather than a class object and for the do_login function it says
that function needs only one argument but two are given. I solved the
above errors by adding the following code:

Cmd.__init__(self)

def do_login(self,passwd='godwin')

But i know that my first code should work without any problems or is
there a problem with it.
Pls enlighten me.
 
F

Fredrik Lundh

Godwin said:
I was just curious about using the cmd module for building my
own command line interface. i saw a problem. The script is as follows:

it helps if you include the code you were running, instead of some
approximation of it.

File "test", line 10
if passwd = 'godwin': print "You are a valid user"
^
SyntaxError: invalid syntax

Traceback (most recent call last):
The interpreter reports that the first argument to super should be a
type rather than a class object and for the do_login function it says
that function needs only one argument but two are given. I solved the
above errors by adding the following code:

Cmd.__init__(self)

def do_login(self,passwd='godwin')

But i know that my first code should work without any problems or is
there a problem with it.

super() only works for new-style classes.

the second argument problem is because do_ methods are called with
a second argument:
Help on module cmd:
...
3. A command `foo' is dispatched to a method 'do_foo()'; the do_ method
is passed a single argument consisting of the remainder of the line.
...

(if you type "login", you get an empty string. if you type "login foo", you get
the string "foo". etc).

</F>
 
G

Godwin Burby

Sorry! I just retyped my script instead of copy pasting it.
well thank u once again for clearing my confusion.
 

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

Forum statistics

Threads
473,780
Messages
2,569,607
Members
45,240
Latest member
pashute

Latest Threads

Top