[关闭]
@bergus 2015-12-02T06:20:01.000000Z 字数 2765 阅读 4259

python笔记之Cmd模块

python python模块 cmd


Cmd类型提供了一个创建命令行解析器的框架,默认情况下,它使用readline来进行交互式操作、命令行编辑和命令完成.

    使用cmd创建的命令行解释器循环读取输入的所有行并且解析它们,然后发送命令给一个合适的命令处理器。输入行为解析成两个部分:命令和参数。如果用户输入cmd param,它解释成命令cmd和参数param,然后使用param作为参数调用do_cmd方法。如果do_cmd命令处理器返回真,那么程序会干净的退出,否则会继续等待下一个命令的输入。
    简单的来说,可以继承Cmd来创建命令行界面,然后对所有想处理的命令command执行do_command方法。这个方法会接受命令行的其余部分作为自己的唯一参数。比如,如果在命令行中输入:
    say hello
    do_say方法会连同作为唯一参数的字符串"hello"一起调用。

Cmd一些常用的方法

    cmdloop():类似与Tkinter的mainloop,运行Cmd解析器;
    onecmd(str):读取输入,并进行处理,通常不需要重载该函数,而是使用更加具体的do_command来执行特定的命名;
    emptyline():当输入空行时调用该
    default(line):当无法识别输入的command时调用该方法;
    completedefault(text,line,begidx,endidx):如果不存在针对的complete_*()方法,那么会调用该函数
    precmd(line):命令line解析之前被调用该方法;
    postcmd(stop,line):命令line解析之后被调用该方法;
    preloop():cmdloop()运行之前调用该方法;
    postloop():cmdloop()退出之后调用该方法;

例子

  1. import cmd
  2. import string, sys
  3. class CLI(cmd.Cmd):
  4. def __init__(self):
  5. cmd.Cmd.__init__(self)
  6. self.prompt = '> '
  7. def do_hello(self, arg):
  8. print "hello again", arg, "!"
  9. def help_hello(self):
  10. print "syntax: hello [message]",
  11. print "-- prints a hello message"
  12. def do_quit(self, arg):
  13. sys.exit(1)
  14. def help_quit(self):
  15. print "syntax: quit",
  16. print "-- terminates the application"
  17. # shortcuts
  18. do_q = do_quit
  19. # try it out
  20. cli = CLI()
  21. cli.cmdloop()
  22. >>> help
  23. Documented commands (type help <topic>):
  24. ========================================
  25. hello
  26. quit
  27. Undocumented commands:
  28. ======================
  29. help
  30. q
  31. >>> hello world
  32. hello again world !
  33. >>> q

打印目录文件或目录的例子

  1. import cmd
  2. import os
  3. import sys
  4. classCLI(cmd.Cmd):
  5. def __init__(self):
  6. cmd.Cmd.__init__(self)
  7. self.prompt = "> " # define command prompt
  8. def do_dir(self, arg):
  9. if not arg:
  10. self.help_dir()
  11. elif os.path.exists(arg):
  12. print"\n".join(os.listdir(arg))
  13. else:
  14. print "No such pathexists."
  15. def help_dir(self):
  16. print "syntax: dir path -- displaya list of files and directories"
  17. def do_quit(self, arg):
  18. return True
  19. def help_quit(self):
  20. print "syntax: quit -- terminatesthe application"
  21. # define the shortcuts
  22. do_q = do_quit
  23. if __name__ =="__main__":
  24. cli = CLI()
  25. cli.cmdloop()
     上面的代码中self.prompt用于修改命令行的提示符。
说明:在cmd.Cmd派生类中,以do_*开头为命令,以help_*开头的为帮助,成员函数的__doc__可以用作为帮助说明。

     可以使用self.intro = “Welcomemessage”来输出欢迎信息。
当打印帮助的时候,使用doc_header、misc_header、undoc_header可以用于格式化输出。

     通过按下TAB可以实现自动完成,多个选项的情况下按下2个TAB键可以实现列出所有选项,自动完成的方法添加前缀complete_。
defcomplete_dir(self, text, line, begidx, endidx):
    if not text:
        completions = ["aaa","bbb", "ccc"]
    else:
        completions = ["ddd","eee",]
    #print completions
    return completions

注意:自动补全只在Linux下有效。

    在cmd中几种可以重载的方法,可以用于显示动作或者改变基类的行为。一般而言由于preloop()和postloop()的存在,不需要也没有必要重载cmdloop()方法。每次迭代cmdloop()和onecmd()来分发命令给处理器时,实际输入行为使用parseline()来解析,创建一个元组包含命令和参数。如果改行为空会调用emptyline()方法。默认实现运行以前的命令,如果改行包含一个命令,首先调用precmd(),然后查找调用处理器的方法,不管有没有找到命令都会调用postcmd()。

    为了补充标准命令处理,cmd包含两个特殊命令的前缀,问号(?)相当于内置的帮助命令,感叹号(!)会映射到do_shell(),以运行其它的命令,感叹号需要有do_shell方法才会启用,需要自行添加。
  1. def do_shell(self, arg):
  2. "run a shell commad"
  3. print ">", arg
  4. sub_cmd = subprocess.Popen(arg,shell=True, stdout=subprocess.PIPE)
  5. print sub_cmd.communicate()[0]
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注