Skip to main content

cmd.Cmd module

Python has lots of surprises for it's lovers, one such pleasant one is the cmd.Cmd module

 A programmer might encounter tons of project where she might have to write a command line interpreter for his program to execute it. Typical undergrad lab. projects have tonnes of such project where one has to implement something, say for example Graph or a toy ticket reservation system. All these projects require the developer to create a command terminal which will prompt the user to write a command from some predefined set of them and then print the result in the terminal.

Python provides a brilliant solution to such repreatative task in the form of a module cmd.Cmd. The best part is that the it gives the developer the freedom to do his implementation task independently to the command interpreter module. Cmd module offers tonnes of feature, some of them are --
  1. command auto-completion
  2. auto help functions
  3. auto generated description of the command from the python docs.
  4. function stubs as place holders for the python functions.
  5. command history, if readline module is available.
 I will post a toy example for better understanding as a standing point. For more advanced usage please refer the documentation page of Cmd module.

1:  from cmd import Cmd  
2:    
3:    
4:    
5:  class Console(Cmd):  
6:    def __init__(self):  
7:      Cmd.__init__(self)  
8:      self.prompt = '=>>'  
9:      self.intro = "Enter Commands"  
10:    
11:    def emptyline(self):  
12:      pass  
13:    def default(self, line):  
14:      print "Unrecognized Command"  
15:    
16:    #Exit Commands  
17:    def do_EOF(self, args):  
18:      return -1  
19:    def do_exit(self, args):  
20:      self.do_EOF(args);  
21:    
22:    #Commands  
23:    def do_CommandA(self, args):  
24:      """ Help on Command A """  
25:      print "Command A executed"  
26:    def do_CommandB(self, args):  
27:      """ Help on Command B """  
28:      print "Command B executed"  
29:    
30:  if __name__=="__main__":  
31:    Console().cmdloop()  
32:    

The result when this code is run of a terminal is something like this,

Comments

Popular posts from this blog

Multimaster replication with Symmetric DS

Symmetric DS is an awesome tool for trigger based replication whcih works for all major database vendors, including but not limited to PostgreSQL, MySQL, MSSQL, Oracle and many others. Symmetric-DS is a java application and can execute on any platform on whcih JRE is available including Windows and Linux. Trigger based replication, in constrast to disk based (eg. DRBD ) or transaction log file shipping based or statement based , works by registering triggers on DMLs and sending the data thus generated to remote machines. Another very popular trigger based DB replication tool is Slony . Symmetric-DS in addition to being database agnostic also supports multi-master replication (MMR). MMR usecase involves multiple database nodes, connected in a pool with DML updates coming from any of them. This is different from the normal master-slave replication, where slaves are not expected to generate any data events, and the sole authority of database is the master. MMR requirement causes d...

Reset root password RHEL/Rocky/CentOS 9

Unlike the earlier versions of Rethat variants, version 9 doesn't allow single user mode to change password, as maintanance mode in 9 requires root password . Single user mode (runlevel 1) can easily be obtained by appending the word ' single ' at the end of the line starting with 'linux' by editing the entry in boot menu by pressing ' e ' at boot menu. To reset the root password on the other hand, one requires to follow a specific set of commands, At the boot menu, edit rescue mode to append 'rd.break ' at the end of the line starting with kernel. Boot with the edited line by pressing Ctrl+X or F10. At the new prompt starting with switch_root, type the following commands, mount -o remount, rw /sysroot chroot /sysroot touch /.autorelabel passwd <new root password> exit reboot       

Devstack installation with cells in multiple machines

  Devstack is a testing and development package for OpenStack . The official devstack website has excellent but terse installation instructions, which could be daunting to a newbie. Normally, a traditional install is pretty straightforward but problems creep in when you have to go for some particular configuration or installation like cells or a simple code-test-debug setup, the information on the main site is insufficient. In this blog, I will enumerate the steps for setting up a cell-ular devstack installation with one parent and one child. Parent will not be instantiating any VMs s it's only a 'command centre'. 1. On both machines, install git and vim, clone the devstack repo using git. yum install git vim git clone https://git.openstack.org/openstack-dev/devstack 2. On both machines, open up the file 'local.conf' inside the cloned directory 'devstack' cd devstack; vim local.conf 3. Copy the parameters below to the file...