Jump to content

Check out our Community Blogs

Register and join over 40,000 other developers!

Recent Status Updates

View All Updates

- - - - -

Python cmd Module

cmd python-2.7

  • Please log in to reply
1 reply to this topic

#1 PurityLake


    CC Regular

  • Member
  • PipPipPip
  • 36 posts
  • Location:Ireland
  • Programming Language:C++, Python
  • Learning:C, C++, Lua

Posted 12 March 2013 - 01:47 PM

cmd is a python module that is a basic command line application. Simple as but it can be a very powerful tool, if you want to have a simple command line to run certain process on your computer. cmd is a very adaptable module, it allows for a lot of overridden methods to customize how your commands are interpreted, what to do just before a loop, what to do just after a loop and more.


Let's take a look at an example:  




# terminal.py
import cmd

class terminal(cmd.Cmd):
    def preloop(self):

        print "we are in the preloop function" # this will be called before terminal.cmdloop() begins

    def postloop(self):

        print "we are in the postloop function" # this will be called when terminal.cmdloop() exits

    def do_hello(self, e):
        Say hello to everyone
        print "Hello, world"

    def do_exit(self, e):
        This exits the cmdloop
        return True

if __name__ == "__main__":

    term = terminal()

Sample Commands:


C:\> python terminal.py
we are in the preloop function
(Cmd) hello
Hello, World
(Cmd) help hello
                Say hello
(Cmd) exit
we are in the post loop function 




help command on its own will print all the names of all documented and undocumented functions, but with an arugment it will return the docstring of the function if it exists


Ok, that is pretty easy now isn't it? A lot of the functions are very self-explanatory but its easier to go over them




This is actually a functions inherited from cmd.Cmd. This functions is run when you call term.cmdloop(). This can be used to set up certain variables before you start running your cmdloop(). 





Again this is a function that is inherited from cmd.Cmd. This function runs just after your term.cmdloop() ends. This can be used to clean up after it or to save other things, you decide




do_hello(self, e)
do_exit(self, e)

These are functions that you design. These will be run when the keyword (hello or exit in this case) are passed to the program. These functions must be started by do_. This is how the loop understands what commands are valid. Also another thing you may have noticed is the unused parameter e. Well, try remove it and see what happens. You get something like the following:


def do_hello(self)


(cmd) hello
Traceback (most recent call last):
  File "terminal.py", line 24, in (module)
  File "C:\Python27\lib\cmd.py", line 142, in cmdloop
    stop = self.onecmd(line)
  File "C:\Python27\lib\cmd.py", line 219, in onecmd
     return func(arg)
TypeError: do_hello() takes exactly 1 argument (2 given)

What just happened? Well cmdloop() uses onecmd() which takes your input and adds the parameter(s) supplied (or nothing in the case of do_hello()) so basically the program tries to pass parameters when you don't want them so the easiest way around it is to make your functions take a parameter even if they don't need it.


A few handy extras:

Cmd.prompt: This can be changed get rid of that horrible looking "(Cmd)" default prompt

Cmd.onecmd(str): This is the function that interprets the commands and runs the required function

Cmd.precmd(line) & Cmd.postcmd(stop, line): These are the very similiar to Cmd.preloop() and Cmd.postloop() except that they are used in conjunction with Cmd.onecmd(str)

Cmd.emptyline(): Simply is a function that is called if the command you pass when you hit the enter key is empty, this function defaults to recalled the last previous successful command which is stored in Cmd.lastcmd

Cmd.default(line): This is called if the command you pass, is not known as a valid keyword. This defaults to "*** Unknown syntax: notACommand" replace notACommand with anything else that isn't defined



Well I hope you enjoyed this tutorial and that it helped you. Please comment if you want more to be added or explained in more detail



Python wiki






  • 1

#2 wa11a


    CC Lurker

  • New Member
  • Pip
  • 5 posts
  • Programming Language:C, Java, C++, C#, Python
  • Learning:JavaScript

Posted 23 August 2013 - 09:02 AM

thank you, very nice.

  • 0

Also tagged with one or more of these keywords: cmd, python-2.7

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download