Python's cmd library learning

Source: Internet
Author: User


One: cmd introduction


official documents that refer to Python

The cmd class provides a simple framework for writing Line-orientedcommand interpreters. These is often useful for test harnesses, administrativetools, and prototypes that'll later be wrapped in a more sophis Ticatedinterface

CMD is a simple framework for writing programs based on a command-line interpreter, which is often useful for testing tools, management tools, and prototypes later wrapped in more complex interfaces.

II: basic use of CMD

There are several points to note when writing a CMD-based program:

1. to inherit from CMD. CMD Class 2. To initialize the parent Class 3. All commands are preceded by Do_ 4. All command Help is preceded by Help_ 5. A command corresponds to a help, and an error occurs if no help is executed.


Three: cmd example

     

# -*- coding: utf-8 -*- #!/usr/bin/python env# introduces some package import sysimport  cmdimport os# inherits the CMD. CMD class class cli (cmd). CMD):         def __init__ (self):                  #先初始化父类                  cmd.cmd.__init__ (self)                   #设置命令行的提示符                  self.prompt =  "<zhang> "     #第一个命令dir命令 with one parameter              def do_dir (Self,arg):                 if not arg:        &nBsp;               self.help_dir ()                 elif  Os.path.exists (ARG):                         print  "\ n". Join (Os.listdir (ARG))                  else:                          print  "no such pathexists"      #带第二个命令          def do_quit (Self,arg):                 return True     #dir命令的帮助          d Ef help_dir (self):                 print  "syntax:dir path -- displaya list of files and  Directories "     #help命令的帮助         def help_ Quit (self):                 print  "Syntax:quit --terminatesthe application"      #命令的别名          do_q = do_quitif __name__ ==  "__main__": #开始执行cmd   &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;CLI&NBSP;=&NBSP;CLI () #循环接受用户输入的命令          cli.cmdloop ()


IV: Implementation Results

[[email protected] python]# python cmdtest.py <zhang>dirsyntax:dir path--Displaya list of files and directories< ; Zhang>dir/ Tmpkeyring-8mjwb8ferret.ice-unixkeyring-6y9txtorbit-gdm.x11-unixkeyring-e2wqqa.x0-lockpulse-weknqpdx0htqkeyring-aoiv8ekey ring-i47izekeyring-bshjbupulse-8tdhlwhpdc2jkeyring-viwkfekeyring-m5pgn7keyring-pb8q2xkeyring-tme3bwkeyring-gtw1ih.esd-0  <zhang>helpdocumented commands (Type help <topic>): ========================================dir quitundocumented Commands:======================help Q<zhang>q


This article is from the "Focus on Linux" blog, so be sure to keep this source http://forlinux.blog.51cto.com/8001278/1543099

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.