user3164083
user3164083

Reputation: 1073

python - takes 1 positional argument (2 given)

When I call inform in the python input box, it says I am giving do_inform 2 arguments, but I am only typing "inform" and clicking "OK". Why does it say I am providing two arguments?

import cmd

class DisplayWelcome(cmd.Cmd):
    """Welcome user to game"""

    def do_inform(self):
        k = input('Enter a letter')
        print (k)


def main():
    d = DisplayWelcome()
    #d.do_greet()
    d.do_inform()
    d.cmdloop()

if __name__ == '__main__':
    main()

Upvotes: 1

Views: 519

Answers (1)

Martijn Pieters
Martijn Pieters

Reputation: 1124238

All Cmd.do_* methods are passed the remainder of the command line, even if empty.

When you type inform at the command prompt, the remainder of the line is empty, so Cmd calls self.do_inform('').

Always give all of your do_* methods an argument for that remainder; you can ignore it if you like:

def do_inform(self, rest=None):
    k = input('Enter a letter')
    print (k)

The cmd library documentation doesn't make this very clear; is is mentioned at the top of the cmd.cmdloop() method documentation:

Repeatedly issue a prompt, accept input, parse an initial prefix off the received input, and dispatch to action methods, passing them the remainder of the line as argument.

Upvotes: 1

Related Questions