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()退出之后调用该方法;

例子

importcmd
importstring,sys
classCLI(cmd.Cmd):
def__init__(self):
cmd.Cmd.__init__(self)
self.prompt='> '
defdo_hello(self,arg):
print"hello again",arg,"!"
defhelp_hello(self):
print"syntax: hello [message]",
print"-- prints a hello message"
defdo_quit(self,arg):
sys.exit(1)
defhelp_quit(self):
print"syntax: quit",
print"-- terminates the application"
# shortcuts
do_q=do_quit
# try it out
cli=CLI()
cli.cmdloop()
>>>help
Documentedcommands(type help):
========================================
hello
quit
Undocumentedcommands:
======================
help
q
>>>hello world
hello again world!
>>>q
打印目录文件或目录的例子
importcmd
importos
importsys
classCLI(cmd.Cmd):
def__init__(self):
cmd.Cmd.__init__(self)
self.prompt="> "# define command prompt
defdo_dir(self,arg):
ifnotarg:
self.help_dir()
elifos.path.exists(arg):
print"\n".join(os.listdir(arg))
else:
print"No such pathexists."
defhelp_dir(self):
print"syntax: dir path -- displaya list of files and directories"
defdo_quit(self,arg):
returnTrue
defhelp_quit(self):
print"syntax: quit -- terminatesthe application"
# define the shortcuts
do_q=do_quit
if__name__=="__main__":
cli=CLI()

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方法才会启用,需要自行添加。

defdo_shell(self,arg):
"run a shell commad"
print">",arg
sub_cmd=subprocess.Popen(arg,shell=True,stdout=subprocess.PIPE)
printsub_cmd.communicate()[0]